← All demo levels

Level 2 · Ideas

Ten things that look impossible

Level 1 was effects. These are ideas — flocking birds, rope that swings, a computer solving a maze in front of you. Each is a famous idea in computing, and each is smaller than you'd think.

What changed from Level 1 Level 1 demos mostly did one thing each. These ones have a rule that runs over and over, and the interesting behaviour appears on its own.

Nobody programs the birds to make those shapes. Three simple rules do it. That idea — small rules producing big behaviour — is one of the most useful things you'll ever learn.
1

Birds that flock

What it takes to build

MediumA day3 rules~50 lines

How it works: each bird can only see neighbours within about a centimetre of screen, and follows three rules:

1. Don't crowd the ones very close. 2. Point roughly the way your neighbours point. 3. Drift toward the middle of your neighbours.

That's all. There's no leader, no plan, and nobody drew those swirls. This is called emergence — simple rules, repeated, producing behaviour nobody designed. It was invented in 1986 and it's how flocks are animated in films.

What to ask for:

Canvas boids flocking simulation with separation, alignment and cohesion. Each bird only considers neighbours within 50 pixels, has a maximum speed, and wraps around the edges. Draw each as a small triangle pointing along its direction of travel.
2

Rope you can swing

grab the rope and throw it around

What it takes to build

MediumA dayReal physics~45 lines

How it works: the rope is 18 dots. Each dot remembers where it was last frame, and that difference is its speed — you never store speed separately. Move each dot by that difference, add a little downward pull, done.

Then the clever part: walk down the rope and force every pair of dots back to the correct distance apart. Do that fourteen times per frame and a loose chain of dots behaves like a real rope — it swings, it whips, it settles.

This is Verlet integration. It's how cloth and hair are simulated in games, and it's about fifteen lines of actual maths.

What to ask for:

Verlet integration rope on canvas: points store current and previous position, gravity added each frame, then run distance constraints between neighbours 14 times per frame to stiffen it. Top point pinned. Let me grab any point with the pointer.
3

Watching a computer think

Green is the start, red is the goal

What it takes to build

MediumA dayFamous algorithm~60 lines

How it works: keep a queue of squares to look at, starting with the green one. Take the first, look at its four neighbours, add any you haven't seen to the back of the queue. Repeat.

Because you always take from the front and add to the back, you explore everything one step away before anything two steps away — so the first time you reach the goal, you've found the shortest route. That's guaranteed, not luck.

The grey spreading outward is the computer's search. You're watching it think. This is breadth-first search, and it's the same idea behind route-finding in maps.

What to ask for:

Breadth-first search pathfinding on a grid with random walls. Animate the frontier spreading outward, then highlight the shortest path back from the goal using a came-from map. Handle the case where no route exists.
4

Watching sorting happen

What it takes to build

EasyA few hoursThe classic first algorithm~35 lines

How it works: compare two bars side by side. If the left is taller, swap them. Move along one. When you reach the end, start again from the beginning. Stop when a whole pass makes no swaps.

Each pass pushes the tallest remaining bar to the right — which is why it's called bubble sort, things bubbling to the top.

Watch the comparison counter. Forty-four bars costs around a thousand comparisons. Double the bars and it roughly quadruples, not doubles. That's why bubble sort is taught and then never used — but seeing it is how you understand why faster ones were invented.

What to ask for:

Animated bubble sort on canvas with about 45 bars. Highlight the two bars being compared in red, count comparisons, and run in small batches per frame so it animates smoothly instead of finishing instantly.
5

A drawing that draws itself

What it takes to build

EasyAn hourOne CSS trick~10 lines

How it works: this is the neatest trick on the page. Any line in an SVG can be made dashed — and you can set the dash length to be the entire length of the line. One dash, one gap, each as long as the whole line.

Now shift where the dashing starts. At one extreme the gap covers everything and the line is invisible. Slide it along and the line appears to draw itself.

Nothing is being drawn. It's one very long dash sliding into view. The JavaScript here is a single line that measures the path so the numbers are exactly right.

What to ask for:

SVG self-drawing line animation using stroke-dasharray and stroke-dashoffset, with getTotalLength() in JavaScript to set the dash length exactly. Loop it so it draws, holds, then undraws.
6

Sound you can see

Press a note — sound with your eyes

What it takes to build

MediumA dayBrowser makes the sound~50 lines

How it works: your browser has a built-in synthesiser. You ask it for a tone at a frequency — 440 vibrations a second is the A that orchestras tune to — and it makes it. No sound file is downloaded, because nothing was recorded. It's generated the instant you press.

On the way to the speakers the sound passes an analyser, which hands you 2,048 numbers describing the wave right now. Draw those as a line and you're looking at the actual sound.

