Bounce In · How it works & code
Bounce In: After the ball lands, it bounces back using bounce.out easing multiple times, each bounce being lower than the previous one.
Runtime logic
Visiting `/gsap/bounce`, DemoHost lazy-loads `demos/kits/kit-gsap.js` (registry `mod: 'kit-gsap'`) and calls `mount(host, { variant: 'bounce' })`. mount resolves the variant to family `bounce`; `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π; 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.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.ellipse() — Adds an elliptical arc; ctx.fill() — Fills the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); 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.stroke() — Strokes the current path; ctx.lineTo() — Adds a straight line to a point; 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; ctx.translate() — Translates the coordinate system
Source code
frontend/src/demos/kits/kit-gsap.js: FAM.bounce (L255-L271)
Code walkthrough
L255-271: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L255-255: Title shown in the info and control panels: “Bounce In”. L255-255: Range/default of the count slider. L255-255: Extra demo-specific slider/toggle: Drop Height. L255-255: Range/default of the size slider. L255-255: Default main colour. L256-256: Help text of the count parameter. L256-256: Help text of the extra parameter. L256-256: Help text of the size parameter. L257-257: Preset buttons: name → parameter overrides (5). L258-258: Body of the “Notes” section. L259-259: Main formula of the “Formula” section. L259-259: Second formula line. L260-271: Per-frame draw function: draws with Canvas 2D `fillRect`, `beginPath`, `ellipse`, `fill`, `arc`; math via `Math.min`, `Math.hypot`, `Math.round`; returns readouts `mode`, `cell` to the info panel.
Principles
y = ground − (1 - bource.out (p)) · Drop Height gsap.from(".ball", { y: -300, duration: 1, ease: "bounce.out", stagger: 0.12 }) Bounce.out: When a small ball hits the ground, it stops like a real leather ball, and the rhythm of this "bounces" is the bounce.out curve (lower top left angle) -- the curve falls back several times after it touches the top (end). There's a 0.12-second stagger between the balls, so it's going to fall down in turn; then it's going to go back up again. 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.