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

Spin Out · How it works & code

Shrink while spinning the squares, make it transparent until it disappears ease: power3.in,Turn faster) and use it from a very small state bac…

Runtime logic

Visiting `/gsap/spin-out`, DemoHost lazy-loads `demos/kits/kit-gsap.js` (registry `mod: 'kit-gsap'`) and calls `mount(host, { variant: 'spin-out' })`. mount resolves the variant to family `spin-out`; `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.setLineDash() — Sets the dash pattern for strokes; ctx.stroke() — Strokes the current path; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.translate() — Translates the coordinate system; ctx.rotate() — Rotates the coordinate system; ctx.scale() — Scales the coordinate system; ctx.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.restore() — Restores the saved drawing state; 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.rect() — Adds a rectangle to the path

Source code

frontend/src/demos/kits/kit-gsap.js: FAM.spin-out (L309-L322)

Code walkthrough

L309-322: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L309-309: Title shown in the info and control panels: “Spin Out”. L309-309: Range/default of the count slider. L309-309: Extra demo-specific slider/toggle: Circles 3,. L309-309: Range/default of the size slider. L309-309: Default main colour. L310-310: Help text of the count parameter. L310-310: Help text of the extra parameter. L310-310: Help text of the size parameter. L311-311: Preset buttons: name → parameter overrides (5). L312-312: Body of the “Notes” section. L313-313: Main formula of the “Formula” section. L313-313: Second formula line. L314-322: Per-frame draw function: draws with Canvas 2D `setLineDash`, `stroke`, `save`, `translate`, `rotate`, `scale` …; math via `Math.max`, `Math.round`; returns readouts `mode`, `cell` to the info panel.

Principles

Out: Rotation = Circles · 360° · power3.in(p),scale = 1 − power3.in(p) tl.to(box, { rotation: 360, scale: 0, opacity: 0, ease: "power3.in" }).to(box, { scale: 1, rotation: 0, ease: "back.out(2)" }) Roll-out: roll-out of squares, Transparent until it disappears (ease: power3.in, faster) and then from a very small state, back.out bounces back to the original size - theback slows slightly beyond the target before coming back, so there's a little "ball" feeling. Each square compares to the previous 0.1 second. The dashed line frame is the original size of the square. There's no load here GSAP 库,而是用几十行代码复刻了它最核心的几个概念:补间(tween,从 A Smooth transition of value B Value, slow motion ease,Control the slow pace of transition, the time axis timeline,Sort multi-part patches in order) and staggered ( stagger,让一组元素依次出发)。每一帧都直接根据「当前时间」算出画面,所以拖动右侧滑条会立即看到变化。公式区给出了对应的 GSAP In writing, you can move directly to the real project.

Open the interactive demo · GitHub