Falling Sand · How it works & code
cellular automaton sand fall and angle of repose.
Runtime logic
Visiting `/canvas/sand`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'sand' })`. mount resolves the variant to family `sand`; `withControls` builds the control panel (3 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21
Dependencies
react 18.3.1
Imported APIs
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; makeLayer — Offscreen layer (trails / pixel fields); rng — Seeded deterministic PRNG (same seed, same result); hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.clear — Clears with the background colour (translucent = trails); S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.drawImage() — Draws an image or offscreen canvas; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.stroke() — Strokes the current path
Source code
frontend/src/demos/kits/kit-canvas.js: FAM.sand (L792-L844)
Code walkthrough
L794-794: Control definitions (colour/speed/size/count/extras): sand color, grain pixel size, pouring rate (grains/s), bottom funnel. L795-795: Preset buttons: name → parameter overrides (3). L796-804: Info-panel content: live rows(v), formula, notes, params, steps. L805-843: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `drawImage`, `fillRect`, `beginPath`, `arc`, `stroke`; math via `Math.max`, `Math.floor`, `Math.round`, `Math.min`; returns readouts `g`, `cols`, `rows`, `rate`, `out`, `top`, `drain` to the info panel.
Principles
Rule①: Empty below → One drop Rule②: Or lower left/Bottom Right Empty → Tilt (twilight first) Otherwise static; maximum slope 1 格/格 ⇒ The Cape of ≈ 45° 画面被切成网格,每格要么是沙要么是空。每一步从下往上扫描,每粒沙只看身边三格,按两条简单规则移动——This is it It's all about motive 。 By local rules alone, the angles of sand piles, landslides and stable slopes emerge naturally The Cape of ). The peace of true dry sand 30°~35°, This is the rule 45°. 底部中央有个漏斗口,倒入与漏出平衡后会形成 V Stable sand.