Press a higher note and the waves bunch closer together. That's not a metaphor — higher pitch literally is more waves per second, and you can count them.

What to ask for:

Web Audio: buttons that play sine wave notes with a short fade in and out so there's no click. Route through an AnalyserNode and draw the waveform on canvas with getByteTimeDomainData. Resume the AudioContext on first click.
7

Changing every pixel

What it takes to build

MediumA few hoursHow every filter works~40 lines

How it works: a picture is a very long list of numbers. For each pixel: how much red, how much green, how much blue, how see-through. Four numbers, repeated for every pixel. A small image here is around 200,000 numbers.

A filter is just maths on that list. Invert: replace each number with 255 minus itself. Grey: replace red, green and blue with their average. Posterise: round every number to the nearest 64, so only a few shades survive.

Every filter in every photo app is this. Instagram is arithmetic on a list of numbers — more artfully chosen arithmetic, but arithmetic.

What to ask for:

Canvas image filters using getImageData: greyscale using the 0.299/0.587/0.114 luminance weights, invert, and posterise by rounding each channel. Keep a copy of the original pixels so Reset works.
8

Ten thousand rows

What it takes to build

HarderA day or twoThe trick behind every big list~35 lines

How it works: scroll it. Ten thousand rows, perfectly smooth. Now read the counter underneath — only about nine rows actually exist.

The trick: make one tall empty box, 10,000 × 32 pixels high, so the scrollbar is the right size. Then create just enough rows to fill the window, and as you scroll, move those same rows into position and rewrite their text.

You're not scrolling a list. You're looking through a window at nine rows that keep repainting themselves. If you actually made 10,000 rows the page would crawl — this is why long lists in real apps stay fast.

What to ask for:

Virtual scrolling for 10,000 rows: a spacer div at total height, a pool of only the visible rows, repositioned with transform translateY on scroll and their content rewritten. Recalculate on resize.
9

Undo and redo

What it takes to build

MediumA dayTwo lists, that's it~40 lines

How it works: draw something, then undo it. Then redo it. It feels like a big feature. It's two lists.

Keep a past list and a future list. Before any change, push a copy of how things look onto past. To undo: move the current picture onto future, and pop the last one off past. To redo: exactly the reverse.

One rule makes it behave correctly: when you draw something new, empty the future list. You've created a new branch of history, so the old future can never be returned to. Miss that line and undo goes strange in ways that are very hard to debug.

What to ask for:

Undo/redo with two stacks: push a copy of state onto the past before each change and clear the future. Undo moves current onto future and pops past; redo does the reverse. Disable the buttons when their stack is empty.
10

Text made of dust

What it takes to build

HarderA day or twoReading your own drawing~45 lines

How it works: two steps, and the first is the surprising one.

Step one: write the text onto an invisible canvas. Then read that canvas back pixel by pixel and ask "is anything here?" Every pixel that's part of a letter becomes a dot with a home position. You drew text in order to measure it.

Step two: every frame, nudge each dot toward its home — a little more if it's far away, and always slowing down slightly. That's a spring. Scatter them and they pull themselves back into readable letters.

Nobody positioned a single dot. The letters are a consequence of where the text happened to be.

What to ask for:

Particle text on canvas: draw the word to an offscreen canvas, read getImageData every 4 pixels, and make a particle wherever alpha is high. Each particle springs toward its home position with damping. Scatter them on click and let them reassemble.

What Level 2 was really about

Every one of these has the same shape: a small rule, repeated very fast.

DemoThe rule underneath
BirdsMatch your neighbours, don't crowd them
RopeKeep neighbours the right distance apart
MazeAlways look at the nearest unexplored square
SortingIf the left one's bigger, swap
Particle textMove a bit closer to home
The thing worth taking away None of these were programmed to produce the result you see. The swirls, the whip of the rope, the letters reforming — nobody wrote those. They're what happens when a simple rule runs ten thousand times.

When something looks too complicated to build, the useful question is: what small rule, repeated, would produce this? Surprisingly often, there is one.

Things to try

  1. Break the flock. In demos.js, find the boids and set the separation strength to 0. All sixty birds collapse into one point. Turn alignment off instead and they scatter. Two numbers, two completely different worlds.
  2. Make the rope stiff. Change the constraint loop from 14 times to 2. It turns to jelly. Push it to 40 and it becomes a steel bar. That number is the material.
  3. Count the cost. Shuffle and sort a few times, and watch the comparison number. Then think about what a million items would cost. That instinct is worth more than memorising any algorithm.
  4. Prove the virtual scroll. Open your browser's Inspect tool, look at the row elements, and scroll. Watch the same nine elements change their text. Seeing that happen is the moment it clicks.
  5. Change the word. In the particle text, swap 'LEVEL 2' for your own name. Everything else keeps working — because nothing was ever about that specific word.