UNPKG

raf-timer

Version:

An abstraction around requestionAnimationFrame.

39 lines (32 loc) 1.47 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating // requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel // MIT license var requestAnimationFrame = exports.requestAnimationFrame = window.requestAnimationFrame; var cancelAnimationFrame = exports.cancelAnimationFrame = window.cancelAnimationFrame; var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for (var x = 0; x < vendors.length && !requestAnimationFrame; ++x) { exports.requestAnimationFrame = requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; exports.cancelAnimationFrame = cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']; } if (!requestAnimationFrame) { exports.requestAnimationFrame = requestAnimationFrame = function requestAnimationFrame(callback) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; } if (!cancelAnimationFrame) { exports.cancelAnimationFrame = cancelAnimationFrame = function cancelAnimationFrame(id) { clearTimeout(id); }; }