Visualizations
408 / 500

408 · Type and image

Compression Echoes

LZ77 compresses a poem while every back-reference loops back over the page in gold.

A scribe parses the poem with LZ77, the algorithm at the heart of zip, gzip and PNG: at each position it searches everything already written for the longest earlier copy of what comes next, and emits either a literal character or a back-reference saying how far to go back and how much to copy. Each reference is drawn as an arc from the earlier phrase to the new one and the copied letters light up in its color (by copy length), so refrains and rhymes turn into loops over an illuminated page, and overlapping copies like 'bells, bells, bells' copy their own output. As in DEFLATE, the literals are Huffman coded: their tree grows in the margin as a codebook, the compressed token stream scrolls along the bottom, and a live counter compares the bits against plain 8-bit text.

Try it. Click the page and type to write your own verse (Enter for a new line, Backspace to delete, Esc to start blank) and watch the scribe compress it as you go. Hover an arc or a copied phrase to see its distance, length and cost. The left and right arrow keys switch poems. Left alone, it works through four public domain poems.

  • LZ77 longest-match parsing
  • Huffman coding of literals
  • Arc diagrams over per-character text layout
  • Cached measureText layout

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an LZ77 compression visualizer that draws every back-reference as an arc over the text, using 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, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Paint it a deep blue background.
- Put a repetitive public domain poem in a string, for example the first stanza of Poe's The Bells, keeping its line breaks.
- Write the LZ77 parser: at each position, scan backwards through everything already seen for the longest match of the text that follows (let it run past the current position, so copies can overlap themselves). If the match is at least 3 characters, emit a reference (distance, length) and skip ahead; otherwise emit a literal character.
- Lay the poem out one character at a time with cached ctx.measureText widths, so you know the x and y of every character, and draw it in a serif font with generous line spacing.
- For every reference, draw a curve (bezierCurveTo) from the middle of the earlier phrase to the middle of the copied one, arching above the text, and color the copied characters to match.

Once that works, make it beautiful:
- Animate a scribe that emits one token at a time: literals ink in quickly, references draw their arc first and then light up the copied letters.
- Color arcs by copy length, give them a soft glow with globalCompositeOperation = "lighter", and add an illuminated drop cap for the first letter.
- Show the compressed token stream along the bottom and a counter comparing the bits used (say 1 flag bit plus 8 bits per literal, 1 + 12 + 6 bits per reference) with plain 8-bit text.
- Let me type my own verse into the canvas and compress it live.

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 Huffman coding the literals as DEFLATE does, lazy matching, or hovering an arc to see its distance and length.
PreviousDendritesSupercooled melt freezes into branching dendrites, with latent heat glowing between arms. NextChromaticity LabDraw any light's spectrum and watch it land on the CIE 1931 horseshoe.

Related visualizations

  • Huffman GardenAlgorithms Letter frequencies sprout into a Huffman tree drawn as a botanical plate.
  • BPE TokenizerType and image Byte pair encoding learns a vocabulary live, fusing typeset tiles merge by merge.
  • Line BreakingType and image Greedy line breaking versus Knuth-Plass, with rivers and the breakpoint graph.
  • Hidden in PixelsType and image A treasure map hides in a landscape's lowest bits. Peel the bit planes to find it.
  • TeletextRetro A working teletext decoder: mosaic graphics, carousel pages and hidden answers.
  • Glyph RasterizerType and image Letters become pixels: exact-area scanline coverage, subpixel RGB and hinting.
  • JPEG AnatomyAlgorithms A real JPEG codec runs live on a moving scene, every stage down to its 64 cosines.
  • ASCII RendererType and image A rotating 3D scene drawn in characters chosen by shape, not just brightness.
  • Hash AvalancheAlgorithms Real SHA-256 woven round by round: flip one input bit and half the state catches fire.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs