Parallax Layers · How it works & code
When driving, nearby trees rush backward quickly, while distant mountains move slowly — the brain uses this speed difference to judge distance.
Runtime logic
Visiting `/gsap/parallax-g`, DemoHost lazy-loads `demos/kits/kit-gsap.js` (registry `mod: 'kit-gsap'`) and calls `mount(host, { variant: 'parallax-g' })`. mount resolves the variant to family `parallax-g`; `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°); 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.createLinearGradient() — Creates a linear gradient; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.closePath() — Closes the current sub-path; ctx.arcTo() — Adds an arc using tangent lines; ctx.stroke() — Strokes the current path; 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.parallax-g (L405-L419)
Code walkthrough
L405-419: Calls the factory `gFam()` to produce the full definition (spec + doc + build) from the options below. L405-405: Title shown in the info and control panels: “Parallax Layers”. L405-405: Range/default of the count slider. L405-405: Extra demo-specific slider/toggle: Parallax intensity. L405-405: Range/default of the size slider. L405-405: Default main colour. L406-406: Help text of the count parameter. L406-406: Help text of the extra parameter. L406-406: Help text of the size parameter. L407-407: Preset buttons: name → parameter overrides (5). L408-408: Body of the “Notes” section. L409-409: Main formula of the “Formula” section. L409-409: Second formula line. L410-419: Per-frame draw function: draws with Canvas 2D `createLinearGradient`, `fillRect`, `beginPath`, `arc`, `fill`, `moveTo` …; math via `Math.min`, `Math.sin`, `Math.round`; returns readouts `mode`, `cell` to the info panel.
Principles
Layer speed = 1 - strength · (n − 1 − i) / n(i = n − 1 is the nearest level) gsap.to (layer, {x: - distance) · Speed, please: "none", scrollTrigger: {scrub: true}) Discrepancies: The trees that are in the vicinity of the car are retreating and the mountains that are far away are moving, and the brain is so close. How many layers do you divide?, The lighter the color, the deeper it gets. The image moves automatically to the right, and the pointer moves right or right after the image. 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.