Word Cloud · How it works & code
Zipf frequency + Archimedean spiral to prevent overlap, with linear/√/log size mapping.
Runtime logic
Visiting `/dataviz/wordcloud`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'wordcloud' })`. mount resolves the variant to family `wordcloud`; `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. 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; rng — Seeded deterministic PRNG (same seed, same result); 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.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.measureText() — Measures text width; ctx.createRadialGradient() — Creates a radial gradient; ctx.fillRect() — Fills a rectangle; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.translate() — Translates the coordinate system; ctx.scale() — Scales the coordinate system; ctx.rotate() — Rotates the coordinate system; ctx.restore() — Restores the saved drawing state; ctx.strokeRect() — Strokes a rectangle; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.wordcloud (L511-L558)
Code walkthrough
L512-512: Control definitions (colour/speed/size/count/extras): Starting hue, Font size scaling, Word count, Vertical proportion, Font size mapping, Monochrome. L513-513: Preset buttons: name → parameter overrides (6). L514-522: Info-panel content: live rows(v), formula, notes, params, steps. L523-557: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `measureText`, `createRadialGradient`, `fillRect`, `save`, `translate`, `scale` …; math via `Math.round`, `Math.sqrt`, `Math.log`, `Math.min`, `Math.cos`, `Math.sin` …; returns readouts `n`, `placed`, `w`, `f`, `px`, `m`, `f1` … to the info panel.
Principles
The word follows the law of Zipov: k Number of names ≈ f₁ / k FontSize = Min + (Max − Min) × m(f), m To Linear / √ / log Shine on Archimed screw placement: r = a·θ, Find the first non-overlapping position along the screw Word Cloud 把文本里出现次数多的词画得大,次数少的画得小,快速告诉你"What's this text about?"。 Arranged method: Start with the most frequent words, first at the centre; then the words follow one line Archimedean spiral Go round and round until you find a word you don't like Overlapped Location. Character mapping is critical: linear mapping makes the first one particularly large and all others very small; square root or logarithmic mapping is more even. The pointer suspends any word to see its number and number.