Created
July 4, 2015 06:22
-
-
Save glued/151fb9e241a33067d9ce to your computer and use it in GitHub Desktop.
ES2015 requestAnimationFrame polyfill
This file contains 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
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | |
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating | |
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel | |
// MIT license | |
/* | |
to use just create a file raf.js | |
import './utils/raf'; | |
*/ | |
(function() { | |
let vendors = ['ms', 'moz', 'webkit', 'o']; | |
let lastTime = 0; | |
for(let x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { | |
let vendor = vendors[x]; | |
window.requestAnimationFrame = window[`${vendor}RequestAnimationFrame`]; | |
window.cancelAnimationFrame = window[`${vendor}CancelAnimationFrame`] || window[`${vendor}CancelRequestAnimationFrame`]; | |
} | |
if(!window.requestAnimationFrame){ | |
window.requestAnimationFrame = (callback) => { | |
let currTime = Date.now(); | |
let timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
let id = window.setTimeout(() => callback(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