1
0
Fork 0

Optimize life

This commit is contained in:
Pabloader 2025-05-12 20:29:11 +00:00
parent 28bdadcbca
commit b47fbca76d
1 changed files with 8 additions and 7 deletions

View File

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