ECHO GARDEN / STUDY / LEVEL 4 OF 7
Who sings
with whom.
Two notes closer than 37 cm are linked by a strand, and every link becomes part of the melody. A link between two players' notes is a duet. Links are never sent over the network: every headset computes them from the garden with the same function, gardenLinks, so both players see the same strands. This page runs it.
The panel
drag notes · the selected note can be pulled outThe links
Every candidate pair, in the order gardenLinks considers them
| pair | distance | kind | result |
|---|
The real code
echoGardenRules.jsTwo loops make every pair once (j starts after i). The pairs are sorted, then taken greedily: a pair is kept only if neither note is already used. The final sort by id gives every headset the same order, which level 6 uses to agree on the beat.
Where the scene draws them
echoGarden.jsEach link gets a strand, kept in a map by the link's id. A link that was not there in the last frame plays both notes once: that is how you hear two notes "meet".
What you should be able to explain
- Derived, not sent. The links depend only on the notes. Both headsets have the same notes (level 7), so they compute the same links without a single extra message. Sending links too would only make it possible for them to disagree.
- Greedy pairing. Sorting by distance and taking a pair only when both notes are free gives each note at most one partner. A crowd of notes still makes a readable set of strands and a clear melody.
- Duets first. When a note could pair with its own player's note or with the other player's, the duet wins. The rule pushes the two players to compose together.
- Deterministic order.
Object.entriescan list notes in a different order on different headsets. Ties are broken by id and the result is sorted by id, so the answer never depends on that order.
Exercises: ① Put three notes close together. Which two are linked, and why is the third left alone? ② Untick "duets first" and find an arrangement where the links change. ③ Pull the selected note out of the panel: why can two notes that overlap on the screen be unlinked?