Skip to content

Instantly share code, notes, and snippets.

@smilack

smilack/Main.js

Last active Sep 16, 2020
Embed
What would you like to do?
RequestAnimationFrame with timestamp
"use strict"
exports._requestAnimationFrame = function(fn) {
return function(window) {
return function() {
return window.requestAnimationFrame(function(timestamp) {
fn(timestamp)();
});
};
};
};
module Main (main) where
import Prelude
import Data.Newtype (wrap)
import Effect (Effect)
import Effect.Console (log)
import Web.HTML (Window, window)
import Web.HTML.Window (RequestAnimationFrameId, requestAnimationFrame)
main :: Effect Unit
main = do
_ <- requestAnimationFrameWithTime callbackWithTime =<< window
_ <- requestAnimationFrame callback =<< window
pure unit
callback :: Effect Unit
callback = log "callback without time"
foreign import _requestAnimationFrameWithTime :: (Number -> Effect Unit) -> Window -> Effect Int
requestAnimationFrameWithTime
:: (Number -> Effect Unit) -> Window -> Effect RequestAnimationFrameId
-- requestAnimationFrameWithTime fn = map wrap <<< _requestAnimationFrameWithTime fn
requestAnimationFrameWithTime fn window = erAFId
where
eInt :: Effect Int
eInt = _requestAnimationFrameWithTime fn window
erAFId :: Effect RequestAnimationFrameId
erAFId = map wrap eInt
callbackWithTime :: Number -> Effect Unit
callbackWithTime time = log $ "callback with time: " <> show time
"use strict";
var $foreign = require("./foreign.js");
var Control_Bind = require("../Control.Bind/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_Newtype = require("../Data.Newtype/index.js");
var Data_Show = require("../Data.Show/index.js");
var Data_Unit = require("../Data.Unit/index.js");
var Effect = require("../Effect/index.js");
var Effect_Console = require("../Effect.Console/index.js");
var Web_HTML = require("../Web.HTML/index.js");
var Web_HTML_Window = require("../Web.HTML.Window/index.js");
var requestAnimationFrameWithTime = function (fn) {
return function (window) {
var eInt = $foreign["_requestAnimationFrameWithTime"](fn)(window);
var erAFId = Data_Functor.map(Effect.functorEffect)(Data_Newtype.wrap(Web_HTML_Window.newtypeRequestAnimationFrameId))(eInt);
return erAFId;
};
};
var callbackWithTime = function (time) {
return Effect_Console.log("callback with time: " + Data_Show.show(Data_Show.showNumber)(time));
};
var callback = Effect_Console.log("callback without time");
var main = function __do() {
Control_Bind.bindFlipped(Effect.bindEffect)(requestAnimationFrameWithTime(callbackWithTime))(Web_HTML.window)();
Control_Bind.bindFlipped(Effect.bindEffect)(Web_HTML_Window.requestAnimationFrame(callback))(Web_HTML.window)();
return Data_Unit.unit;
};
module.exports = {
main: main
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.