Skip to content

Instantly share code, notes, and snippets.

@Gomah
Created October 22, 2017 13:21
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 Gomah/11a9228c3165ecf972ddd8a78cc8b3ad to your computer and use it in GitHub Desktop.
Save Gomah/11a9228c3165ecf972ddd8a78cc8b3ad to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill
(() => {
let lastTime = 0;
const vendors = ['ms', 'moz', 'webkit', 'o'];
for (let x = 0; x < vendors.length && !window.requestAnimationFrame; x += 1) {
window.requestAnimationFrame = window[`${vendors[x]}RequestAnimationFrame`];
window.cancelAnimationFrame =
window[`${vendors[x]}CancelAnimationFrame`] ||
window[`${vendors[x]}CancelRequestAnimationFrame`];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = cb => {
const currTime = new Date().getTime();
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
const id = window.setTimeout(() => {
cb(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = id => {
clearTimeout(id);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment