The requestAnimationFrame polyfill no longer expects a Browserify environment and uses window through-out.

This commit is contained in:
Richard Davey 2019-11-20 17:10:11 +00:00
parent d0786e8dda
commit 0bfebb29ed

View file

@ -5,48 +5,31 @@
// https://gist.github.com/timhall/4078614
// https://github.com/Financial-Times/polyfill-service/tree/master/polyfills/requestAnimationFrame
// Expected to be used with Browserify
// Browserify automatically detects the use of `global` and passes the
// correct reference of `global`, `self`, and finally `window`
// Date.now
if (!(Date.now && Date.prototype.getTime)) {
Date.now = function now() {
return new Date().getTime();
};
}
// performance.now
if (!(global.performance && global.performance.now)) {
var startTime = Date.now();
if (!global.performance) {
global.performance = {};
}
global.performance.now = function () {
return Date.now() - startTime;
};
}
// requestAnimationFrame
var lastTime = Date.now();
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !global.requestAnimationFrame; ++x) {
global.requestAnimationFrame = global[vendors[x] + 'RequestAnimationFrame'];
global.cancelAnimationFrame = global[vendors[x] + 'CancelAnimationFrame'] ||
global[vendors[x] + 'CancelRequestAnimationFrame'];
var vendors = [ 'ms', 'moz', 'webkit', 'o' ];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++)
{
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!global.requestAnimationFrame) {
global.requestAnimationFrame = function (callback) {
if (typeof callback !== 'function') {
if (!window.requestAnimationFrame)
{
window.requestAnimationFrame = function (callback)
{
if (typeof callback !== 'function')
{
throw new TypeError(callback + 'is not a function');
}
var currentTime = Date.now(),
delay = 16 + lastTime - currentTime;
var currentTime = Date.now();
var delay = 16 + lastTime - currentTime;
if (delay < 0) {
if (delay < 0)
{
delay = 0;
}
@ -59,8 +42,10 @@ if (!global.requestAnimationFrame) {
};
}
if (!global.cancelAnimationFrame) {
global.cancelAnimationFrame = function(id) {
if (!window.cancelAnimationFrame)
{
window.cancelAnimationFrame = function(id)
{
clearTimeout(id);
};
}