Fourier Epicycles
Chains of rotating circles from a real discrete Fourier transform redraw a hummingbird, a treble clef, the word "Fourier" and more, sharpening from 5 to 200 terms with a live spectrum. Draw your own shape to decompose it.
Made in Claude Code (Claude subscription) with claude-opus-5.5. The agent wrote code, looked at the preview and fixed it over several iterations. This is not a single-shot result: comparisons on this site show models in one attempt.
Starting prompt
Build "Fourier Epicycles": a chain of rotating circles, computed with a real discrete Fourier transform, draws a series of line drawings with its pen tip on an elegant dark blueprint. It should look like a polished piece of mathematical illustration and be beautiful from the first frame. Single self-contained HTML file using Canvas 2D; Google Fonts are fine; no libraries, images or other external files, everything procedural.
The mathematics:
- Each drawing is a path of points defined in code (control points through a Catmull-Rom spline, with some points marked as sharp corners), resampled to N = 512 points evenly spaced along its length. An open stroke is traced out and back, so that it becomes a closed loop.
- Centre the points on their mean, take the DFT c_k = (1/N) Σ z_n e^(−2πikn/N) for k = −256…255 and sort the terms by amplitude. Each term is a circle of radius |c_k| that turns k times per revolution; chained largest first from a fixed centre, the end of the chain is the pen.
- The K largest terms give the drawing at K terms. K may be fractional (the next term fades in by its fraction), so the drawing sharpens smoothly from a blob to the exact shape.
- Make every closed drawing run the same way round, and before each morph shift the new drawing's starting point (multiply c_k by e^(ikτ), with τ chosen to best match the low frequencies of the previous drawing), so that morphs stay calm.
The drawings, in this order, each fitted to the frame:
1. A hummingbird silhouette: a long thin bill, a round head, a raised wing with five pointed feather tips and a forked tail.
2. A treble clef with its top loop, the spiral around the G line and a tail that curls into a small ball (an open stroke).
3. A classic parametric heart.
4. The word "Fourier" in a slanted single-stroke script: a capital F with a lead-in curl, a wavy cap and a crossbar, then a cursive o-u-r-i-e-r with notched r's, closed by an underline swash back to the start.
5. A heptagram {7/3} star polygon with sharp points.
6. A portrait profile like a cameo: forehead, nose, lips and chin, a hair bun and a neck cut as a bust.
7. A sitting cat with pointed ears and a curling tail.
The performance, about 17 seconds per drawing, looping forever:
1. Morph (1.7 s): the coefficients blend from the previous drawing at 5 terms to the new one at 5 terms.
2. 5 terms (2.6 s): the broad gesture.
3. Grow to 20 terms on a logarithmic ease (1.1 s), then hold (3 s): the form emerges.
4. Grow to 200 terms (1.7 s), then hold (5.6 s): every detail, exactly.
5. Drop back to 5 terms (1.5 s), then morph into the next drawing.
The pen makes one revolution every 4.8 s throughout. Open the page late in the first drawing's 5-term stage, so it is moving and sharpening within the first second.
The look:
- A deep navy blueprint with a soft radial glow behind the figure, fine gridlines aligned to the figure's centre (minor every ~22 px, major every fifth line), slightly brighter axes with draughtsman's ticks and tiny "Re" and "Im" labels, a vignette and faint film grain. Prerender all of this to an offscreen canvas.
- Thin 1 px circles coloured by rank: the 5 largest warm gold, ranks 6–20 pale cyan, the rest blue and fainter. Radius arms run from centre to centre, with small dots at the first joints, tiny "k=+1"-style labels above the three largest circles and a small crosshair at the fixed centre.
- The trail is ink of light. Evaluate the curve for the current terms over the last ~97% of a revolution behind the pen and draw it in short chunks, in three additive passes (a wide soft amber glow, a warm middle and a thin near-white core), bright at the pen and fading towards the tail. A glowing white-gold sprite marks the pen tip.
- The exact target drawing shows as a faint dotted blueprint line, with registration corners around it.
The spectrum: a small panel with bars of |c_k| for k = −36…+36 (−26…+26 on a phone), square-root scaled. Excluded terms are dim; included terms light up in the same gold, cyan and blue rank colours as the circles as K grows, in sync with them. A small legend sits underneath.
Interface:
- Top left: the title, a small uppercase subtitle and the formula z(t) = Σ c_k e^(ikt).
- Bottom left: the drawing's number and name, a large live count of terms, a short italic note on the current stage, and a logarithmic scale 1–5–20–50–200 with a filling bar.
- A small hint: "Draw a closed shape to decompose it". With a mouse or a finger the visitor draws a shape; on release it is closed, resampled, transformed and morphed in from the current picture, then rebuilt from 5 to 200 terms like the others before the sequence continues.
- Everything starts automatically and is visibly moving from the first frame, without any interaction. The layout works on a phone in portrait: the figure sits between the title and the caption, and the spectrum spans the bottom. The figure never hides under the panels.
Engineering rules (the page must work on the first load, with no fixes):
- Use the plain Canvas 2D API and only methods that exist on it. The page runs in a sandboxed iframe without same-origin access: do not use localStorage, sessionStorage, cookies, alert, prompt or window.top.
- Compute each drawing's DFT once, never per frame. Handle devicePixelRatio (capped at 2) and window resizing.
- Declare each identifier once. Guard every animated value against NaN.
- Answer with the complete file exactly once, in a single ```html code block: no drafts, partial snippets or notes.
Keep planning brief and write the code. Aim for roughly 500 to 750 lines, compact and well structured.