Last active
June 8, 2016 02:33
-
-
Save preco21/8af61ea67f849ab91231fc6883f41781 to your computer and use it in GitHub Desktop.
A simple fps counter
This file contains hidden or 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
import getTimeMethod from './getTimeMethod'; | |
class FPSCounter { | |
constructor() { | |
this._getTime = getTimeMethod(); | |
this._fps = 0; | |
this._prevTime = 0; | |
this._frames = 0; | |
this.startTick(); | |
} | |
startTick() { | |
this._prevTime = this._getTime(); | |
} | |
tick() { | |
const currentTime = this._getTime(); | |
this._frames++; | |
if (currentTime - this._prevTime > 1000) { | |
this._fps = Math.round(this._frames * 1000 / (currentTime - this._prevTime)); | |
this._prevTime = currentTime; | |
this._frames = 0; | |
} | |
} | |
getFPS() { | |
return this._fps; | |
} | |
} | |
export { | |
FPSCounter as default, | |
}; |
This file contains hidden or 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
function getTimeMethod() { | |
return window.performance && window.performance.now | |
? () => window.performance.now() | |
: () => Date.now(); | |
} | |
export { | |
getTimeMethod as default, | |
}; |
Author
preco21
commented
May 25, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment