Created
September 19, 2016 05:11
-
-
Save kitak/ad0562119897fe5ea1b8946c35f1cd69 to your computer and use it in GitHub Desktop.
FPS計測したいとき
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
class FpsCalculator { | |
constructor() { | |
this._isRunning = false; | |
this._beginTime = Date.now(); | |
this._prevTime = this._beginTime; | |
this._frames = 0; | |
} | |
start() { | |
if (this._isRunning) { | |
return null; | |
} | |
this._beginTime = Date.now(); | |
this._prevTime = this._beginTime; | |
this._frames = 0; | |
this._isRunning = true; | |
const loop = () => { | |
if (!this._isRunning) { | |
return null; | |
} | |
this._update(); | |
requestAnimationFrame(loop); | |
} | |
loop(); | |
} | |
stop() { | |
this._isRunning = false; | |
this._frames = 0; | |
} | |
_update() { | |
this._frames++; | |
let prevTime = this._prevTime; | |
let time = Date.now(); | |
if (time > prevTime + 1000) { | |
console.log((this._frames * 1000) / (time - prevTime)); | |
this._prevTime = time; | |
this._frames = 0; | |
} | |
this._beginTime = time; | |
} | |
} | |
const calculator = new FpsCalculator(); | |
calculator.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment