|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<title>Conway's Game of Life - AssemblyScript</title> |
| 5 | +<link rel="icon" href="https://assemblyscript.org/favicon.ico" type="image/x-icon" /> |
| 6 | +<meta name="viewport" content="user-scalable=0" /> |
| 7 | +<style> |
| 8 | +html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; } |
| 9 | +body { border-top: 2px solid #bc18d4; } |
| 10 | +h1 { padding: 18px 20px 20px; font-size: 12pt; margin: 0; } |
| 11 | +a { color: #111; text-decoration: none; } |
| 12 | +a:hover { color: #bc18d4; text-decoration: underline; } |
| 13 | +canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); background: #100707; cursor: cell; user-select: none; } |
| 14 | +#edge { position: absolute; bottom: 40px; right: 40px; color: #fff; display: none; text-shadow: 0 1px 2px #000; -ms-user-select: none; user-select: none; } |
| 15 | +</style> |
| 16 | +</head> |
| 17 | +<body> |
| 18 | +<h1> |
| 19 | + <a href="https://en.wikipedia.org/wiki/Conway's_Game_of_Life">Conway's Game of Life</a> in |
| 20 | + <a href="http://assemblyscript.org">AssemblyScript</a> |
| 21 | + ( <a href="https://github.com/AssemblyScript/examples/blob/master/game-of-life/assembly/index.ts">source</a> ) |
| 22 | +</h1> |
| 23 | +<canvas></canvas> |
| 24 | +<div id="edge">Might be blurry because MS Edge does not support 'image-rendering: crisp-edges' (yet) :-(</div> |
| 25 | +<script>"use strict"; |
| 26 | + |
| 27 | +// Configuration |
| 28 | +const RGB_ALIVE = 0xD392E6; |
| 29 | +const RGB_DEAD = 0xA61B85; |
| 30 | +const BIT_ROT = 10; |
| 31 | + |
| 32 | +// Set up the canvas with a 2D rendering context |
| 33 | +var cnv = document.getElementsByTagName("canvas")[0]; |
| 34 | +var ctx = cnv.getContext("2d"); |
| 35 | +var bcr = cnv.getBoundingClientRect(); |
| 36 | + |
| 37 | +// Compute the size of the universe (here: 2px per cell) |
| 38 | +var width = bcr.width >>> 1; |
| 39 | +var height = bcr.height >>> 1; |
| 40 | +var size = width * height; |
| 41 | +var byteSize = (size + size) << 2; // input & output (here: 4b per cell) |
| 42 | + |
| 43 | +cnv.width = width; |
| 44 | +cnv.height = height; |
| 45 | +cnv.style = ` |
| 46 | + image-rendering: optimizeSpeed; |
| 47 | + image-rendering: -moz-crisp-edges; |
| 48 | + image-rendering: -webkit-optimize-contrast; |
| 49 | + image-rendering: -o-crisp-edges; |
| 50 | + image-rendering: optimize-contrast; |
| 51 | + image-rendering: crisp-edges; |
| 52 | + image-rendering: pixelated; |
| 53 | + -ms-interpolation-mode: nearest-neighbor; |
| 54 | +`; |
| 55 | +ctx.imageSmoothingEnabled = false; |
| 56 | + |
| 57 | +// Compute the size of and instantiate the module's memory |
| 58 | +var memory = new WebAssembly.Memory({ initial: ((byteSize + 0xffff) & ~0xffff) >>> 16 }); |
| 59 | + |
| 60 | +// Fetch and instantiate the module |
| 61 | +fetch("build/optimized.wasm") |
| 62 | +.then(response => response.arrayBuffer()) |
| 63 | +.then(buffer => WebAssembly.instantiate(buffer, { |
| 64 | + env: { |
| 65 | + memory, |
| 66 | + abort: function() {} |
| 67 | + }, |
| 68 | + config: { |
| 69 | + BGR_ALIVE : rgb2bgr(RGB_ALIVE) | 1, // little endian, LSB must be set |
| 70 | + BGR_DEAD : rgb2bgr(RGB_DEAD) & ~1, // little endian, LSB must not be set |
| 71 | + BIT_ROT |
| 72 | + }, |
| 73 | + Math |
| 74 | +})) |
| 75 | +.then(module => { |
| 76 | + var exports = module.instance.exports; |
| 77 | + |
| 78 | + // Initialize the module with the universe's width and height |
| 79 | + exports.init(width, height); |
| 80 | + |
| 81 | + var mem = new Uint32Array(memory.buffer); |
| 82 | + |
| 83 | + // Update about 30 times a second |
| 84 | + (function update() { |
| 85 | + setTimeout(update, 1000 / 30); |
| 86 | + mem.copyWithin(0, size, size + size); // copy output to input |
| 87 | + exports.step(); // perform the next step |
| 88 | + })(); |
| 89 | + |
| 90 | + // Keep rendering the output at [size, 2*size] |
| 91 | + var imageData = ctx.createImageData(width, height); |
| 92 | + var argb = new Uint32Array(imageData.data.buffer); |
| 93 | + (function render() { |
| 94 | + requestAnimationFrame(render); |
| 95 | + argb.set(mem.subarray(size, size + size)); // copy output to image buffer |
| 96 | + ctx.putImageData(imageData, 0, 0); // apply image buffer |
| 97 | + })(); |
| 98 | + |
| 99 | + // When clicked or dragged, fill the current row and column with random live cells |
| 100 | + var down = false; |
| 101 | + [ [cnv, "mousedown"], |
| 102 | + [cnv, "touchstart"] |
| 103 | + ].forEach(eh => eh[0].addEventListener(eh[1], e => down = true)); |
| 104 | + [ [document, "mouseup"], |
| 105 | + [document, "touchend"] |
| 106 | + ].forEach(eh => eh[0].addEventListener(eh[1], e => down = false)); |
| 107 | + [ [cnv, "mousemove"], |
| 108 | + [cnv, "touchmove"], |
| 109 | + [cnv, "mousedown"] |
| 110 | + ].forEach(eh => eh[0].addEventListener(eh[1], e => { |
| 111 | + if (!down) return; |
| 112 | + var loc; |
| 113 | + if (e.touches) { |
| 114 | + if (e.touches.length > 1) return; |
| 115 | + loc = e.touches[0]; |
| 116 | + } else { |
| 117 | + loc = e; |
| 118 | + } |
| 119 | + var bcr = cnv.getBoundingClientRect(); |
| 120 | + exports.fill((loc.clientX - bcr.left) >>> 1, (loc.clientY - bcr.top) >>> 1, 0.5); |
| 121 | + })); |
| 122 | + |
| 123 | + // :-( |
| 124 | + if (navigator.userAgent.indexOf(" Edge/") >= 0) document.getElementById("edge").style.display = "block"; |
| 125 | +}).catch(err => { |
| 126 | + alert("Failed to load WASM: " + err.message + " (ad blocker, maybe?)"); |
| 127 | + console.log(err.stack); |
| 128 | +}); |
| 129 | + |
| 130 | +// see comment in assembly/index.ts on why this is useful |
| 131 | +function rgb2bgr(rgb) { |
| 132 | + return ((rgb >>> 16) & 0xff) | (rgb & 0xff00) | (rgb & 0xff) << 16; |
| 133 | +} |
| 134 | +</script> |
| 135 | +</body> |
| 136 | +</html> |
0 commit comments