| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A polyfill for window.requestAnimationFrame and |
| 17 |
* window.cancelAnimationFrame. |
| 18 |
* Code based on https://gist.github.com/paulirish/1579671 |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.animationFrame.polyfill'); |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* @define {boolean} If true, will install the requestAnimationFrame polyfill. |
| 26 |
*/ |
| 27 |
goog.define('goog.dom.animationFrame.polyfill.ENABLED', true); |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Installs the requestAnimationFrame (and cancelAnimationFrame) polyfill. |
| 32 |
*/ |
| 33 |
goog.dom.animationFrame.polyfill.install = function() { |
| 34 |
if (goog.dom.animationFrame.polyfill.ENABLED) { |
| 35 |
var vendors = ['ms', 'moz', 'webkit', 'o']; |
| 36 |
for (var i = 0, v; v = vendors[i] && !goog.global.requestAnimationFrame; |
| 37 |
++i) { |
| 38 |
goog.global.requestAnimationFrame = |
| 39 |
goog.global[v + 'RequestAnimationFrame']; |
| 40 |
goog.global.cancelAnimationFrame = |
| 41 |
goog.global[v + 'CancelAnimationFrame'] || |
| 42 |
goog.global[v + 'CancelRequestAnimationFrame']; |
| 43 |
} |
| 44 |
|
| 45 |
if (!goog.global.requestAnimationFrame) { |
| 46 |
var lastTime = 0; |
| 47 |
goog.global.requestAnimationFrame = function(callback) { |
| 48 |
var currTime = new Date().getTime(); |
| 49 |
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); |
| 50 |
lastTime = currTime + timeToCall; |
| 51 |
return goog.global.setTimeout(function() { |
| 52 |
callback(currTime + timeToCall); |
| 53 |
}, timeToCall); |
| 54 |
}; |
| 55 |
|
| 56 |
if (!goog.global.cancelAnimationFrame) { |
| 57 |
goog.global.cancelAnimationFrame = function(id) { clearTimeout(id); }; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
}; |