Kinetic UI
Back to Introduction
Background Effects

Boids Flocking Simulation

A high-performance HTML5 Canvas implementation of Craig Reynolds' Boids algorithm, creating an organic flocking simulation that reacts to your mouse.

Quick Actions
CLI Installation
npx kinetic-ui-cli@latest add boids-canvas
Boids Simulation

FLOCKING

Move your cursor to scatter the swarm.

Installation

npx kinetic-ui-cli@latest add boids-canvas

Usage

import { BoidsCanvas } from "@/components/creative/boids-canvas";

export default function Page() {
  return (
    <div className="w-full h-screen relative">
      <BoidsCanvas count={200} color="#3b82f6" />
      <div className="absolute inset-0 flex items-center justify-center pointer-events-none">
        <h1 className="text-white text-6xl font-bold">Hero Text</h1>
      </div>
    </div>
  );
}

How it works

This component does not use WebGL or Three.js. Instead, it relies on an incredibly optimized 2D Canvas requestAnimationFrame loop.

The behavior of the "boids" (bird-oid objects) is governed by three simple mathematical rules coined by Craig Reynolds in 1986:

  1. Separation: Steer to avoid crowding local flockmates.
  2. Alignment: Steer towards the average heading of local flockmates.
  3. Cohesion: Steer to move towards the average position (center of mass) of local flockmates.

Additionally, a fourth rule is injected into the velocity vector when the user's cursor comes within a certain radius, forcing the boids to rapidly scatter away from the mouse.

Usage Guide

1. Import the Component

Import the component into your React or Next.js page:

import { BoidsCanvas } from "@/components/creative/boids-canvas";

2. Implementation Example

Render the component and customize its behavior by passing props:

import { BoidsCanvas } from "@/components/creative/boids-canvas";

export default function Example() {
  return (
    <div className="w-full flex items-center justify-center p-8">
      <BoidsCanvas
        count{10}
        color="example"
      />
    </div>
  );
}
Performance Notes
  • Uses CSS hardware acceleration via GPU transform-binds.
  • Damped interpolation minimizes paint-redraw flashes.
  • Tree-shakable import: only compiles elements requested.
Accessibility Notes
  • Full keyboard focus-ring navigation support (`Tab`/`Shift+Tab`).
  • ARIA roles mapped correctly for assistive screen readers.
  • Respects `prefers-reduced-motion` system toggles.