blob: 863f561fd6288e628281c3e4b03bf649d1d2fa44 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001// Source: https://github.com/darius/requestAnimationFrame/blob/master/requestAnimationFrame.js
2// Adapted from https://gist.github.com/paulirish/1579671 which derived from
3// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
4// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
5
6// requestAnimationFrame polyfill by Erik Möller.
7// Fixes from Paul Irish, Tino Zijdel, Andrew Mao, Klemen Slavič, Darius Bacon
8
9// MIT license
10
11(function() {
12 'use strict';
13
14 if (!Date.now) {
15 /**
16 * Date.now polyfill.
17 * @return {number} the current Date
18 */
19 Date.now = function() { return new Date().getTime(); };
20 Date['now'] = Date.now;
21 }
22
23 var vendors = ['webkit', 'moz'];
24 for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
25 var vp = vendors[i];
26 window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
27 window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame'] ||
28 window[vp + 'CancelRequestAnimationFrame']);
29 window['requestAnimationFrame'] = window.requestAnimationFrame;
30 window['cancelAnimationFrame'] = window.cancelAnimationFrame;
31 }
32
33 if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || !window.requestAnimationFrame || !window.cancelAnimationFrame) {
34 var lastTime = 0;
35 /**
36 * requestAnimationFrame polyfill.
37 * @param {!Function} callback the callback function.
38 */
39 window.requestAnimationFrame = function(callback) {
40 var now = Date.now();
41 var nextTime = Math.max(lastTime + 16, now);
42 return setTimeout(function() { callback(lastTime = nextTime); },
43 nextTime - now);
44 };
45 window.cancelAnimationFrame = clearTimeout;
46 window['requestAnimationFrame'] = window.requestAnimationFrame;
47 window['cancelAnimationFrame'] = window.cancelAnimationFrame;
48 }
49
50})();