Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Forked from paulirish/rAF.js
Last active December 25, 2015 14:29
Show Gist options
  • Save Gaubee/6991570 to your computer and use it in GitHub Desktop.
Save Gaubee/6991570 to your computer and use it in GitHub Desktop.
// MIT license
(function() {
"use strict"
var G = window,
lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o'],
_KEY_AnimationFrame = 'AnimationFrame',
_KEY_equest = 'equest',
_KEY_ancel = 'ancel',
_KEY_requestAnimationFrame = 'r' + _KEY_equest + _KEY_AnimationFrame,
_KEY_cancelAnimationFrame = 'c' + _KEY_ancel + _KEY_AnimationFrame,
now = Date.now || function() {
return +new Date
};
for (var x = 0; x < vendors.length && !G[_KEY_requestAnimationFrame]; ++x) {
G[_KEY_requestAnimationFrame] = G[vendors[x] + 'R' + _KEY_equest + _KEY_AnimationFrame];
G[_KEY_cancelAnimationFrame] = G[vendors[x] + 'C' + _KEY_ancel + _KEY_AnimationFrame] || G[vendors[x] + 'C' + _KEY_ancel + 'R' + _KEY_equest + _KEY_AnimationFrame];
}
if (!G[_KEY_requestAnimationFrame])
G[_KEY_requestAnimationFrame] = function(callback, element) {
var currTime = now(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
id = G.setTimeout(function() {
callback(currTime + timeToCall);
},
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!G[_KEY_cancelAnimationFrame])
G[_KEY_cancelAnimationFrame] = function(id) {
clearTimeout(id);
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment