1 /** @scope window
  2   Polyfill for cross-browser backwards compatible window.requestAnimationFrame support.
  3 
  4   Supports using `window.requestAnimationFrame` on browsers prior to the following:
  5 
  6   * Chrome 10
  7   * Firefox 4.0 (Gecko 2.0)
  8   * Internet Explorer 10.0
  9   * Opera 15
 10   * Safari 6.0
 11 
 12   Modified from Erik Möller:
 13   http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
 14 */
 15 (function() {
 16   var lastTime = 0;
 17   var vendors = ['ms', 'moz', 'webkit', 'o'];
 18   for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
 19     window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
 20     window.cancelRequestAnimationFrame = window[vendors[x]+
 21       'CancelRequestAnimationFrame'];
 22   }
 23 
 24   if (!window.requestAnimationFrame) {
 25     window.requestAnimationFrame = function(callback) {
 26       var currTime = new Date().getTime();
 27       var timeToCall = Math.max(0, 16 - (currTime - lastTime));
 28       var id = window.setTimeout(function() { callback(window.performance.now()); }, timeToCall);
 29       lastTime = currTime + timeToCall;
 30       return id;
 31     };
 32   }
 33 
 34   if (!window.cancelAnimationFrame) {
 35     window.cancelAnimationFrame = function(id) { clearTimeout(id); };
 36   }
 37 }());
 38