A magnet at its critical point, where domains of spins turn fractal.
Each small square is a spin, up (paper) or down (ink), on a lattice of about 90,000, and neighbors that agree lower the energy. Below the critical temperature Tc = 2.269 the magnet orders, above it noise wins, and right at Tc domains appear inside domains at every scale. Ordinary spin flips crawl there, so the engine is the Wolff cluster algorithm: grow a cluster of aligned spins with bond probability 1 - exp(-2/T) and flip it whole, an exact Markov chain whose clusters are themselves the critical fractals, plus a checkerboard heat bath for local shimmer. Spins that disagree with the magnetization averaged over their neighborhood are colored, paper islands amber and ink lakes teal, so the nesting shows at a glance, and the inset tracks the measured magnetization against Onsager's exact solution.
Try it. Drag the temperature slider (or use the arrow keys; C snaps to Tc, 1 and 3 jump to cold and hot). Drag on the lattice to paint a magnetic field that pulls spins up, and right-drag or Shift-drag for the opposite pole. Space reheats to random spins, and R also clears the painted field. Leave it alone and it tours the phase diagram.
Wolff cluster algorithm
Checkerboard heat bath
Onsager's exact solution
Two-scale coloring by coarse-grained magnetization
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 Ising model 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. Create a lattice of about 300 x 200 spins in an Int8Array, each +1 or -1 at random, with periodic (wrap-around) edges.
- Each frame, run one Metropolis sweep: pick random spins, compute the energy change of flipping one (2 times the spin times the sum of its four neighbors), and flip it if that change is negative or with probability exp(-change / T).
- Draw the lattice into an ImageData one pixel per spin, up spins light and down spins dark, put it on a small offscreen canvas, and drawImage it scaled up with image smoothing off.
- Add a temperature slider from 1.5 to 3.5 and mark the critical temperature, Tc = 2 / ln(1 + sqrt(2)), about 2.269. Watch it order when cold, boil when hot, and grow domains of every size near Tc.
Once that works, make it beautiful and fast:
- Near Tc, single flips get very slow. Add the Wolff cluster algorithm: pick a random seed spin, grow a cluster with a stack by adding each aligned neighbor with probability 1 - exp(-2 / T), then flip the whole cluster at once. Run a few clusters per frame and watch the fractal domains appear in seconds.
- Use warm paper and ink colors instead of pure white and black, and add a soft vignette.
- Plot the measured magnetization |m| against Onsager's exact curve, m = (1 - sinh(2 / T)^-4)^(1/8) below Tc and 0 above, in a small inset.
- Let the mouse paint a local magnetic field that adds to each spin's energy, so you can draw with domains.
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 measuring the correlation length, a 3D Ising slice viewer, or the q-state Potts model with more colors.