Last active
January 4, 2016 00:29
-
-
Save a60814billy/8542464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 原本的用法,可以將變數包起來,並用return 決定要開那些介面 | |
Framework.FpsAnalysis = function () { | |
if (!(this instanceof arguments.callee)) return new arguments.callee(); | |
var timeData = new Array(50); | |
var fpsData = new Array(50); | |
for (var i = 0; i < fpsData.length; i++) { | |
timeData[i] = 0; | |
fpsData[i] = 0; | |
} | |
var currentPoint = 1; | |
var fps = 0; | |
timeData[0] = (new Date()).getTime(); | |
return { | |
update: function () { | |
timeData[currentPoint] = (new Date()).getTime(); | |
fps -= fpsData[currentPoint]; | |
fpsData[currentPoint] = timeData[currentPoint] - (currentPoint == 0 ? timeData[timeData.length - 1] : timeData[currentPoint - 1]); | |
fps += fpsData[currentPoint]; | |
currentPoint = (++currentPoint) % fpsData.length; | |
}, | |
getUpdateFPS: function () { | |
return Math.floor(1000 / (fps / fpsData.length)); | |
} | |
}; | |
}; | |
//改用 Klass 的方式就不能將變數包起來決定要開那些介面, | |
Framework.FpsAnalysis = Klass({ | |
_timeData: [], | |
_fpsData: [], | |
_currentPoint: 0, | |
_fps: 0, | |
// __construct 為自訂的建構子 | |
__construct: function(){ | |
this._timeData = new Array(50); | |
this._fpsData = new Array(50); | |
for(var i=0;i<this._fpsData.length;i++){ | |
this._timeData[i] = 0; | |
this._fpsData[i] = 0; | |
} | |
this._currentPoint = 1; | |
this._fps = 0; | |
this._timeData[0] = (new Date()).getTime(); | |
}, | |
update: function(){ | |
this._timeData[this._currentPoint] = (new Date()).getTime(); | |
this._fps -= this._fpsData[this._currentPoint]; | |
this._fpsData[this._currentPoint] = this._timeData[this._currentPoint] - (this._currentPoint === 0 ? this._timeData[this._timeData.length-1] : this._timeData[this._currentPoint -1]); | |
this._fps += this._fpsData[this._currentPoint]; | |
this._currentPoint = (++this._currentPoint) % this._fpsData.length; | |
}, | |
getUpdateFPS: function(){ | |
return Math.floor(1000 / (this._fps / this._fpsData.length)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment