Files
playground-frontend/src/app/shared/fps-counter.ts
T
lobo 833e1ec8dc feat(algorithms): add live metrics to algorithm pages
Each algorithm page shows its numbers with app-metrics.
FpsCounter publishes a rate every 500 ms, not every frame.
Fractal 2D reset now also resets the zoom slider.
2026-10-21 14:35:15 +02:00

31 lines
746 B
TypeScript

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);
}
}