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