Ink Spread · How it works & code
Gaussian Diffusion of Ink.
Runtime logic
Visiting `/canvas/ink`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'ink' })`. mount resolves the variant to family `ink`; `withControls` builds the control panel (4 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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. 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; rng — Seeded deterministic PRNG (same seed, same result); makeNoise — Smooth value noise + fBm octaves; hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.get — Reads current control values; S.onDown — Pointer down; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.createRadialGradient() — Creates a radial gradient; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path
Source code
frontend/src/demos/kits/kit-canvas.js: FAM.ink (L667-L706)
Code walkthrough
L669-669: Control definitions (colour/speed/size/count/extras): Ink Color, Drip Size, Diffusion Coefficient D, Auto Drip Interval (s). L670-670: Preset buttons: name → parameter overrides (4). L671-679: Info-panel content: live rows(v), formula, notes, params, steps. L680-705: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `createRadialGradient`, `beginPath`, `arc`, `fill`; math via `Math.max`, `Math.sqrt`, `Math.cos`, `Math.sin`; returns readouts `n`, `D`, `age`, `sig`, `peak`, `drops`, `every` … to the info panel.
Principles
∂c/∂t = D·∇²c (Equation of Proliferation) Point source: c(r,t) = M/(4πDt) · e^(−r²/(4Dt)) Radius σ = √(2Dt), Peak ∝ 1/t A drop of ink goes into the water, and molecules do irregular movements, spreading from thick to light. It's a math thing Proliferation Equation Description, its solution is a widening and shrinking one Gaussian Bell 。 Spread radius with √t Growth (slower later), central concentration by 1/t Decline, but total ink M No change. Each drop of ink is also drifted with a slight stream, forming a natural shape. Click on the canvas drop.