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

Polar Spectrum · How it works & code

Wrap the spectrum into a circle: angle = frequency, radius = decibels

Runtime logic

Visiting `/audio/polar`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'polar' })`. mount resolves the variant to family `polar`; `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.stroke() — Strokes the current 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.setLineDash() — Sets the dash pattern for strokes; ctx.arcTo() — Adds an arc using tangent lines; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width; ctx.createRadialGradient() — Creates a radial gradient; ctx.fillRect() — Fills a rectangle

Source code

frontend/src/demos/kits/kit-audio.js: FAM.polar (L553-L591)

Code walkthrough

L555-555: Control definitions (colour/speed/size/count/extras): Starting hue, Outer radius, Sample points, Background noise threshold dB, Rotation speed, Rainbow colors. L556-556: Preset buttons: name → parameter overrides (6). L557-565: Info-panel content: live rows(v), formula, notes, params, steps. L566-590: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `arc`, `stroke`, `moveTo`, `lineTo`, `closePath` …; produces/reads sound via `A.voices`; math via `Math.round`, `Math.min`, `Math.cos`, `Math.sin`, `Math.atan2`; returns readouts `chord`, `n`, `fl`, `rot`, `deg`, `f`, `l` … to the info panel.

Principles

Angle θ = 2π × log(f / 40) / log(7600 / 40) (logarithmic frequency around) Radius r = R × (L − Doorsill) / (0 − Doorsill) pts = (cx + r·cos θ, cy + r·sin θ) Put the normal one"Spectrum Curve"Change Number from Point to Point Polar Coordinates :Walking around the circle is from 40 Hz 到 7600 Hz(按倍数均匀分布),离圆心越远表示这个频率越响。 The bass, the chord, the melody in the music will come out from different angles"Petal"; Every single chord, the petal jumps. Doorsill The following sounds are considered 0, To avoid a small circle of noise.

Open the interactive demo · GitHub