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