40 lines
1005 B
TypeScript
40 lines
1005 B
TypeScript
import { createCanvas } from "@common/display/canvas";
|
|
import life from "./life.c";
|
|
|
|
const width = life.getWidth();
|
|
const height = life.getHeight();
|
|
|
|
const canvas = createCanvas(width, height);
|
|
const context = canvas.getContext('2d')!;
|
|
const imageData = context.createImageData(width, height);
|
|
|
|
const initField = life.initField as CallableFunction;
|
|
const step = life.step as CallableFunction;
|
|
const pixels = new Uint8Array(life.memory.buffer, life.getPixels(), width * height * 4);
|
|
|
|
export default function main() {
|
|
initField(Date.now());
|
|
|
|
console.log(life, pixels.length);
|
|
|
|
requestAnimationFrame(loop);
|
|
}
|
|
|
|
let sum = 0;
|
|
let count = 0;
|
|
|
|
async function loop() {
|
|
const start = performance.now();
|
|
step();
|
|
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);
|
|
} |