diff --git a/src/games/life/index.ts b/src/games/life/index.ts index 6ecfe92..dfb70ad 100644 --- a/src/games/life/index.ts +++ b/src/games/life/index.ts @@ -6,14 +6,14 @@ const height = life.getHeight(); const canvas = createCanvas(width, height); const context = canvas.getContext('2d')!; -const imageData = context.createImageData(width, height); +let imageData: ImageData; const step = life.step as CallableFunction; -let pixels: Uint8Array; export default function main() { const pixelsPtr = life.initField(); - pixels = new Uint8Array(life.memory.buffer, pixelsPtr, width * height * 4); + const pixels = new Uint8ClampedArray(life.memory.buffer, pixelsPtr, width * height * 4); + imageData = new ImageData(pixels, width, height); console.log(life, pixels.length); @@ -25,16 +25,17 @@ let count = 0; async function loop() { const start = performance.now(); + step(); + + context.putImageData(imageData, 0, 0); + context.clearRect(0, 0, 35, 15); + const end = performance.now(); sum += end - start; count++; - imageData.data.set(pixels); - - context.putImageData(imageData, 0, 0); - context.clearRect(0, 0, 35, 15); context.fillText(`${(sum / count).toFixed(1)} ms`, 2, 10); requestAnimationFrame(loop); } \ No newline at end of file