Hermite-Gauss grids, donuts and spinning vortex beams from a two-mirror laser.
A laser's beam can only take shapes that survive a round trip between its mirrors: the Hermite-Gauss modes (grids of lobes) and Laguerre-Gauss modes (rings and donuts with a helical phase). Each is computed exactly per pixel from Hermite and Laguerre polynomial recurrences. The mirrors' g parameters set everything else: the cavity is stable only when 0 < g1 g2 < 1, the spot sizes and waist follow closed-form Gaussian beam formulas, and the Gouy phase fixes how far apart in frequency the transverse orders sit, so superposed modes beat and the pattern rotates at a rate the mirrors control. Tilting the output coupler walks the beam off the pump spot, by an amount that blows up near the stability edge, and the laser hops to whichever Hermite-Gauss mode overlaps the pump best. Past the edge a traced ray zigzags out of the cavity and the beam dies.
Try it. Pick a mode or superposition from the thumbnails or keys 0 to 9, and press P to see the phase as color. Drag on the beam to tilt the output coupler and watch it hop modes (A realigns). Drag a mirror up or down to bend it, drag the output coupler sideways to change the length, or drag the dot on the stability diagram. C changes the laser color.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a laser cavity mode explorer with 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 square canvas that shows a laser beam's cross-section, rendered per pixel into ImageData at about 200 x 200 and scaled up.
- Implement Hermite-Gauss modes HG(m, n) = H_m(sqrt2 x / w) H_n(sqrt2 y / w) exp(-(x^2 + y^2) / w^2), computing Hermite polynomials with the recurrence H(k+1) = 2u H(k) - 2k H(k-1).
- Implement Laguerre-Gauss modes LG(p, l) = (sqrt2 r / w)^|l| L_p^|l|(2 r^2 / w^2) exp(-r^2 / w^2) exp(i l phi), using the generalized Laguerre recurrence, and the complex power (x + i y)^l for the r^|l| exp(i l phi) part.
- Show intensity |E|^2 in a red laser color with a white-hot core. Add buttons for TEM00, HG 1,0, HG 3,3, a donut LG 0,1 and a vortex LG 0,3.
Once that works, make it beautiful:
- Add a phase view that colors each pixel by the field's phase (hue) and amplitude (brightness), so the vortex shows its spiral.
- Superpose modes of different order with a slowly advancing relative phase, such as LG 0,0 plus LG 0,3, so the pattern spins.
- Draw the cavity below: two mirrors with curvatures R1 and R2 a distance L apart. Compute g1 = 1 - L / R1 and g2 = 1 - L / R2, show the beam only when 0 < g1 g2 < 1, and scale the spot size with the Gaussian beam formula w^2 proportional to sqrt(g1 / (g2 (1 - g1 g2))). Let me drag the mirrors to bend them.
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 a g1-g2 stability diagram, mode hopping when a mirror is tilted, or tracing a ray that escapes an unstable cavity.