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

Power Functions · How it works & code

Comparison of power functions with different exponents.

Runtime logic

Visiting `/math/power`, DemoHost lazy-loads `demos/kits/kit-math.js` (registry `mod: 'kit-math'`) and calls `mount(host, { variant: 'power' })`. mount resolves the variant to family `power`; `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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. 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; MATH_DOCS — Formula/notes/params text table for math demos; ctx.fillRect() — Fills a rectangle; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.setLineDash() — Sets the dash pattern for strokes; 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.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.restore() — Restores the saved drawing state; ctx.fillText() — Draws filled text

Source code

frontend/src/demos/kits/kit-math.js: FAMILY_CONTROLS['power'] (L86-L86), drawFamily() · case 'power' (L606-L606), MATH_DOCS['power'] (L106-L114)

Code walkthrough

L86-86: This demo's entry in the `FAMILY_CONTROLS` table (control config). L606-606: `case` branch in `drawFamily()`: this demo's per-frame drawing. L106-114: This demo's entry in the `MATH_DOCS` table (info-panel text: formula/notes/params/steps).

Principles

y = xⁿ x^½ = √x,x⁻¹ = 1/x All of them (1,1)。n>1 在 (0,1) Lower Internal y=x、(a) Then it gets steeper; 0<n<1 相反;n<0 在 x→0 时趋于无穷。奇数次关于原点对称。

Open the interactive demo · GitHub