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

Spectrum Bars · How it works & code

Real FFT: Breaks music into frequency bars with peak hold and log frequency axis.

Runtime logic

Visiting `/audio/bars`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'bars' })`. mount resolves the variant to family `bars`; `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.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.createLinearGradient() — Creates a linear gradient; ctx.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width; ctx.createRadialGradient() — Creates a radial gradient; AudioContext.createBiquadFilter() — Creates a biquad filter (low/band/high-pass)

Source code

frontend/src/demos/kits/kit-audio.js: FAM.bars (L315-L356)

Code walkthrough

L317-317: Control definitions (colour/speed/size/count/extras): Bar Color, Bar Width Proportion, Band count, Peak Drop dB/s, Smooth τ, Log Frequency Axis. L318-318: Preset buttons: name → parameter overrides (6). L319-327: Info-panel content: live rows(v), formula, notes, params, steps. L328-355: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `createLinearGradient`, `fill` …; produces/reads sound via `A.voices`; math via `Math.round`, `Math.pow`, `Math.max`, `Math.floor`, `Math.min`; returns readouts `chord`, `beat`, `n`, `ax`, `band`, `rng`, `lv` … to the info panel.

Principles

Reciprocal subparagraphs: fᵢ = 40 × (7600/40)^(i/N) Frequency tape level L = 10·log₁₀(Average |X(k)|²) (X = FFT Results) Peak ← max (peak) - Drop speed · Δt, L) From the player"Music column"That's it. Let's get closer 64 The sound of milliseconds is done once FFT ,Get 512 The strength of each frequency point; then divide them into parts N 段,每段画一根柱子,柱子越高这段频率越响。 Log Frequency Axis :People think 100→200 Hz 和 1000→2000 Hz 的"Span"一样大,所以按倍数分段更自然;关掉后低音会挤在最左边。 Decibel Scale Synonym: impact by log Compression, whispering. The little stripes on the top of the pole are: Peak , It's going to slow down and make it easier to see what's going on.

Open the interactive demo · GitHub