Visualizations
211 / 500

211 · Emergence

Slime Mold

Simple agents leave trails that self-organize into living networks.

Modeled on Physarum polycephalum. Each of tens of thousands of agents moves forward, sniffs a trail map with three sensors ahead of it, and turns toward the strongest scent while depositing more. The trail maps blur and fade every step. Three species share the dish, each following its own scent, so three colored networks overlap. No agent knows the big picture, yet together they weave vein-like networks that link up every patch of food.

Try it. Drag to lay down food that attracts the swarm. Press R to reset.

  • Agent-based simulation
  • Diffusion and decay
  • Stigmergy

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 Physarum slime mold simulation with JavaScript and the HTML canvas element. 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. Keep a trail map as a Float32Array at half resolution (one cell per 2 CSS pixels) and scale it up with drawImage.
- Create about 30,000 agents stored in typed arrays (x, y, heading), scattered at random with random headings.
- Each step, every agent samples the trail map at three sensors 9 cells ahead: straight, 22.5 degrees left and 22.5 degrees right. If straight is strongest it keeps going; if both sides beat straight it turns randomly; otherwise it turns 45 degrees toward the stronger side. Then it moves one cell forward and adds 5 to the trail where it lands. Wrap around the edges.
- After moving the agents, blur the trail map with a 3x3 average and multiply it by about 0.9 so old trails fade.
- Draw the trail map into an ImageData buffer with a glowing color ramp (black, deep blue, cyan, white).

Once that works, make it beautiful:
- Precompute sine and cosine lookup tables and avoid allocating anything in the main loop so it holds 60 fps.
- Let the mouse drag lay down food: a separate map that adds scent to the trail every step, so the network grows toward it and links the food together. Drop food at random spots every few seconds when nobody is interacting.
- Add a cheap bloom by drawing a shrunken copy of the image back on top with "lighter" blending.

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 several species that avoid each other's trails, steering agents with an image, or slowly changing sensor distance over time.
PreviousTsunamiA quake lifts the seafloor and a tsunami races across a live nautical chart. NextHaeckel PlatesAn endless folio of generative radiolarians, diatoms and medusae in lithograph ink.

Related visualizations

  • Social AmoebaeEmergence Starving amoebae relay spiral waves of cAMP and stream together into mounds.
  • Ant FarmEmergence A colony digs its nest grain by grain behind glass, with no plan but local rules.
  • Particle LifeEmergence Colored particles with simple attractions that assemble into living cells.
  • Neural EcosystemEmergence Herbivores and predators with tiny neural brains evolve in a pastel petri dish.
  • Active AstersEmergence Microtubules and motor proteins self-organize into asters, in two-color fluorescence.
  • Rock Paper LizardsEmergence Three lizard morphs chase each other in spirals until mobility tips them into extinction.
  • Turing PatternsEmergence Two chemicals react and diffuse into coral, spots and stripes.
  • Self-Replicating LoopsEmergence Langton's 1984 loops copy their own genome and tile the plane with a glass colony.
  • Cell SortingEmergence A Cellular Potts tissue unmixes itself by differential adhesion, on a stained slide.

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

← More from Emergent Mind Labs