Zum Inhalt

src/lib/timing.ts

// src/lib/timing.ts

export function formatTime(seconds: number): string {
  const m = Math.floor(seconds / 60)
    .toString()
    .padStart(2, '0');
  const s = Math.floor(seconds % 60)
    .toString()
    .padStart(2, '0');
  return `${m}:${s}`;
}

export class FpsMeter {
  private samples: number[] = [];
  private last = performance.now();

  tick(): number {
    const now = performance.now();
    const dt = now - this.last;
    this.last = now;
    if (dt > 0) {
      this.samples.push(1000 / dt);
      if (this.samples.length > 30) this.samples.shift();
    }
    return this.fps();
  }

  fps(): number {
    if (this.samples.length === 0) return 0;
    const sum = this.samples.reduce((a, b) => a + b, 0);
    return sum / this.samples.length;
  }
}