webdevpuneet.com

Three.js Scroll Gear Train — GSAP Mechanical Ratios

Three.js Scroll Gear Train Mechanism · Scroll · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<section class="gtr-stage" id="gtrStage">
  <div class="gtr-intro"><p>Scroll ↓ to turn the machine</p></div>
  <canvas id="gtrCanvas"></canvas>
  <div class="gtr-hud">DRIVE <span id="gtrRot">0</span>°</div>
</section>
<section class="gtr-bottom"><p>Every tooth in mesh.</p></section>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Fully procedural gears: disc, rim teeth, hub, and spoke cutouts from primitives — tooth count is a real parameter
Exact spur-gear kinematics: angles chain through teethPrev/teethCurrent ratios, flipping direction at each mesh
Half-tooth phase offsets (π/teeth) on alternating gears so teeth interleave without clipping
Scroll scrubs the driver angle itself (0 → 4π) — scrolling is cranking, and reverse runs the machine backwards
Tooth width derived from circumference so any radius/teeth combo yields plausible pitch
Camera dollies along the train following power flow, ending on the fastest gear
Brass material from high-metalness MeshStandardMaterial under warm key and amber point lights
Live HUD converting the scrubbed driver angle to degrees

About this UI Snippet

How to Build a Scroll-Driven Gear Train With Three.js and GSAP

Screenshot of the Three.js Scroll Gear Train Mechanism snippet rendered live

The Three.js Scroll Gear Train Mechanism snippet builds five interlocking brass gears procedurally — disc, teeth, hub, and spoke cutouts — and lets scroll act as the crank: GSAP's ScrollTrigger scrubs the driver gear's angle, and every downstream gear derives its rotation through the real tooth-count ratios, counter-rotating at each mesh point exactly as physical gears would. The scene reads as a machine because the math underneath is the machine's math.

Procedural gears from primitives

Each gear is a Group: a CylinderGeometry disc rotated flat, N tooth boxes placed around the rim at (cos a, sin a) × (r + 0.1r) with each tooth rotated a + π/2 to point radially outward, a smaller dark hub cylinder, and four dark wedge boxes that fake spoke cutouts on the face. Building gears in code rather than loading a model means tooth count is a real parameter — and tooth count is exactly what the animation math needs. Tooth width derives from circumference, (2πr / teeth) × 0.45, so any radius/teeth combination produces a plausible gear with correct-looking pitch.

Real gear ratios, not decorative spinning

Most gear animations on the web rotate everything at arbitrary speeds and hope nobody looks closely. Here each gear's angle is computed by chaining angle × (teethPrev / teethCurrent) down the train while flipping direction at every mesh — the actual kinematics of spur gears. The 24-tooth driver turning twice (4π across the scroll) spins the 10-tooth fourth gear nearly five times in the opposite direction. Because ratios are exact and both gears in each pair carry a half-tooth phase offset (π / teeth on alternating gears), teeth visibly interleave through the mesh point instead of clipping through each other — the detail that sells the whole illusion.

Scroll as the crank handle

The single scrubbed value here is not an abstract progress fraction but the driver's angle itself, tweened from 0 to 4π with ease: 'none'. That framing matters: scrolling *is* cranking, at a fixed mechanical advantage, and scrolling backwards runs the machine in reverse — which is free, since every gear's angle is a pure function of the driver's. The HUD converts the same value to degrees, so users see DRIVE 720° at full scroll. This is the purest expression of the derive-everything pattern used across this series, from the portal gate sequence to the Rubik's cube assembly.

A dolly that follows power through the train

The camera starts on the big slow driver and dollies rightward along the zigzagging train as the drive angle accrues, ending close on the smallest, fastest gear — following the power flow, like a documentary camera would. Position and look-at both derive from normalized drive progress, with a small clock-time bob so the shot never feels locked off. Warm directional and amber point lights against high-metalness, mid-roughness MeshStandardMaterial give the brass look; fog matched to the dark background swallows the train's far end.

Where to take it

Because ratios are parameter-driven, the train doubles as a live mechanism diagram: change any teeth value and both the geometry and the motion update coherently. Add a chain of number counters geared to each wheel for an odometer effect, or pair it with the clockwork feel of scroll timeline dots for process storytelling. For a more organic machine aesthetic, the fabric ripple snippet shows the same scrub philosophy applied to soft surfaces instead of rigid bodies.

Step by step

How to Use

  1. 1
    Load the three CDN scriptsAdd three.min.js, gsap.min.js, and ScrollTrigger.min.js in that order before the snippet JS.
  2. 2
    Paste HTML, CSS, and JSA pinned zigzag train of five brass gears sits motionless under warm light with a DRIVE 0° HUD.
  3. 3
    Scroll to crankThe big driver turns with your scroll; each downstream gear counter-rotates at its exact tooth ratio — the small 10-tooth gear visibly races.
  4. 4
    Watch the mesh pointsHalf-tooth phase offsets keep teeth interleaving cleanly through each contact point rather than clipping.
  5. 5
    Scroll back upThe machine runs in reverse — every gear angle is a pure function of the driver angle, so backwards is free.
  6. 6
    Re-gear the trainEdit the specs array: radius, teeth, and color per gear. Ratios, spacing, and phase offsets all recompute from teeth counts automatically.

Real-world uses

Common Use Cases

Engineering and manufacturing sites
A mechanism that obeys real ratios signals precision better than any tagline — ideal for machining, robotics, and industrial automation firms.
"How it works" process sections
Map each gear to a pipeline stage and pair with pinned copy via scroll pin steps — the ratio metaphor (small input, big output) writes itself.
Teaching gear kinematics interactively
Scrub-as-crank makes ratio math tangible: students see a 24:10 ratio as visibly different speeds, forwards and backwards.
Steampunk and craft-brand aesthetics
Brass materials and exposed mechanisms suit watchmakers, distilleries, and heritage brands — a warmer palette than the neon of synthwave terrain.
Puzzle and automation game promos
Factorio-adjacent games can showcase mechanism-building with an interactive train visitors crank themselves.
Kinetic sculpture pages
The train idles as sculpture and becomes machine on scroll, complementing particle pieces like magnetic particles.

Got questions?

Frequently Asked Questions

The frame loop walks the train carrying an angle: for each gear after the driver it multiplies by teethPrev / teethCurrent and flips sign, which is the physical law of spur gears (equal tooth velocity at the contact point). Because every angle derives from the single scrubbed driver angle, the gears can never drift out of mesh no matter how fast or how far the user scrolls in either direction.

Alternating gears start with a rotation offset of π / teeth — half a tooth pitch. With that phase shift, one gear's tooth aligns with its neighbor's gap at the contact point, and since the ratio math advances both at matched surface speed, the interleave is preserved through the whole rotation. Without the offset the ratios would still be correct, but teeth would sit tip-to-tip and pass through each other.

It removes one layer of indirection: the tween IS the crank. Tweening angle from 0 to 4π means the HUD is a unit conversion, gear angles are ratio multiplications, and camera progress is a normalization — every consumer derives what it needs. A 0–1 value would work identically but every use site would multiply by 4π first.

makeGear() assembles a Group from primitives: a flat CylinderGeometry disc, N BoxGeometry teeth placed around the rim at angle a with rotation a + π/2 so they point radially, a narrow hub cylinder, and four dark wedges suggesting spoke cutouts. Tooth width is (2πr / teeth) × 0.45, derived from circumference, so pitch looks right for any parameter combination — and one Group rotation.z turns the entire assembly.

Yes. Use the JSX, Vue, Angular, or Tailwind export buttons. Build the gears and ScrollTrigger inside a mount effect against a canvas ref, update the HUD through a ref rather than state, and on cleanup kill the ScrollTrigger, traverse each gear group disposing geometries and materials, and call renderer.dispose() to release the WebGL context and unpin the section.