Created
October 29, 2015 14:31
-
-
Save luigimannoni/109d2fe2bfb966cfff2e to your computer and use it in GitHub Desktop.
RequestAnimationFrame Polyfill for IE9-
This file contains hidden or 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
| // RAF Polyfill | |
| 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