How the interactive pages are made.
Students often ask how the boards, sliders and 3D views on this site work. The answer is shorter than it looks: a web page is a drawing that a small program redraws whenever something changes. These lessons build that program one part at a time, in plain JavaScript first and then with two libraries, and every page shows the exact code that is running on it.
I use the percolation board from the Hoshen–Kopelman lesson as the running example, so the physics is already familiar and we can concentrate on the interface. The cluster labeling is imported from that lesson's module rather than written again. Each page ends with a working interface that can be copied into another page under my code-use terms.
The lessons
- Lesson 1 · plain JavaScript and SVG
A board that redraws
A grid of SVG rectangles, one state object and one draw function. We add a slider for the occupation probability, a button for a new board and a neighbor toggle. No library.
- Lesson 2 · events and state
Making it respond
Event listeners, the one-state-one-draw pattern, keyboard access and touch. In preparation.
- Lesson 3 · Plotly
Charts that mirror a notebook
The same spanning-fraction curve we would draw with
plotly.graph_objects, drawn in the page and redrawn from a slider. A translation table from Python to JavaScript, and when Plotly is worth its size. - Lesson 4 · three.js
A cluster in three dimensions
Renderer, scene, camera and light; an instanced mesh of surface voxels; pointer rotation; rendering only while visible. About eighty lines.
- Lesson 5 · keeping the page responsive
Workers and animation frames
Moving a heavy loop off the page thread, requestAnimationFrame and reduced-motion settings, as in the elastic laboratory. In preparation.
- Lesson 6 · publishing
Putting it on a page
Where files go in a Jekyll site, cache-busting versions, a Node test for the model and the check at phone width. In preparation.
What we need before starting
A text editor and a browser. Nothing is installed: the pages are ordinary HTML files with a <script type="module"> tag, and the two libraries are loaded from public content networks when a chart or 3D view scrolls into view. Readers who know Python will recognize most of the JavaScript; the differences that matter are pointed out where they arise.
Why plain JavaScript first
Most interactive figures on this site use no library at all. An SVG rectangle with a fill attribute is as easy to set as a NumPy array element, and a page that loads nothing extra opens instantly on a phone. I reach for Plotly when the reader needs hover readouts, zoom or export, and for three.js when there are tens of thousands of cubes to draw. The lessons follow the same order.