Multi-column Text · How it works & code
A long paragraph automatically splits into N columns, shortening the reading line.
Runtime logic
Visiting `/layout/columns`, DemoHost lazy-loads `demos/kits/kit-layout.js` (registry `mod: 'kit-layout'`) and calls `mount(host, { variant: 'columns' })`. mount resolves the variant to family `columns`; `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; hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; fmt — Number formatting (fixed decimals); F — Formula block HTML; 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.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.setLineDash() — Sets the dash pattern for strokes; ctx.fillText() — Draws filled text; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-layout.js: FAM.columns (L245-L287), TITLES['columns'] (L647-L647)
Code walkthrough
L247-247: Control definitions (colour/speed/size/count/extras): Emphasis color, Column gap, Number of columns, Typography Effect, Vertical lines between columns, Sinking effect. L248-248: Preset buttons: name → parameter overrides (6). L249-257: Info-panel content: live rows(v), formula, notes, params, steps. L258-286: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `fill`, `beginPath`, `moveTo`, `lineTo`, `stroke`, `setLineDash`; math via `Math.round`, `Math.max`, `Math.floor`; returns readouts `cols`, `gap`, `cw`, `cell`, `chars`, `rule`, `drop` to the info panel. L647-647: This demo's entry in the `TITLES` table (title).
Principles
Column-count: N (or Column-width: X) Column-gap: G;column-rule painting column vertical line Flow content by Filling a column and spilling to the next column It's in newspapers and magazines Multibar Body :A long text automatically torn N bar, reading lines are shorter. CSS 用 column-count / column-width 声明,内容像瀑布一样先流完左栏再进右栏(和 Grid This post is part of our special coverage Global Voices 2011. Simulates the text stream with a colour paragraph here; suspends a column to read estimated words. The opening of the "substance" will draw a capital guide in the upper left corner of the first column.