Visualizations
358 / 500

358 · Physics

Optical Bench

A blueprint ray tracer: prisms, lenses, mirrors and a fiber with real glass dispersion.

Every part on this blueprint is built from exact straight faces and circular arcs, and every ray is traced separately in 16 wavelengths. At glass it refracts by Snell's law with an index from the Sellmeier equation for real BK7 crown and SF11 flint, is trapped by total internal reflection when Snell's law has no answer, and leaves a fainter reflection behind sized by Fresnel's equations. So the flint prism fans white light into a spectrum, a fast lens shows both spherical and chromatic aberration, a telescope squeezes a wide beam into a narrow one, and a curved fiber carries light around a corner. The segments are splatted as anti-aliased lines into a floating point buffer, colored so the whole spectrum adds up to white, then tone mapped with a soft bloom.

Try it. Drag parts to move them, and turn them with the mouse wheel, Q and E, or the round handle above the selected part. Add parts from the toolbar, remove them by dropping them on it, press G to swap a part between crown and flint glass, and press 1 to 5 for the prism, telescope, aberration, fiber and periscope setups.

  • 2D ray tracing with Snell and Fresnel
  • Sellmeier dispersion
  • Anti-aliased float buffer splatting

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 interactive 2D optics sandbox 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 canvas that fills the window and stays sharp on high-DPI screens, with a dark blueprint background and a faint grid.
- Represent every optical part as a list of straight edges, each with an outward normal: a triangular prism, a rectangular glass block and a flat mirror to begin with.
- Emit a beam of about 15 parallel rays from a light source. For each ray, find the nearest edge it hits, then reflect it at mirrors, or refract it at glass with Snell's law (n = 1.5). Work out whether it is entering or leaving from the sign of the ray direction dotted with the normal, and use total internal reflection when Snell's law has no solution. Draw each segment as a line.
- Let the mouse drag parts around and the wheel rotate them, retracing every frame.

Once that works, make it beautiful:
- Trace each ray in about 12 wavelengths, with an index that depends on wavelength (a Cauchy formula is fine, or the Sellmeier equation for real glasses) and a color for each wavelength. Draw with globalCompositeOperation = "lighter" so the colors add back to white where they overlap, and the prism splits white light into a rainbow.
- Add lenses built from two circular arcs, and watch the rays near the edge focus too early.
- Add Fresnel reflectance so each glass surface leaves a faint reflected ray behind.
- Add a screen that shows the colors that land on it.

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 curved optical fiber, a float buffer with bloom for glowing beams, or a two-lens telescope.
PreviousPrism PuzzleSplit white light with rutile prisms to light colored targets. A search AI plays. NextGaussian Mixture EMExpectation maximization fits Gaussians to a painting and turns it into a poster.

Related visualizations

  • Rainbow OpticsPhysics A double rainbow, dark band and fogbow computed from Snell's law and wave optics.
  • Eye OpticsPhysics Real ray tracing through a model human eye that focuses, blurs and wears glasses.
  • Pool CausticsPhysics Sunlight bent by a rippling pool surface draws a living net of light on the tiles.
  • Ice Crystal HalosPhysics Sundogs, arcs and halos built up live from millions of rays traced through ice.
  • Path Tracer3D Photorealistic light, computed by bouncing millions of random rays.
  • Glass HarpSound Ray-traced wine glasses, tuned with water and sung by stick-slip friction on the rim.
  • MiragePhysics Bent light over hot asphalt and cold sea: road puddles, floating cars, Fata Morgana.
  • SOFAR ChannelPhysics Sound rays bend into the ocean's deep channel, lighting up caustics and shadow zones.
  • Light & ShadowPhysics Red, green and blue lights cast crisp, colored shadows through spinning shapes.

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

← More from Emergent Mind Labs