Flock Lab is a flocking simulation made with plain JavaScript and the HTML5 canvas. A flock of starlings, a school of fish or a flock of sheep moves as one, flees from a predator and steers around obstacles you place, all driven by the classic boids rules. It builds on the free flocking tutorial on spicyyoghurt.com.
This download gives you the complete project behind Flock Lab. See how all the pieces fit together, or use it as a head start on a project of your own.
What's included:
- The complete code, fully commented and split into classes (JavaScript modules, no libraries or build tools)
- Three scenes, each with its own boids, predator and obstacles: starlings and a falcon over the countryside, fish and a shark among rocks, and sheep and a sheepdog in a meadow
- A settings file for the presets, the colors and the flocking rules
- A README and a license
The simulation itself has:
- The three rules of flocking (separation, alignment and cohesion), plus steering away from the edges, the obstacles and the predator
- A preset for each scene (Starlings, Fish and Sheep), with its own flock settings
- A choice of 200, 500, 1,000 or 3,000 boids, with a spatial grid in the code that keeps even thousands of them running smoothly
- A field of view of 360°, 270° or 120°
- A predator that follows your mouse or finger, or hunts the flock on its own
- Obstacles you place and remove with a click or a tap
- A choice of edges: wrap around, or make the boids turn back
- A slider for every setting, and a button that copies your settings as code
- Buttons to pause, restart and clear the obstacles you placed
- A canvas that fills the screen and stays sharp, from phone to desktop
What you can do with it:
You can use the code in your own games and projects, including ones you sell. You don't have to credit us in your project. Just keep the short comment with our name at the top of each code file you use from this package. You can release your project anywhere, for example online or in an app store. Here's what you can't do:
- publish your project's source code, for example in a public repository, if it contains code from this package
- sell or share the code from this package on its own, for example in another source code package
- release Flock Lab itself, unchanged or with only minor changes
LICENSE.txt in the zip has the full terms.
To run the project, you need a modern browser and a local web server, such as the Live Server extension for Visual Studio Code. The README explains how. With 3,000 boids, it can run slowly on older phones.
Want to learn how flocking works? The free tutorial walks you through it step by step, from a single boid to a flock that flees from a predator: