Path: blob/trunk/third_party/closure/goog/debug/fpsdisplay.js
2868 views
// Copyright 2011 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 Displays frames per second (FPS) for the current window.16* Only supported in browsers that support requestAnimationFrame.17* See: https://developer.mozilla.org/en/DOM/window.requestAnimationFrame.18*19* @see ../demos/fpsdisplay.html20*/2122goog.provide('goog.debug.FpsDisplay');2324goog.require('goog.asserts');25goog.require('goog.async.AnimationDelay');26goog.require('goog.dom');27goog.require('goog.dom.TagName');28goog.require('goog.ui.Component');29303132/**33* Displays frames per seconds that the window this component is34* rendered in is animating at.35*36* @param {goog.dom.DomHelper=} opt_domHelper An optional dom helper.37* @constructor38* @extends {goog.ui.Component}39* @final40*/41goog.debug.FpsDisplay = function(opt_domHelper) {42goog.debug.FpsDisplay.base(this, 'constructor', opt_domHelper);43};44goog.inherits(goog.debug.FpsDisplay, goog.ui.Component);454647/**48* CSS class for the FPS display.49*/50goog.debug.FpsDisplay.CSS = goog.getCssName('goog-fps-display');515253/**54* The number of samples per FPS report.55*/56goog.debug.FpsDisplay.SAMPLES = 10;575859/**60* The current animation.61* @type {goog.debug.FpsDisplay.FpsAnimation_}62* @private63*/64goog.debug.FpsDisplay.prototype.animation_ = null;656667/** @override */68goog.debug.FpsDisplay.prototype.createDom = function() {69this.setElementInternal(70this.getDomHelper().createDom(71goog.dom.TagName.DIV, goog.debug.FpsDisplay.CSS));72};737475/** @override */76goog.debug.FpsDisplay.prototype.enterDocument = function() {77goog.debug.FpsDisplay.base(this, 'enterDocument');78this.animation_ = new goog.debug.FpsDisplay.FpsAnimation_(this.getElement());79this.delay_ = new goog.async.AnimationDelay(80this.handleDelay_, this.getDomHelper().getWindow(), this);81this.delay_.start();82};838485/**86* @param {number} now The current time.87* @private88*/89goog.debug.FpsDisplay.prototype.handleDelay_ = function(now) {90if (this.isInDocument()) {91this.animation_.onAnimationFrame(now);92this.delay_.start();93}94};959697/** @override */98goog.debug.FpsDisplay.prototype.exitDocument = function() {99goog.debug.FpsDisplay.base(this, 'exitDocument');100this.animation_ = null;101goog.dispose(this.delay_);102};103104105/**106* @return {number} The average frames per second.107*/108goog.debug.FpsDisplay.prototype.getFps = function() {109goog.asserts.assert(110this.isInDocument(), 'Render the FPS display before querying FPS');111return this.animation_.lastFps_;112};113114115116/**117* @param {Element} elem An element to hold the FPS count.118* @constructor119* @private120*/121goog.debug.FpsDisplay.FpsAnimation_ = function(elem) {122/**123* An element to hold the current FPS rate.124* @type {Element}125* @private126*/127this.element_ = elem;128129/**130* The number of frames observed so far.131* @type {number}132* @private133*/134this.frameNumber_ = 0;135};136137138/**139* The last time which we reported FPS at.140* @type {number}141* @private142*/143goog.debug.FpsDisplay.FpsAnimation_.prototype.lastTime_ = 0;144145146/**147* The last average FPS.148* @type {number}149* @private150*/151goog.debug.FpsDisplay.FpsAnimation_.prototype.lastFps_ = -1;152153154/**155* @param {number} now The current time.156*/157goog.debug.FpsDisplay.FpsAnimation_.prototype.onAnimationFrame = function(now) {158var SAMPLES = goog.debug.FpsDisplay.SAMPLES;159if (this.frameNumber_ % SAMPLES == 0) {160this.lastFps_ = Math.round((1000 * SAMPLES) / (now - this.lastTime_));161goog.dom.setTextContent(this.element_, this.lastFps_);162this.lastTime_ = now;163}164this.frameNumber_++;165};166167168