Circle Packing · How it works & code
Randomly select a point, calculate its distance to the nearest circle and border, place a new circle there that just fits without overlapping; repeat many times, larger circles occupy positions first, smaller ones fill the gaps.
Runtime logic
Visiting `/generative/circles`, DemoHost lazy-loads `demos/kits/kit-generative.js` (registry `mod: 'kit-generative'`) and calls `mount(host, { variant: 'circles' })`. mount resolves the variant to family `circles`; `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; TAU — Constant 2π; 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.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillRect() — Fills a rectangle; ctx.drawImage() — Draws an image or offscreen canvas; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.rect() — Adds a rectangle to the path; ctx.clip() — Turns the current path into the clipping region; ctx.restore() — Restores the saved drawing state
Source code
frontend/src/demos/kits/kit-generative.js: FAM.circles (L259-L269)
Code walkthrough
L259-269: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L259-259: Title shown in the info and control panels: “Circle Packing”. L259-259: Range/default of the size slider: Maximum radius. L259-259: Range/default of the count slider: Attempt count. L260-260: Extra demo-specific slider/toggle: Spacing. L260-260: Default main colour. L260-260: Help text of the count parameter. L260-260: Preset buttons: name → parameter overrides (5). L261-261: Body of the “Notes” section. L261-261: Main formula of the “Formula” section. L261-261: Second formula line. L262-269: Per-frame draw function: draws with Canvas 2D `beginPath`, `arc`, `fill`, `stroke`; math via `Math.min`, `Math.hypot`, `Math.sin`; returns readouts `mode`, `cell` to the info panel.
Principles
r = min (maximum radius to border distance, min j)|p − c_j| -r j) - spacing) r Circle Filling: randomly select a point, calculate how far it is from the nearest circle and border, and place a circle that happens to be non-overlapping; repeat many times, with a large circle first in position, and a small dome. The circle breathes gently, and the circle around the pointer becomes brighter. Generative Art = a simple mathematical rule + Random number/噪声,让电脑自己「画」出图案;改变参数就是改变规则。