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

Circular Waveform · How it works & code

Wrap the time-domain waveform into a circle: radius = base + amplitude

Runtime logic

Visiting `/audio/circle-wave`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'circle-wave' })`. mount resolves the variant to family `circle-wave`; `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.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.arc() — Adds a circular arc (center, radius, start/end angle); 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.circle-wave (L358-L400)

Code walkthrough

L360-360: Control definitions (colour/speed/size/count/extras): Waveform color, Amplitude amplification, History ring count, Base radius, Duration of one circle (ms), Line width. L361-361: Preset buttons: name → parameter overrides (6). L362-370: Info-panel content: live rows(v), formula, notes, params, steps. L371-399: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `lineTo`, `moveTo`, `closePath`, `fill`, `stroke` …; produces/reads sound via `A.voices`; math via `Math.round`, `Math.min`, `Math.cos`, `Math.sin`, `Math.max`, `Math.atan2` …; returns readouts `chord`, `bpm`, `win`, `ns`, `deg`, `tm`, `x` … to the info panel.

Principles

Angle θ = 2π × (Sample Serial Number / Number of samples taken in a circle) Radius r(θ) = R₀ + A · x(θ) (x Yes −1…1 Sample value) Point Coordinate (cx + r) · cos θ, cy + r · sin θ) The normal oscillator paints the sound Left to Right 的曲线:横轴是时间,纵轴是振幅。这里把横轴"卷"In a circle: Turn around = A little while (Default 24 milliseconds) The radius indicates an amplitude up or down in the base circle. Sample values x 为正,曲线就鼓出去;为负就凹进来。声音越响,圆越"毛";低音(频率低)一圈里起伏次数少,高音起伏密。外面的淡色环是之前几个时刻的形状,形成拖影。

Open the interactive demo · GitHub