Created
November 2, 2015 12:36
-
-
Save domasx2/e97dce67349fb46f01de to your computer and use it in GitHub Desktop.
Wrap requestAnimationFrame to implement return value & cancelAnimationFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const callbacks = new Map(); | |
let id = 0; | |
const realRequestAnimationFrame = window.requestAnimationFrame; | |
window.requestAnimationFrame = function(cb) { | |
id++; | |
callbacks.set(id, cb); | |
return id; | |
} | |
window.cancelAnimationFrame = function(id) { | |
callbacks.delete(id); | |
} | |
function tick(timestamp) { | |
realRequestAnimationFrame(tick); | |
let cbs = Array.from(callbacks.values()); | |
callbacks.clear(); | |
for (let cb of cbs) { | |
cb(timestamp); | |
} | |
} | |
realRequestAnimationFrame(tick); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment