A millipede and a centipede ripple across a sketchbook page on waves of legs.
Every leg has its own phase oscillator, coupled to its neighbors in a chain like the pattern generators in each body segment, so the legs settle into a metachronal wave with a fixed lag between neighbors. The millipede uses a direct wave that runs from tail to head with a long stance for pushing power, and the centipede a retrograde wave that runs from head to tail with a short stance for speed; planted legs are inked dark and swinging legs in red pencil, so you can watch the waves travel. A planted foot stays put on the ground while the body slides past, a swinging foot reaches for a foothold half a stride ahead of where its hip will be, and two-bone inverse kinematics bends every knee. The body follows its head by arc length along a smoothed ride height over the terrain, so it arches over anything in its way, and the gait diagrams on the right log stance and swing for the first sixteen legs.
Try it. Drag on the ground to sketch rocks and hills for the animals to climb, or click to drop a pebble. Change the wave speed and leg count with the buttons or the arrow keys: a new leg count scrambles every phase, and you can watch the coupling pull the legs back into step. C clears the ground.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a walking millipede with JavaScript and the HTML canvas element, where waves of legs come from coupled oscillators. 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 full-window canvas that stays sharp on high-DPI screens, painted warm cream.
- Draw a gently bumpy ground line from a sum of a few sine waves, and store its height for every x.
- Give the millipede 40 legs, each with a phase angle. Every frame, advance each phase with a base frequency plus a coupling term toward its neighbors: dθ/dt = ω + K sin(θ[j-1] - θ[j] - lag) + K sin(θ[j+1] - θ[j] + lag). Start the phases random and watch them lock into a wave.
- A leg is in stance for the first 70% of its cycle and in swing for the rest. A stance foot stays fixed on the ground while the body moves; a swing foot travels in an arc to a point half a stride ahead of its hip.
- Move the body at stride divided by stance time, so planted feet don't slide. Place body segments behind the head along the ground, a fixed distance apart, and attach one hip under each segment.
- Draw each leg as two lines (thigh and shin) using two-bone inverse kinematics to find the knee.
Once that works, make it beautiful:
- Give the page a sketchbook look: paper texture with faint specks, pencil lines with a little random jitter that is re-rolled a few times a second, and a translucent brown wash on the body with ink segment rings.
- Color swinging legs differently from planted ones so the wave is easy to see.
- Let the mouse draw rocks on the ground that the body climbs over by riding a smoothed version of the terrain.
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 a centipede with a backward-running wave, a gait diagram that logs stance and swing for each leg, or turning walking speed into a slider.