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 canvas = createCanvas(width, height);
const context = canvas.getContext('2d')!; const context = canvas.getContext('2d')!;
const imageData = context.createImageData(width, height); let imageData: ImageData;
const step = life.step as CallableFunction; const step = life.step as CallableFunction;
let pixels: Uint8Array;
export default function main() { export default function main() {
const pixelsPtr = life.initField(); 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); console.log(life, pixels.length);
@ -25,16 +25,17 @@ let count = 0;
async function loop() { async function loop() {
const start = performance.now(); const start = performance.now();
step(); step();
context.putImageData(imageData, 0, 0);
context.clearRect(0, 0, 35, 15);
const end = performance.now(); const end = performance.now();
sum += end - start; sum += end - start;
count++; 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); context.fillText(`${(sum / count).toFixed(1)} ms`, 2, 10);
requestAnimationFrame(loop); requestAnimationFrame(loop);
} }