-
-
Save artzub/5348070 to your computer and use it in GitHub Desktop.
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
/** | |
* requestAnimationFrame polyfill by Erik Möller & Paul Irish et. al. | |
* https://gist.github.com/1866474 | |
* | |
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | |
* http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating | |
**/ | |
/*jshint asi: false, browser: true, curly: true, eqeqeq: true, forin: false, newcap: true, noempty: true, strict: true, undef: true */ | |
(function( window ) { | |
'use strict'; | |
var lastTime = 0; | |
var prefixes = 'webkit moz ms o'.split(' '); | |
// get unprefixed rAF and cAF, if present | |
var requestAnimationFrame = window.requestAnimationFrame; | |
var cancelAnimationFrame = window.cancelAnimationFrame; | |
// loop through vendor prefixes and get prefixed rAF and cAF | |
var prefix; | |
for( var i = 0; i < prefixes.length; i++ ) { | |
if ( requestAnimationFrame && cancelAnimationFrame ) { | |
break; | |
} | |
prefix = prefixes[i]; | |
requestAnimationFrame = requestAnimationFrame || window[ prefix + 'RequestAnimationFrame' ]; | |
cancelAnimationFrame = cancelAnimationFrame || window[ prefix + 'CancelAnimationFrame' ] || | |
window[ prefix + 'CancelRequestAnimationFrame' ]; | |
} | |
// fallback to setTimeout and clearTimeout if either request/cancel is not supported | |
if ( !requestAnimationFrame || !cancelAnimationFrame ) { | |
requestAnimationFrame = function( callback, element ) { | |
var currTime = new Date().getTime(); | |
var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); | |
var id = window.setTimeout( function() { | |
callback( currTime + timeToCall ); | |
}, timeToCall ); | |
lastTime = currTime + timeToCall; | |
return id; | |
}; | |
cancelAnimationFrame = function( id ) { | |
window.clearTimeout( id ); | |
}; | |
} | |
// put in global namespace | |
window.requestAnimationFrame = requestAnimationFrame; | |
window.cancelAnimationFrame = cancelAnimationFrame; | |
})( window ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment