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.
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.