Lab/11
Game of Life
Conway's four rules on a wrapping grid. Draw a seed or randomize, then watch cells live and die by their neighbor counts.
- Canvas
- Simulation
Draw cells, then press play
lab/game-of-life0gen
0alive
Installation
Add Game of Life to your project with the shadcn CLI. The files land in components/lab, ready to edit.
$ pnpm dlx shadcn@latest add @soultel/lab-game-of-lifeOr by URL: https://asafefe.dev/r/lab-game-of-life.json
Brings along
- utils
- @soultel/lab-canvas
Usage
import { useState } from "react"
import { GameOfLife } from "@/components/lab/game-of-life"
export function Example() {
const [running, setRunning] = useState(true)
return (
<div>
<GameOfLife running={running} className="rounded-md border" />
<button onClick={() => setRunning((r) => !r)}>{running ? "Pause" : "Play"}</button>
</div>
)
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| ref | Ref<GameOfLifeHandle> | - | step(), randomize(density?), and clear(). |
| running | boolean | false | Advance on a timer. |
| speed | number | 90 | Time per generation in ms. |
| cols / rows | number | 48 / 28 | Board size in cells. |
| density | number | 0.2 | Share of live cells to start with. |
| editable | boolean | true | Draw and erase cells with the pointer. |
| color / ghostColor | string | "var(--primary)" / "var(--foreground)" | Live cells, and ghosts plus grid lines. |
| onChange | ({ generation, alive }) => void | - | Called after every change. |
