polyfills.requestAnimationFrame.raw.js Maven / Gradle / Ivy
The newest version!
// requestAnimationFrame
(function (global) {
var rafPrefix;
if ('mozRequestAnimationFrame' in global) {
rafPrefix = 'moz';
} else if ('webkitRequestAnimationFrame' in global) {
rafPrefix = 'webkit';
}
if (rafPrefix) {
global.requestAnimationFrame = function (callback) {
return global[rafPrefix + 'RequestAnimationFrame'](function () {
callback(performance.now());
});
};
global.cancelAnimationFrame = global[rafPrefix + 'CancelAnimationFrame'];
} else {
var lastTime = Date.now();
global.requestAnimationFrame = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var
currentTime = Date.now(),
delay = 16 + lastTime - currentTime;
if (delay < 0) {
delay = 0;
}
lastTime = currentTime;
return setTimeout(function () {
lastTime = Date.now();
callback(performance.now());
}, delay);
};
global.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}
}(this));