assemblyscript/tests/compiler/game-of-life.html

58 lines
1.8 KiB
HTML
Raw Normal View History

<canvas id="canvas" width="640" height="480"></canvas>
<script src="../../node_modules/binaryen/index.js"></script><script>
// Fetch the .wast (just because we dont's store the .wasm in git)
fetch("game-of-life.optimized.wast").then(response => response.text()).then(text => {
// Convert it to binary format
var binary = Binaryen.parseText(text).emitBinary();
// Instantiate the module
var module = new WebAssembly.Module(binary);
var instance = new WebAssembly.Instance(module, { /* no imports */ });
// Set up the canvas with a 2D rendering context
var cnv = document.getElementById("canvas");
var ctx = cnv.getContext("2d");
var w = cnv.width,
h = cnv.height,
s = w * h, // memory required to store either input or output
S = s + s; // total memory required to store input and output
// Grow the (exported) memory if its size isn't sufficient
var memory = instance.exports.memory;
if (memory.buffer.byteLength < S)
memory.grow(Math.ceil((S - memory.buffer.byteLength) / 65536));
// Initialize with width and height
instance.exports.init(w, h);
// Fill input at [0, s-1] with random live cells
var mem = new Uint8Array(memory.buffer);
for (var y = 0; y < h; ++y)
for (var x = 0; x < w; ++x)
mem[y * w + x] = Math.random() > 0.1 ? 0 : 1;
// Update about 30 times a second
(function update() {
setTimeout(update, 33);
instance.exports.step();
mem.set(mem.subarray(s, S), 0); // copy output -> input
})();
// Keep rendering the output at [s, 2*s-1]
(function render() {
requestAnimationFrame(render);
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "#333";
for (var y = 0; y < h; ++y)
for (var x = 0; x < w; ++x)
if (mem[s + y * w + x])
ctx.fillRect(x, y, 1, 1);
})();
}).catch(err => {
throw err;
});
</script>