Optimize life
This commit is contained in:
parent
28bdadcbca
commit
b47fbca76d
|
|
@ -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);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue