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

Pendulum · How it works & code

RK4 Non-linear monopolistic blocker + resonance drive.

Runtime logic

Visiting `/physics/pendulum`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'pendulum' })`. mount resolves the variant to family `pendulum`; `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. 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

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; hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; S.get — Reads current control values; S.onUp — Pointer up; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.stroke() — Strokes the current path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.fillRect() — Fills a rectangle; ctx.createRadialGradient() — Creates a radial gradient; ctx.fill() — Fills the current path

Source code

frontend/src/demos/kits/kit-physics.js: FAM.pendulum (L431-L473)

Code walkthrough

L433-433: Control definitions (colour/speed/size/count/extras): Bob color, Bob mass m, Pendulum length L, Air damping c, gravity g, Driving force A. L434-434: Preset buttons: name → parameter overrides (6). L435-443: Info-panel content: live rows(v), formula, notes, params, steps. L444-472: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `setLineDash`, `beginPath`, `arc`, `stroke`, `moveTo`, `lineTo` …; math via `Math.min`, `Math.sin`, `Math.cos`, `Math.sqrt`, `Math.atan2`, `Math.max`; returns readouts `px`, `py`, `Lp`, `th`, `om`, `T`, `L` … to the info panel.

Principles

θ'' = −(g/L)·sin θ − (c/m)·θ' + A·cos(Ω t),Ω = √(g/L) Small angular approximation: T = 2 π√ (L) / g) (not related to quality) E = ½·m·L²·ω² + m·g·L·(1 − cos θ) 单摆是最经典的振动系统。重力沿切线方向的分量 −mg·sin θ 把摆锤拉回最低点,所以它来回摆动。摆角很小时, The cycle depends on long swing and gravity , It's not about quality or amplitude——This is what Galileo found. Air blockages slow energy loss; drivers drive it with inherent frequency cycles ( Resonance ), Complement energy, so it won't stop. The block is c/m, The larger the quality, the less afraid to block. Value RK4 (Four-grade dragon–库塔)积分,能量在长时间里也很稳定。按住摆锤可以拖到任意角度松手。

Open the interactive demo · GitHub