toosoon-utils
Version:
Utility functions & classes
42 lines (41 loc) • 933 B
JavaScript
import { now } from '../../functions';
/**
* Utility class for controlling FPS calls
*
* @exports
* @class FrameRate
*/
export default class FrameRate {
_fps;
_interval = 0;
_time = 0;
_elapsedTime = 0;
_lastUpdate = 0;
/**
* @param {number} [fps=30] Frame per second limit
*/
constructor(fps = 30) {
this.fps = fps;
}
/**
* Return true if elapsed time since last update is higher than current FPS
*
* @returns {boolean}
*/
update() {
this._time = now();
this._elapsedTime = this._time - this._lastUpdate;
if (this._elapsedTime < this._interval) {
return false;
}
this._lastUpdate = this._time - (this._elapsedTime % this._interval);
return true;
}
get fps() {
return this._fps;
}
set fps(fps) {
this._fps = fps;
this._interval = 1000 / fps;
}
}