Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Forked from paulirish/rAF.js
Created February 7, 2012 02:50
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 jcayzac/1756825 to your computer and use it in GitHub Desktop.
Save jcayzac/1756825 to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill
// 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 and Julien Cayzac
(function() {
var lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o'],
x,
ok = function() { return window.requestAnimationFrame && window.cancelAnimationFrame }
for (x in vendors) {
window.requestAnimationFrame = w[vendors[x]+'RequestAnimationFrame']
window.cancelAnimationFrame = w[vendors[x]+'CancelAnimationFrame'] || w[vendors[x]+'CancelRequestAnimationFrame']
if (ok()) return
}
window.requestAnimationFrame = function(callback, ignored) {
var currTime = new Date().getTime(),
timeToCall = Math.max(1, 16 - (currTime - lastTime)),
id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall)
lastTime = currTime + timeToCall
return id
}
window.cancelAnimationFrame = function(id) { clearTimeout(id) }
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment