Burst · How it works & code
Particles burst shaped by velocity field.
Runtime logic
Visiting `/canvas/burst`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'burst' })`. mount resolves the variant to family `burst`; `withControls` builds the control panel (8 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); 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.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.burst (L547-L586)
Code walkthrough
L548-548: Control definitions (colour/speed/size/count/extras): Particle Color, Particle Size, particles per burst, drag k, Shape 1 Circle, 2 Stars, 3 Rings, 4 Hearts, auto interval s. L549-549: Preset buttons: name → parameter overrides (8). L550-558: Info-panel content: live rows(v), formula, notes, params, steps. L559-585: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `beginPath`, `arc`, `fill`; math via `Math.round`, `Math.min`, `Math.cos`, `Math.sin`, `Math.abs`, `Math.hypot` …; returns readouts `live`, `shape`, `u`, `k`, `r`, `rmax`, `shots` … to the info panel.
Principles
v(t) = u·e^(−kt),r(t) = u·(1 − e^(−kt)) / k Maximum radius r∞ = u / k Shape: u(m)=u · g (thorium), g = 0.55 + 0.45|cos(2.5θ)| All particles start from the same point Evenly distributed direction 在 360°。The shape after the explosion is completely by“First speed in each direction”决定:速度都一样就是圆环,按角度调制速度就变成星形或心形。 阻力让速度按指数衰减,所以图案先快速扩张、再慢慢定格,最远到 u/k。 Click the canvas to explode once at the pointer.