Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft. flo-bit.dev/shapecraft
Something went wrong. Try again.
828 B · 74 lines
JavaScript
at commit da69385e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475class Clock {
constructor( autoStart = true ) {
this.autoStart = autoStart;
this.startTime = 0; this.oldTime = 0; this.elapsedTime = 0;
this.running = false;
}
start() {
this.startTime = now();
this.oldTime = this.startTime; this.elapsedTime = 0; this.running = true;
}
stop() {
this.getElapsedTime(); this.running = false; this.autoStart = false;
}
getElapsedTime() {
this.getDelta(); return this.elapsedTime;
}
getDelta() {
let diff = 0;
if ( this.autoStart && ! this.running ) {
this.start(); return 0;
}
if ( this.running ) {
const newTime = now();
diff = ( newTime - this.oldTime ) / 1000; this.oldTime = newTime;
this.elapsedTime += diff;
}
return diff;
}
}
function now() {
return performance.now();
}
export { Clock };