Skip to content

Instantly share code, notes, and snippets.

@kitak
Created September 19, 2016 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitak/ad0562119897fe5ea1b8946c35f1cd69 to your computer and use it in GitHub Desktop.
Save kitak/ad0562119897fe5ea1b8946c35f1cd69 to your computer and use it in GitHub Desktop.
FPS計測したいとき
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