life: add fps counter

This commit is contained in:
2025-08-05 13:32:24 +02:00
parent 6724d34823
commit 3c5b472921

18
life.ts
View File

@@ -1,5 +1,9 @@
const SCALE = 4; const SCALE = 4;
const times: number[] = [];
let fps: number;
function init() { function init() {
const canvas = document.getElementById("canvas") as HTMLCanvasElement | null; const canvas = document.getElementById("canvas") as HTMLCanvasElement | null;
if (!canvas) throw new Error("unable to get canvas HTML element"); if (!canvas) throw new Error("unable to get canvas HTML element");
@@ -63,6 +67,20 @@ function update(ctx: CanvasRenderingContext2D, cells: number[], state: number[])
} }
ctx.fill(); // draw only once per frame ctx.fill(); // draw only once per frame
const now = performance.now();
while (times.length > 0 && times[0] <= now - 1000) {
times.shift();
}
times.push(now);
fps = times.length;
ctx.save();
ctx.fillStyle = "red";
ctx.font = "bold 48px sans-serif";
ctx.shadowColor = "white";
ctx.fillText("" + fps, 10, 50);
ctx.restore();
window.requestAnimationFrame(t => update(ctx, cells, state)); window.requestAnimationFrame(t => update(ctx, cells, state));
} }