There is something wonderfully strange about opening a web page, dropping a few dots of “food” onto a dark canvas, and watching a tiny virtual worm wriggle toward them.

What makes worm-sim more interesting than a normal animation is what sits underneath it: a simplified computational model inspired by the nervous system of Caenorhabditis elegans, one of neuroscience’s favorite tiny animals.

C. elegans is only about a millimeter long, yet its nervous system has been mapped at an extraordinary level of detail. The adult hermaphrodite is classically described as having 302 neurons, and decades of electron-microscopy work have produced detailed wiring diagrams of how those neurons connect. More recent work has expanded and refined whole-animal connectomes for both sexes.

That makes it tempting to ask a slightly dangerous question:

If we know the wiring diagram, can we just put it into JavaScript and get a worm?

Not quite.

But worm-sim shows why trying is interesting anyway.

The Connectome Is the Starting Point

At the heart of the project is a large JavaScript object called weights.

Conceptually, it looks like this:

var weights = {
    ADAL: {
        ADAR: 2,
        ADFL: 1,
        AIBL: 1,
        AVBR: 7,
        RIML: 3,
        // ...
    },
    // ...
};

Each key represents a neuron or muscle-related node, and the values describe weighted connections to other nodes.

The simulator turns this data into a network at startup. Instead of manually declaring every neuron, BRAIN.setup() walks through the weight table, discovers the available nodes, and initializes their state dynamically.

The basic idea is surprisingly compact:

sensory input → neural activity → weighted connections → motor activation → body physics

That one pipeline is the entire game.

The implementation is derived from earlier work around the GoPiGo Connectome, with the connectome later ported to JavaScript before being incorporated into worm-sim.

But the word simulation needs some care here. The project is using biological connectivity as computational structure; it is not recreating every electrical, chemical, molecular, and biomechanical property of a real worm.

That distinction becomes much clearer when you read the code.

A Neuron Here Is More Like a Tiny Accumulator

The virtual neurons in connectome.js are deliberately simple.

Each node maintains activity values, while incoming connections add their weights to the receiving neuron. The simulator uses a firing threshold of:

BRAIN.fireThreshold = 30;

When a non-muscle neuron exceeds that threshold, it can propagate activity through its outgoing connections.

After each neural update, residual activity is reduced:

BRAIN.postSynaptic[ps][BRAIN.nextState] *= 0.85;

So activity does not remain forever. It decays over time.

This gives the system something resembling a very lightweight discrete neural dynamical network:

  1. stimulate some neurons,

  2. accumulate weighted input,

  3. fire neurons that cross a threshold,

  4. propagate their connections,

  5. read muscle activity,

  6. decay what remains,

  7. repeat.

There are no detailed membrane potentials, ion channels, neurotransmitter kinetics, stochastic vesicle release, or biologically realistic spike dynamics here.

And that's fine.

The interesting part is seeing how much behavior you can get before adding all of that complexity.

Giving the Worm Something to Sense

A nervous system floating in isolation is not particularly exciting. It needs a world.

The browser becomes that world.

The current implementation provides several forms of artificial sensory stimulation.

Hunger

Several neurons are continuously stimulated while the simulation runs:

RIML
RIMR
RICL
RICR

This creates a persistent internal drive in the network.

It is better to think of this as a computational hunger signal rather than a detailed model of biological hunger. The simulator is injecting activity into selected neurons to keep behavior going.

Touch

When the worm's head reaches the edge of the browser window, a collision timer activates a collection of sensory neurons including members of FLP, ASH, IL1 and OLQ.

The screen boundary therefore becomes a kind of giant invisible wall.

Touch the wall, sensory activity enters the connectome, and the resulting neural state can affect movement.

Food and Chemotaxis

Clicking the canvas places food.

The simulation calculates a simple smell-like quantity based on the worm's distance from every food source:

currentSmell += 200 / (dist + 1);

What is particularly neat is that the code does not only ask, “How strong is the smell?”

It asks:

Is the smell getting stronger or weaker?

BRAIN.foodGradient = currentSmell - lastSmell;

A positive or negative change then stimulates ASEL or ASER.

That choice has some biological inspiration. The ASE neurons are an important bilateral chemosensory pair in C. elegans, and their left/right members have distinct sensory properties involved in chemical navigation.

Again, this is an abstraction rather than a complete chemotaxis model, but it creates an elegant feedback loop:

move → sample environment → detect change → alter neural state → move again

And that is where the simulation starts feeling less like animation and more like an embodied system.

The Connectome Alone Does Not Make the Worm Crawl

This is probably the most important thing I found while reading the project.

It would be easy to look at the moving worm and assume that its characteristic wriggling simply emerges from the connectome.

The current code is more complicated than that.

The simulator explicitly adds a traveling oscillation across the body. Segments from head toward tail receive alternating dorsal and ventral stimulation based on a sine wave:

if (Math.sin(phase) > 0) {
    // stimulate dorsal muscles
} else {
    // stimulate ventral muscles
}

This produces a traveling bend pattern — essentially a simplified central pattern generator-like mechanism.

The code also adds an independent wandering signal composed of two sine waves, clamps that signal, and applies it mostly near the head.

So locomotion is not simply:

connectome in → worm behavior out.

It is closer to:

connectome + sensory feedback + proprioceptive feedback + procedural oscillation + steering + body physics → worm behavior.

That sounds less magical, but scientifically it is actually more interesting.

Because real locomotion is also a closed-loop neuromechanical problem.

Experimental work in C. elegans has shown that proprioceptive coupling between body regions and motor neurons plays an important role in propagating undulatory movement from head to tail. Computational work has likewise explored how feedback between nervous-system dynamics and body mechanics can sustain locomotion.

The exact biological origin and organization of rhythmic locomotion should not be reduced to the simulator's single sinusoidal mechanism. The research literature contains considerably more nuance around proprioception, neural dynamics, and rhythm generation.

In other words, the project's CPG is a useful engineering solution, not a claim that a JavaScript sine wave has solved worm neuroscience.

Turning Neural Activity Into a Body

The virtual body contains 35 physics nodes linked together.

Neural motor output is condensed into 17 segment activation groups, each tracking:

Signal

Role in the simulator

dorsal

contributes to bending one way

ventral

contributes to bending the opposite way

left / right

collects lateral muscle activity

turn

provides a bounded steering signal

For each body region, dorsal and ventral activity are compared to calculate a desired bend.

The simulator then applies forces to neighboring nodes so that the chain physically tries to assume that curvature.

But bending alone does not create convincing forward locomotion.

Imagine putting a snake-shaped rope on frictionless ice and oscillating it. It might wiggle beautifully while going almost nowhere.

worm-sim solves this with anisotropic friction.

Velocity parallel to the body's local direction is damped only slightly, while sideways movement is damped much more aggressively.

That asymmetry lets the traveling body wave translate into forward motion.

There are also spring-like constraints keeping neighboring nodes together, stiffness resisting excessive curvature, and self-repulsion preventing distant parts of the body from collapsing into each other.

This is a lovely example of something easy to miss when thinking about brains:

Behavior doesn't come from the nervous system alone.

It emerges from the nervous system acting through a body inside an environment.

There Is Even a Primitive Body-to-Brain Loop

The project does something else I especially like.

After calculating the physical worm's current bends, it feeds some of that information back into the neural system.

If a body segment bends far enough in one direction, corresponding dorsal or ventral muscle-related nodes receive additional activity.

So the causal direction is no longer just:

brain
  ↓
muscles
  ↓
body

It begins to resemble:

brain
  ↓
muscles
  ↓
body
  ↓
proprioceptive feedback
  └────────────→ brain

That feedback matters conceptually.

A nervous system cannot properly control an animal if it never learns what its own body just did.

Real C. elegans locomotion research has found proprioceptive feedback to be an important part of coordinating bending waves along the body.

Even in simplified form, that makes the simulator a much more interesting playground than a purely feed-forward neural visualization.

So Is This a Digital C. elegans Brain?

No — at least not in the strong biological sense that phrase might suggest.

A connectome tells us who is connected to whom. It does not automatically tell us the complete rules governing those connections.

Two nervous systems could theoretically share the same wiring graph while behaving differently because of different neurotransmitters, receptor expression, connection strengths, neuromodulators, cellular dynamics, plasticity, sensory states, developmental history, or body mechanics.

That is one of the beautiful frustrations of connectomics:

knowing the wiring diagram is not the same as knowing the algorithm.

The current worm-sim implementation also contains several explicitly engineered components that make the animal move convincingly: the traveling oscillation, a procedural wander signal, physics constants, forward drive, collision handling, synthetic smell gradients, and simplified neural threshold dynamics.

So calling it an exact reconstruction of a worm brain would oversell it.

Calling it a connectome-inspired embodied neural simulation, however, feels much closer to what the code actually does.

And frankly, that is still pretty cool.

This Is Where the Project Becomes an Experiment Platform

Because the system is small and understandable, it is unusually easy to modify.

That opens up more interesting questions than simply watching the worm crawl.

One could remove individual neurons and compare the resulting trajectories. Change connection weights and measure whether food-finding improves or collapses. Disable proprioceptive feedback. Remove the explicit oscillation and investigate whether another controller can recover locomotion. Record neural activity over thousands of trials. Introduce different environments. Or optimize selected parameters through evolutionary algorithms rather than tuning them manually.

A particularly interesting experiment would be to separate the current system into three layers:

CONNECTOME
neural connectivity and activity

↓

CONTROLLER
learned or evolved neural dynamics

↓

BODY + WORLD
physics, food, touch and sensory feedback

Then instead of programming every locomotion rule directly, an optimization process could modify a constrained set of parameters and receive rewards for useful behavior — reaching food, avoiding boundaries, conserving movement, or adapting after simulated neural damage.

At that point the question changes from:

“Can we animate a connectome?”

to:

“How much useful behavior can emerge from this structure if some of its dynamics are allowed to adapt?”

That is a much more interesting experiment.

Small Enough to Understand, Open Enough to Break

Another nice property of worm-sim is that it is not buried beneath a giant framework.

It is mostly straightforward JavaScript, Canvas rendering, neural connectivity data, and custom physics. You can trace the path from sensory input to neuron activity to muscle activation to visible motion without needing a cluster of GPUs.

The repository is also released under the MIT License.

That means the software can generally be used, copied, modified, merged, published, distributed, sublicensed, and incorporated into other work, provided the copyright and license notice requirements are respected. The software is provided without warranty.

For an experimental project, that matters.

You can fork it, break it, replace its neural model, connect it to reinforcement learning, build experiments around lesion studies, log every simulated neuron, or turn the whole thing into something the original project never intended.

That may actually be the most valuable thing about this little worm.

It sits at a fascinating boundary between neuroscience and software engineering, where a biological wiring diagram becomes executable data, that data becomes activity, activity becomes muscle commands, muscle commands become physics, and physics feeds information back into the network.

It is nowhere near a complete digital organism.

But it is complex enough to ask real questions — and simple enough that you can still open the source code and see where the answers are coming from.