A board that redraws.
By the end of this page we will have a percolation board that redraws itself whenever a slider moves or a button is pressed, with every cluster colored and the spanning cluster picked out in orange. The whole thing is one HTML fragment and about seventy lines of JavaScript, and the code shown below is the code running on this page.
Series overview · Next: Charts that mirror a notebook
What we are going to build
Move the slider and watch the clusters. Near \(p \approx 0.59\) a cluster first reaches from the top row to the bottom row; with corner contacts allowed, that happens much earlier. Everything on the board is recomputed and redrawn on each change.
The page: a drawing and three controls
The HTML is the part most people expect to be hard, and it is the shortest. An empty <svg> will hold the board. A range input is the slider. Two buttons choose the neighbor rule and one asks for a new random board. The attributes beginning with data- are names we choose so that the script can find each element; the browser ignores them.
<div data-svg-board>
<svg role="img" aria-label="A percolation board"></svg>
<label for="p">Occupation probability p = <span data-p-value></span></label>
<input id="p" type="range" min="0.1" max="0.9" step="0.01" data-p>
<button type="button" data-neighbors="4" aria-pressed="true">4 neighbors</button>
<button type="button" data-neighbors="8" aria-pressed="false">8 neighbors</button>
<button type="button" data-new>New board</button>
<p data-reading></p>
</div>
<script type="module" src="/assets/js/browser-lessons/svg-board.mjs"></script>
The type="module" on the script matters. A module can import functions from other files, runs after the page has been parsed, and keeps its variables to itself. Every interactive page on this site is written this way.
Step 1: Import the physics, not the drawing
The cluster labeling already exists in the module behind the Hoshen–Kopelman lesson, so we import four functions from it: a seeded random number generator, a random board, the labeling itself and the test for a spanning cluster. The import line is the JavaScript form of from hk_game import labelClusters. The lesson is about drawing, so nothing in this file computes a cluster.
Loading the code…
Step 2: One object holds the state
I keep everything the picture depends on in one plain object: the board size, the occupation probability, the random seed and the neighbor rule. A control changes one field and then asks for a redraw. This is the single habit that keeps interactive pages simple. Nothing is stored in the drawing itself, so the drawing can always be rebuilt from the state.
Loading the code…
Step 3: Build the board once
We create one <rect> for every cell, in reading order, and keep them in an array. The trick that removes all pixel arithmetic is the viewBox: we tell the SVG that it is \(N\) units wide and \(N\) units tall, and then a cell at row \(r\) and column \(c\) is simply the unit square at \((c, r)\). The browser scales the picture to whatever width the page gives it, on a phone or a projector.
Loading the code…
SVG elements live in their own namespace, which is why we write createElementNS with the SVG address rather than the usual createElement. It is the one piece of ceremony in this file.
Step 4: Draw means set one attribute per cell
A redraw recomputes the board from the state and sets the fill of each rectangle. Empty cells stay paper colored. The spanning cluster, if there is one, is orange. Every other cluster takes a hue from its label number, with a stride of 47 degrees around the color circle so that neighboring labels look different. The function returns the cluster count and whether a cluster spans, for the sentence under the board.
Loading the code…
Twenty-four by twenty-four cells is 576 attribute changes per redraw, which the browser does in well under a millisecond. For boards of a few hundred cells on a side I would switch to a <canvas> and paint pixels, but SVG rectangles are easier to inspect, style and make accessible, so they are the right first choice.
Step 5: Wire the controls
Each control gets a listener. The slider fires input continuously while it moves, so the board follows the thumb. The buttons fire click. Each listener does the same two things: change the state, call refresh(). The aria-pressed attribute tells screen readers which neighbor button is active and gives the stylesheet something to color.
Loading the code…
Step 6: Start it
The last lines look for every element marked data-svg-board and mount a board in each. The check for document lets the same file be imported by a Node test, where there is no page; the test exercises the state and color functions without a browser.
Loading the code…
What to try
- Change the stride 47 in
colorForto 1 and see why neighboring labels need to be far apart on the color circle. - Replace
'input'with'change'on the slider. The board now updates only when the thumb is released. Lesson 3 uses this deliberately, because its calculation is slower. - Add a second readout that shows the size of the largest cluster. The sizes are already returned by
labelClusters.