Last active
November 20, 2021 16:00
-
-
Save josfaber/c8434b656641a967c812bc563ac49d53 to your computer and use it in GitHub Desktop.
requestAnimationFrame rAF
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 | |
* Based on: https://gist.github.com/paulirish/1579671 | |
* Tweaked by: Bradley - https://gist.github.com/bradley | |
*/ | |
(function() { | |
var lastTime = 0, | |
vendors = ['ms', 'moz', 'webkit', 'o'], | |
x, | |
length, | |
currTime, | |
timeToCall; | |
for (x = 0, length = vendors.length; x < length && !window.requestAnimationFrame; ++x) { | |
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; | |
window.cancelAnimationFrame = | |
window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']; | |
} | |
if (!window.requestAnimationFrame) { | |
window.requestAnimationFrame = function(callback, element) { | |
currTime = new Date().getTime(); | |
timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
lastTime = currTime + timeToCall; | |
return window.setTimeout(function() { | |
callback(currTime + timeToCall); | |
}, | |
timeToCall); | |
}; | |
} | |
if (!window.cancelAnimationFrame) { | |
window.cancelAnimationFrame = function(id) { | |
clearTimeout(id); | |
}; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment