Grid Wave · How it works & code
Grid ripple driven by decaying circular wave.
Runtime logic
Visiting `/canvas/gridwave`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'gridwave' })`. mount resolves the variant to family `gridwave`; `withControls` builds the control panel (6 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; 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.beginPath() — Starts a new path; ctx.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; ctx.stroke() — Strokes the current 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.gridwave (L632-L665)
Code walkthrough
L634-634: Control definitions (colour/speed/size/count/extras): Grid color, Amplitude A, Grid spacing px, Wave Number k, Decay length L, Connect to lines. L635-635: Preset buttons: name → parameter overrides (6). L636-644: Info-panel content: live rows(v), formula, notes, params, steps. L645-664: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `lineTo`, `moveTo`, `stroke`, `arc`, `fill`; math via `Math.round`, `Math.floor`, `Math.sin`, `Math.exp`, `Math.abs`; returns readouts `pts`, `gap`, `k`, `lam`, `v`, `L`, `z0` to the info panel.
Principles
z(d, t) = 14A · sin(k·d − ωt) · e^(−d / L) d = Description of grid point to pointer, =3 Rad / s Screen y′ = y − z, Brightness ∝ (z + 14A) 以指针为圆心发出一圈圈波纹,网格上每个点按自己到指针的距离 d Calculate Offset z。 sin(k·d − ωt) It's not like we're going to be able to do this e^(−d/L) Keeps the distance away from the rise and fall——It's like a gravel in the water, and it's gonna Whereas the twilight. The shift to vertical deflection and brightness gives the dots of the plane a 3D sense.