1 /**
  2  * Copyright (C) 2009-2012 Klaus Reimer <k@ailis.de>
  3  * See LICENSE.txt for licensing information
  4  * 
  5  * @require threedee.js
  6  */
  7 
  8 /**
  9  * Constructs a new FPS counter.
 10  * 
 11  * @constructor
 12  * @class Counts frames per second.
 13  */
 14 threedee.FpsCounter = function()
 15 {
 16     // Empty
 17 };
 18 
 19 /** 
 20  * The last time a result was calculated. 
 21  * @private 
 22  * @type {number} 
 23  */
 24 threedee.FpsCounter.prototype.lastResult = 0;
 25 
 26 /** 
 27  * The last calculated FPS value. 
 28  * @private 
 29  * @type {number} 
 30  */
 31 threedee.FpsCounter.prototype.fps = 0;
 32 
 33 /** 
 34  * The current FPS counter. 
 35  * @private 
 36  * @type {number} 
 37  */
 38 threedee.FpsCounter.prototype.counter = 0;
 39 
 40 /**
 41  * This method must be called each time a frame is drawn.
 42  */
 43 threedee.FpsCounter.prototype.frame = function()
 44 {
 45     var now;
 46     
 47     now = new Date().getTime();
 48     this.counter++;
 49     if (this.lastResult == 0)
 50     {
 51         this.lastResult = now;
 52     }
 53     else if (this.lastResult + 1000 < now)
 54     {
 55         this.lastResult = now;
 56         this.fps = this.counter;
 57         this.counter = 0;
 58     }
 59 };
 60 
 61 /**
 62  * Returns the number of frames per second.
 63  * 
 64  * @return {number} The number of frames per second
 65  */
 66 threedee.FpsCounter.prototype.getFps = function()
 67 {
 68     return this.fps;
 69 };
 70