Grid Stagger · How it works & code
The stagger grid and from options calculate delays based on distance from the start, creating a ripple-like animation from the origin...
Runtime logic
Visiting `/gsap/grid-stagger`, DemoHost lazy-loads `demos/kits/kit-gsap.js` (registry `mod: 'kit-gsap'`) and calls `mount(host, { variant: 'grid-stagger' })`. mount resolves the variant to family `grid-stagger`; `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; 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.fill() — Fills the current path; 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.stroke() — Strokes the current path; 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; ctx.translate() — Translates the coordinate system; ctx.restore() — Restores the saved drawing state
Source code
frontend/src/demos/kits/kit-gsap.js: FAM.grid-stagger (L421-L435)
Code walkthrough
L421-435: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L421-421: Title shown in the info and control panels: “Grid Stagger”. L421-421: Range/default of the count slider. L421-421: Extra demo-specific slider/toggle: Stagger gap. L421-421: Range/default of the size slider. L421-421: Default main colour. L422-422: Help text of the count parameter. L422-422: Help text of the extra parameter. L422-422: Help text of the size parameter. L423-423: Preset buttons: name → parameter overrides (5). L424-424: Body of the “Notes” section. L425-425: Main formula of the “Formula” section. L425-425: Second formula line. L426-435: Per-frame draw function: draws with Canvas 2D `fill`; math via `Math.floor`, `Math.hypot`, `Math.max`, `Math.min`, `Math.abs`, `Math.round`; returns readouts `mode`, `n`, `cell` to the info panel.
Principles
Delay = distance to starting point (g) · each gsap.to(".cell", { scale: 0.4, yoyo: true, repeat: 1, stagger: { grid: "auto", from: "center", each: 0.06 } }) Grid stagger stagger's grid and from the option allows delay to be measured by "the distance from the starting point" and the animation spreads out from the starting circle like a wave. Default begins at the centre; the pointer is placed in the grid, and the starting point becomes the grid in which the pointer is located. Each grid will be reduced, lightened slightly and restored to its original condition; the starting point will be moved smoothly to the pointer. 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.