houbaoshu

Game of Life

Conway’s Game of Life with a simulation core written in Rust and compiled to WebAssembly. Drag to draw cells, or start from a classic pattern.

Game of Life

Loading WebAssembly…

Loading Gen 0 0 alive 10 gen/s

Press and drag to draw; starting on a live cell erases instead. The simulation holds still while you draw. Keyboard: arrow keys move, Space toggles a cell.

  1. 01

    A Rust core compiled to WebAssembly

    The rules live in a Rust crate that exports Universe through wasm-bindgen; wasm-pack packages it as a module the browser loads directly. The page’s JavaScript only draws and handles input. The pattern presets are in Rust too, guarded by tests: the glider moves one cell diagonally every 4 generations, the pulsar returns to its start every 3, and the glider gun repeats every 30.

  2. 02

    One bit per cell

    Cell states are packed into a FixedBitSet. On wide screens the world is 96 × 48 = 4,608 cells: 144 32-bit words in WebAssembly, so a generation takes 576 bytes. Narrow screens get 48 × 48. Each step writes the next generation into a second buffer and swaps the two, so no generation allocates memory. The edges wrap around into a torus.

  3. 03

    JavaScript reads WebAssembly memory directly

    Nothing is copied per frame. JavaScript takes the address and length of the cell buffer inside WebAssembly’s linear memory, lays a Uint32Array view over it, and reads each cell’s bit straight onto the canvas.

  4. 04

    It stops when nobody is watching

    When the canvas scrolls out of view or the tab goes to the background, the animation loop stops and picks up again on return; while paused, no timer is left running. With “reduce motion” turned on, the page does not start playing by itself.