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