asafefe.dev
Lab

Lab/19

Boids

Every boid looks at its neighbors and balances three urges: keep some distance, match their heading, and drift toward their center. The cursor plays predator or magnet.

  • Canvas
  • Simulation
  • Interaction
Move to scatter the flock
1.5
1.0
1.0

Installation

Add Boids to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-boids

Or by URL: https://asafefe.dev/r/lab-boids.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx10 lines
import { Boids } from "@/components/lab/boids"

export function Hero() {
  return (
    <section className="relative h-[420px]">
      <Boids className="absolute inset-0" pointer="predator" />
      <h1 className="relative">Hello</h1>
    </section>
  )
}

Props

PropTypeDefaultDescription
separationnumber1.5Pull away from close neighbors.
alignmentnumber1Match neighbors' heading.
cohesionnumber1Steer toward neighbors' center.
pointer"predator" | "attractor" | "none""predator"What the cursor does to the flock.
pointerRadiusnumber120Cursor influence radius in px.
speednumber2.6Top speed in px per frame.
perceptionnumber50Neighbor radius in px.
density / maxBoidsnumber1 / 2400, 280Boids per px of area, and the cap.
color / accentColorstring"var(--foreground)", "var(--primary)"Boid color, and the color for startled boids.
accentRationumber0.1Share of boids always drawn in the accent color.
sizenumber7Boid length in px.
refRef<BoidsHandle>-reset() scatters a fresh flock.