Skip to content

Instantly share code, notes, and snippets.

@smilack
Last active September 16, 2020 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smilack/b2026158f0b35e5dbc4d271009d87614 to your computer and use it in GitHub Desktop.
Save smilack/b2026158f0b35e5dbc4d271009d87614 to your computer and use it in GitHub Desktop.
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