Skip to content

Instantly share code, notes, and snippets.

@aMarCruz
Last active August 29, 2015 14:20
Show Gist options
  • Save aMarCruz/401c98f44e2360429b68 to your computer and use it in GitHub Desktop.
Save aMarCruz/401c98f44e2360429b68 to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill by aMarCruz
// requestAnimationFrame polyfill by aMarCruz.
// 2015/01/19
// 2015/05/09 - last rev
// Adapted from https://gist.github.com/paulirish/1579671 which derived from
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// Fixes from Paul Irish, Tino Zijdel, Andrew Mao, Klemen Slavič, Darius Bacon, Erik Möller.
// MIT license
!(function(win) {
var haveraf = function(vendor) {
return win.requestAnimationFrame && win.cancelAnimationFrame ||
(
(win.requestAnimationFrame = win[vendor + 'RequestAnimationFrame']) &&
(win.cancelAnimationFrame = (win[vendor + 'CancelAnimationFrame'] ||
win[vendor + 'CancelRequestAnimationFrame']))
);
};
// there is not more -o- or -ms- prefix
if (!haveraf('webkit') && !haveraf('moz') ||
/iP(ad|hone|od).*OS 6/.test(win.navigator.userAgent)) { // IE9-, buggy iOS6
// closures
var now = Date.now || function() { return +new Date(); }; // pre-es5 browsers
var lastTime = 0;
// polyfills
win.requestAnimationFrame = function(callback) {
var nowTime = now();
var nextTime = Math.max(lastTime + 16, nowTime);
return win.setTimeout(function() {
callback(lastTime = nextTime);
}, nextTime - nowTime);
};
win.cancelAnimationFrame = win.clearTimeout;
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment