Pulsing Grid · How it works & code
Circular waves emanate from the pointer position, amplifying and brightening points where the wave peaks pass, like buoys on a water surface.
Runtime logic
Visiting `/particles-fx/grid-pulse`, DemoHost lazy-loads `demos/kits/kit-particles.js` (registry `mod: 'kit-particles'`) and calls `mount(host, { variant: 'grid-pulse' })`. mount resolves the variant to family `grid-pulse`; `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; 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.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.save() — Saves the drawing state (transform, styles, clip); ctx.rect() — Adds a rectangle to the path; ctx.clip() — Turns the current path into the clipping region; ctx.restore() — Restores the saved drawing state
Source code
frontend/src/demos/kits/kit-particles.js: FAM.grid-pulse (L257-L266)
Code walkthrough
L257-266: Calls the factory `pFam()` to produce the full definition (spec + doc + build) from the options below. L257-257: Title shown in the info and control panels: “Grid pulse”. L257-257: Range/default of the size slider: Point size. L257-257: Range/default of the count slider: Grid Columns. L258-258: Extra demo-specific slider/toggle: Wavelength. L258-258: Preset buttons: name → parameter overrides (4). L259-259: Body of the “Notes” section. L259-259: Main formula of the “Formula” section. L259-259: Second formula line. L260-266: Per-frame draw function: math via `Math.max`, `Math.round`, `Math.hypot`, `Math.sin`, `Math.exp`; returns readouts `n` to the info panel.
Principles
波 = sin(2π·d/Wavelength − 4t) Radius = Size · (1 + 0.8 · max (wave, 0) Grid pulse: A circle of circular waves from the pointer position where the peak is magnified and brightened at points, like buoys on the surface of the water. The location of each particle here is numbered i Random Feeds + Time t + 指针位置」直接算出来,所以拖动滑条会立刻生效。