Arc Progress · How it works & code
multi-layer KPI rings, with linear/ease-out/elastic easing animations
Runtime logic
Visiting `/dataviz/arc`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'arc' })`. mount resolves the variant to family `arc`; `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.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.stroke() — Strokes the current path; ctx.fill() — Fills 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.fillText() — Draws filled text; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.arc (L738-L776)
Code walkthrough
L739-739: Control definitions (colour/speed/size/count/extras): Starting hue, Ring width, Ring Count, ring spacing px, easing method, neon glow. L740-740: Preset buttons: name → parameter overrides (6). L741-749: Info-panel content: live rows(v), formula, notes, params, steps. L750-775: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `arc`, `stroke`, `fill`; math via `Math.cos`, `Math.exp`, `Math.round`, `Math.min`, `Math.hypot`, `Math.floor` …; returns readouts `n`, `name`, `cur`, `tgt`, `from`, `rem`, `s` … to the info panel.
Principles
Arc End Point = Start angle + Completion × 2π Animation: Value = Old value + (New Value − Old value) × e(s),s 从 0 Come on 1 Slow out e(s) = 1 − (1 − s)³; Elasticity e(s) = 1 − cos(4.5πs)·e^(−6s) Arc Progress (Apple And it's like a gym ring Arc length as a proportion of the circle = Completion . We can watch several at the same time KPI. The value changes not instantaneously, but with Slow function Animation: linear velocity;"Slow out"It's more natural than fast"Elasticity"会冲过头再回弹。每隔一段时间会刷新一次目标值来演示。 The pointer moves to the ring (as measured by distance to the centre) and shows its current values and targets on the right.