import {signal} from '@angular/core'; /** Counts ticks and publishes a rate a few times per second, so the display never costs frame time. */ export class FpsCounter { readonly fps = signal(0); private ticks = 0; private windowStart = 0; constructor(private readonly intervalMs = 500) {} tick(now: number = performance.now()): void { if (this.windowStart === 0) { this.windowStart = now; return; } this.ticks++; const elapsed = now - this.windowStart; if (elapsed >= this.intervalMs) { this.fps.set(Math.round(this.ticks * 1000 / elapsed)); this.ticks = 0; this.windowStart = now; } } reset(): void { this.ticks = 0; this.windowStart = 0; this.fps.set(0); } }