asafefe.dev
Lab

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
0gen
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-life

Or by URL: https://asafefe.dev/r/lab-game-of-life.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx12 lines
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

PropTypeDefaultDescription
refRef<GameOfLifeHandle>-step(), randomize(density?), and clear().
runningbooleanfalseAdvance on a timer.
speednumber90Time per generation in ms.
cols / rowsnumber48 / 28Board size in cells.
densitynumber0.2Share of live cells to start with.
editablebooleantrueDraw and erase cells with the pointer.
color / ghostColorstring"var(--primary)" / "var(--foreground)"Live cells, and ghosts plus grid lines.
onChange({ generation, alive }) => void-Called after every change.