Dust Storm · How it works & code
A narrow beam of light enters from the upper left, illuminating floating dust within the beam while remaining nearly invisible outside the beam; the beam rotates with the pointer.
Runtime logic
Visiting `/particles-fx/dust`, DemoHost lazy-loads `demos/kits/kit-particles.js` (registry `mod: 'kit-particles'`) and calls `mount(host, { variant: 'dust' })`. mount resolves the variant to family `dust`; `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.save() — Saves the drawing state (transform, styles, clip); ctx.translate() — Translates the coordinate system; ctx.rotate() — Rotates the coordinate system; ctx.createLinearGradient() — Creates a linear gradient; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.restore() — Restores the saved drawing state; ctx.arcTo() — Adds an arc using tangent lines; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle; ctx.arc() — Adds a circular arc (center, radius, start/end angle)
Source code
frontend/src/demos/kits/kit-particles.js: FAM.dust (L313-L323)
Code walkthrough
L313-323: Calls the factory `pFam()` to produce the full definition (spec + doc + build) from the options below. L313-313: Title shown in the info and control panels: “Beam Dust”. L313-313: Range/default of the size slider: Dust Size. L313-313: Range/default of the count slider: Dust Count. L314-314: Extra demo-specific slider/toggle: Beam Width. L314-314: Default main colour. L314-314: Preset buttons: name → parameter overrides (4). L315-315: Body of the “Notes” section. L315-315: Main formula of the “Formula” section. L315-315: Second formula line. L316-323: Per-frame draw function: draws with Canvas 2D `save`, `translate`, `rotate`, `createLinearGradient`, `beginPath`, `moveTo` …; math via `Math.atan2`, `Math.cos`, `Math.sin`, `Math.min`, `Math.hypot`, `Math.abs` ….
Principles
Dust: Brown Movement x = x₀ + 20·noise(t) Brightness = smoothstep (wide, 0, distance to beam centre) Beam dust: A beam of tilted light comes in from above the left, floating dust is lit in the beam, almost invisible outside the beam; the beam moves with the pointer. The location of each particle here is numbered i Random Feeds + Time t + 指针位置」直接算出来,所以拖动滑条会立刻生效。