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

Chord Pad · How it works & code

Constituent notes, frequency ratios, and synthesized waveforms of major and minor chords, automatically cycling through C–G–Am–F

Runtime logic

Visiting `/audio/chord`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'chord' })`. mount resolves the variant to family `chord`; `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.get — Reads current control values; S.onDown — Pointer down; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; 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.chord (L699-L744)

Code walkthrough

L700-700: Control definitions (colour/speed/size/count/extras): Chord board color, Board spacing, Chord board count, Turn (0 original 1 1 1 2 2), Overall octave shift, Add seventh. L701-701: Preset buttons: name → parameter overrides (6). L702-710: Info-panel content: live rows(v), formula, notes, params, steps. L711-743: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `fill`, `stroke`, `fillRect`, `beginPath`, `lineTo`, `moveTo`; produces/reads sound via `A.voices`; math via `Math.min`, `Math.ceil`, `Math.floor`, `Math.round`, `Math.exp`, `Math.pow`; returns readouts `cur`, `notes`, `freqs`, `ratio`, `hov`, `inv`, `sev` … to the info panel.

Principles

Grand 3 chord = root + 4 semis + 7 semis ≈ frequency ratio 4:5:6 Xiao San chord = Root of chord + 3 + 7 Half≈ 10: 12: 15 One and a half sound per litre, frequency × 2^(1/12) ≈ 1.0595 Chord 就是同时响起的几个音。最常见的 Trichord 35 Composed of root, triangulation and pentagrams: 3th chords C = do-mi-sol) Sounds bright, trichord Am = la-do-mi) Sounds soft and sad. They're beautiful because of the frequency: C The frequency of the third chord is approaching 4:5:6 ,Waves are cyclically aligned"A mixture"。下方的小琴键标出组成音,波形是三个音相加的结果。点任意和弦板就能切换;不碰时会自动按 C–G–Am–F Go ahead.

Open the interactive demo · GitHub