Skip to content

Instantly share code, notes, and snippets.

@radist2s
Forked from paulirish/rAF.js
Last active November 29, 2015 14:52
Show Gist options
  • Save radist2s/9722283 to your computer and use it in GitHub Desktop.
Save radist2s/9722283 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
// MIT license
!window.requestAnimationFrame && (window.requestAnimationFrame = null)
!window.cancelAnimationFrame && (window.cancelAnimationFrame = null)
!function() {
var lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o']
if (!cancelAnimationFrame) {
for (var i = 0; i < vendors.length && !requestAnimationFrame; ++i) {
requestAnimationFrame = window[vendors[i] + 'RequestAnimationFrame']
cancelAnimationFrame =
window[vendors[i] + 'CancelAnimationFrame'] || window[vendors[i] + 'CancelRequestAnimationFrame']
}
}
if (!requestAnimationFrame) {
requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime(),
timeToCall = Math.max(0, 16 - (currTime - lastTime))
var id = setTimeout(function () {
callback(currTime + timeToCall)
}, timeToCall)
lastTime = currTime + timeToCall
return id
};
}
if (!cancelAnimationFrame) {
cancelAnimationFrame = function(id) {
clearTimeout(id)
};
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment