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

Number Bases · How it works & code

multi-base integers with bit visualization

Runtime logic

Visiting `/convert/radix`, DemoHost lazy-loads `demos/kits/kit-convert.js` (registry `mod: 'kit-convert'`) and calls `mount(host, { variant: 'radix' })`. mount resolves the variant to family `radix`; `withControls` builds the control panel (6 presets + sliders). `createInfoPanel` splits the stage into canvas + info panel; `makeCanvas` creates a HiDPI canvas. Every `requestAnimationFrame`: read controls, `drawFamily` runs this demo’s branch, and the info panel readout refreshes. 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

makeCanvas — Creates an auto-sized canvas; loop — requestAnimationFrame loop (stoppable); bar — Demo control bar; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; readLiveControls — Reads live control values (CSS-variable fallback); localPoint — Event → canvas-local coordinates; withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; createInfoPanel — Side/bottom info panel: live readout + formula/notes/params/steps; alignDocParams — Aligns the documented parameters with the actual controls; CONVERT_DOCS — Text table for unit-conversion demos; ctx.fillRect() — Fills a rectangle; ctx.fillText() — Draws filled text; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.createLinearGradient() — Creates a linear gradient; ctx.arc() — Adds a circular arc (center, radius, start/end angle)

Source code

frontend/src/demos/kits/kit-convert.js: FAMILY_CONTROLS['radix'] (L35-L35), onDown() · if (family === 'radix') (L114-L114), onDown() · if (family === 'radix') (L118-L134), onMove() · if (family === 'radix') (L142-L142), drawFamily() · case 'radix' (L237-L237), CONVERT_DOCS['radix'] (L57-L65)

Code walkthrough

L35-35: This demo's entry in the `FAMILY_CONTROLS` table (control config). L114-114: Branch for this demo inside `onDown()`: math via `Math.round`. L118-134: Branch for this demo inside `onDown()`: math via `Math.min`, `Math.max`, `Math.round`. L142-142: Branch for this demo inside `onMove()`: math via `Math.round`. L237-237: `case` branch in `drawFamily()`: this demo's per-frame drawing. L57-65: This demo's entry in the `CONVERT_DOCS` table (info-panel text: formula/notes/params/steps).

Principles

N = Σ bᵢ · 2ⁱ 1-digit hexadecimal = 4-bit binary, 1-bit octadecimal = 3-bit binary Production Change the same number to a different base. Each binary represents 2 arc; hexadecimals are read directly every 4 Collaborations.

Open the interactive demo · GitHub