加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Audio-reactive Particles · How it works & code

Drive particle count, speed, and color using energy from low, mid, and high frequency bands.

Runtime logic

Visiting `/audio/part-audio`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'part-audio' })`. mount resolves the variant to family `part-audio`; `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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. With the top-right Sound switch on, sound plays through the shared AudioContext; off = muted. 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.onDown — Pointer down; 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; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle; ctx.fillText() — Draws filled text; AudioContext.createBiquadFilter() — Creates a biquad filter (low/band/high-pass); AudioContext.createBuffer() — Creates an audio sample buffer; AudioContext.createBufferSource() — Plays a sample buffer (drums/noise); AudioContext.createGain() — Creates a gain (volume) node; AudioContext.createOscillator() — Creates an oscillator (sine/square/saw/triangle)

Source code

frontend/src/demos/kits/kit-audio.js: FAM.part-audio (L460-L500)

Code walkthrough

L462-462: Canvas cursor style. L463-463: Control definitions (colour/speed/size/count/extras): Particle Color, Particle Size, Max particle count, Sensitivity, Gravity, Trail. L464-464: Preset buttons: name → parameter overrides (6). L465-473: Info-panel content: live rows(v), formula, notes, params, steps. L474-499: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `arc`, `fill`, `stroke`, `fillRect`; produces/reads sound via `A.voices`; math via `Math.round`, `Math.sqrt`, `Math.random`, `Math.cos`, `Math.sin`, `Math.min` …; returns readouts `chord`, `beat`, `bands`, `rms`, `cen`, `emit`, `alive` … to the info panel.

Principles

Number of launches ∝ RMS × Sensitivity RMS = √(Σx² / N) First speed, twilight, low frequency energy Colour ∝ spectral mass = Σ f · |X(f)| / Σ |X(f)| 这是音乐可视化里最常见的玩法:把声音拆成几个"Numeral",And let these figures control the picture. RMS (How loud is it?——(b) The number of particles being sprayed; Low-frequency energy (Drums and basses determine the speed of the jet, so each drum ends"咚"It'll blow up at once Spectrum Mass A voice"Brightness", Pronunciation multitemporal and high-temperature particles favour another colour. Hold and drag to move the launch point.

Open the interactive demo · GitHub