UNPKG

toosoon-utils

Version:
44 lines (43 loc) 1.04 kB
/** * 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=60] Frame per second limit */ constructor(fps = 60) { this.fps = fps; } /** * Check if elapsed time since last update is higher than current FPS limit * * @returns {boolean} `true` if elapsed time since last update is higher than current FPS limit, `false` otherwise */ update() { this._time = Date.now(); this._elapsedTime = this._time - this._lastUpdate; if (this._elapsedTime < this._interval) { return false; } this._lastUpdate = this._time - (this._elapsedTime % this._interval); return true; } /** * Frame per second limit */ set fps(fps) { this._fps = fps; this._interval = 1000 / fps; } get fps() { return this._fps; } }