@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
58 lines • 1.6 kB
JavaScript
const getCurrentTime = (time) => (typeof time === 'number' && !isNaN(time) ? time : performance.now() / 1000);
export class Chronometer {
#timeStart;
#currentTime;
#deltaTime;
#lostTime;
#recentlyLostTime;
#isRunning;
get time() {
return this.#currentTime - this.#timeStart - this.#lostTime - this.#recentlyLostTime;
}
get deltaTime() {
return this.#deltaTime;
}
get timeStart() {
return this.#timeStart;
}
get lostTime() {
return this.#lostTime + this.#recentlyLostTime;
}
get isRunning() {
return this.#isRunning;
}
constructor(time) {
const curTime = getCurrentTime(time);
this.#timeStart = curTime;
this.#currentTime = curTime;
this.#deltaTime = 0;
this.#lostTime = 0;
this.#recentlyLostTime = 0;
this.#isRunning = true;
}
update(time) {
const previousTime = this.#currentTime;
this.#currentTime = getCurrentTime(time);
const deltaTime = this.#currentTime - previousTime;
if (this.#isRunning) {
this.#deltaTime = deltaTime;
}
else {
this.#recentlyLostTime += deltaTime;
}
}
stop() {
if (this.#isRunning) {
this.#isRunning = false;
this.#recentlyLostTime = 0;
}
}
start() {
if (!this.#isRunning) {
this.#isRunning = true;
this.#lostTime += this.#recentlyLostTime;
this.#recentlyLostTime = 0;
}
}
}
//# sourceMappingURL=Chronometer.js.map