Visualizations
199 / 500

199 · Nature

Murmuration

Thousands of starlings flock in 3D and scatter from a falcon.

Each bird follows three rules about its seven nearest neighbors, like real starlings: steer away when too close, line up with their heading, and drift toward their center. A spatial hash keeps neighbor lookups fast enough for thousands of boids. Birds are projected from 3D with perspective, so the flock thickens into dark ribbons when it folds toward you. A falcon hunts the flock, and panic spreads from bird to bird in waves.

Try it. Move the pointer to become the predator. Click to startle the flock.

  • Boids
  • Spatial hashing
  • 3D perspective projection

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a starling murmuration with JavaScript and the HTML canvas element: thousands of birds flocking in 3D against a sunset. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Paint a dusk sky gradient from deep blue to peach.
- Create about 1,500 birds in typed arrays, each with a 3D position and velocity, starting in a loose cloud.
- Give every bird the three boids rules: steer away from birds that are too close, match the average velocity of nearby birds, and drift toward their center. Keep each bird near a cruising speed and add a soft pull back toward a center point so the flock stays on screen.
- Find neighbors with a 3D grid (a spatial hash): bucket the birds by cell each frame and only compare against birds in the 27 surrounding cells.
- Project each bird with a simple perspective camera (divide x and y by depth) and draw it as a tiny dark dash, thicker and darker when it is close.

Once that works, make it beautiful:
- Use the 7 nearest neighbors instead of everyone within a radius. Real starlings do this, and it makes the flock stretch and fold like smoke.
- Add a falcon that circles and dives through the flock. Birds near it flee, and let their panic spread to their neighbors so a wave ripples through the swarm.
- Let the camera drift slowly around the flock, add a dark silhouette of reeds along the bottom, and let the pointer steer the falcon.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as flapping wings that bank in turns, a slowly darkening sky as the birds settle to roost, or a reflection of the flock in still water.
PreviousGirih Star PatternsInterlaced Islamic star patterns built live from one angle over a hidden tiling. NextSelf Healing CodeScratch, smudge and tear a printed barcode while Reed-Solomon algebra repairs it live.

Related visualizations

  • Bait BallEmergence 1,400 silver sardines mill, stream and pack into a bait ball as dolphins close in.
  • Peppered MothsNature You are the bird: selection turns moths invisible on lichen, then on sooty bark.
  • EcholocationNature A bat hunts moths in a black cave that exists only as the echoes of its calls.
  • Kelp ForestPhysics Buoyant kelp sways in an ocean surge under god rays and live caustics.
  • Spatial IndexesAlgorithms Quadtree, k-d tree, BVH and grid rebuilt every frame over thousands of moving points.
  • Crowd Flow FieldsSimulation Two armies of 3,600 units pour across a river map, steered by one Dijkstra each.
  • Ball PitPhysics Hundreds of colorful balls tumble, stack and bounce off each other.
  • Seed DispersalPhysics Samaras autorotate, dandelion seeds parachute and leaves flutter across a gusty meadow.
  • Volumetric CloudsNature Raymarched cumulus with silver linings, light shafts and drifting shadows.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs