Elastic Scale · How it works & code
Like a rubber band pulled and released, elements expand past their target size and bounce back and forth before settling.
Runtime logic
Visiting `/gsap/elastic`, DemoHost lazy-loads `demos/kits/kit-gsap.js` (registry `mod: 'kit-gsap'`) and calls `mount(host, { variant: 'elastic' })`. mount resolves the variant to family `elastic`; `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.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.fillText() — Draws filled text; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.lineTo() — Adds a straight line to a point; ctx.fillRect() — Fills a rectangle; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.rect() — Adds a rectangle to the path; ctx.clip() — Turns the current path into the clipping region
Source code
frontend/src/demos/kits/kit-gsap.js: FAM.elastic (L324-L337)
Code walkthrough
L324-337: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L324-324: Title shown in the info and control panels: “Elastic Scale”. L324-324: Range/default of the count slider. L324-324: Extra demo-specific slider/toggle: Elastic Amplitude. L324-324: Range/default of the size slider. L324-324: Default main colour. L325-325: Help text of the count parameter. L325-325: Help text of the extra parameter. L325-325: Help text of the size parameter. L326-326: Preset buttons: name → parameter overrides (5). L327-327: Body of the “Notes” section. L328-328: Main formula of the “Formula” section. L328-328: Second formula line. L329-337: Per-frame draw function: draws with Canvas 2D `setLineDash`, `beginPath`, `arc`, `stroke`, `fill`; math via `Math.max`, `Math.hypot`, `Math.round`; returns readouts `mode`, `cell` to the info panel.
Principles
elastic.out(a, p):e(t) = a · 2^(−10t) · sin((t − s) · 2π / p) + 1,p = 0.3 gsap.to(el, { scale: 1, duration: 1.2, ease: "elastic.out(1.2, 0.3)", stagger: 0.15 }) E Standing elastic.out: like pulling out the rubber band and letting go, the elements enforce their size, I don't know what to do. The small map of the upper left corner is this slow curve: the axis is time, the axis is progress, and the section above the top of the curve is "over 106." The greater the amplitude, the further away. 85. After that, use the flat power Indicator 2.inout Globally 1267, replay. 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.