Three.js Scroll Magnetic Field Lines — DrawRange Reveal Animation

Three.js Scroll Magnetic Field Lines · Scroll · Plain HTML, CSS & JS · Live preview

What's included

Features

Field lines are hand-parameterized arcs (axial lerp plus a sine bulge) rather than a numerically solved physics field
Each line is rendered as a THREE.Line whose geometry.setDrawRange progressively reveals it with zero shader code
Points for every line are computed once via BufferGeometry.setFromPoints, never recomputed per frame
Twenty-six lines swept at different angles around the pole axis approximate a full 3D bar-magnet field pattern
Each line remaps the shared scroll progress into its own staggered local window for a sequential draw-in effect
Camera continuously orbits the pole axis so the 3D field structure reads from constantly shifting angles
Emissive pole materials plus matching point lights make north (red) and south (blue) instantly legible
Fully reversible pinned scroll animation — scrolling up shrinks every line back toward the north pole in staggered reverse

About this UI Snippet

How to Build Scroll-Driven Magnetic Field Lines With Three.js

Screenshot of the Three.js Scroll Magnetic Field Lines snippet rendered live

The Three.js Scroll Magnetic Field Lines snippet renders two glowing pole spheres — red for north, blue for south — connected by dozens of curved field-line arcs that draw themselves progressively, one after another, as the visitor scrolls through a pinned stage, evoking the classic iron-filings diagram of a bar magnet's field, swept around in 3D.

Parametric arcs, not a physics field solver

Rather than numerically integrating actual magnetic field equations, each field line is a hand-parameterized arc: it moves linearly along the pole axis from north to south (axisZ), while bulging outward perpendicular to that axis by sin(u * PI) * bow, where u is the 0-1 position along the line and bow varies per line. Rotating that bulge direction by a different angle for each of the 26 lines sweeps the classic 2D bar-magnet field-line silhouette all the way around the pole axis into a full 3D field pattern — visually convincing without needing to solve Biot-Savart integrals.

One THREE.Line per field line, revealed via drawRange

Each field line's full set of sample points is computed once at startup via BufferGeometry.setFromPoints, then rendered as a THREE.Line. The "stroke draw" reveal effect — each line appearing to trace itself from pole to pole — comes entirely from geometry.setDrawRange(0, count): setting count to a small number renders only the first few points (a short stub near the north pole), and increasing it toward the full point count reveals progressively more of the line's length, with no shader or masking trick required. This is the same drawRange revealing technique used for the path animation in scroll SVG path draw, applied here to a 3D curve instead of a flat SVG path.

Staggered per-line reveal windows for a natural draw-in sequence

Just like the panel stagger in origami crane unfold, each field line remaps the shared scrubbed eased progress into its own local 0-1 window via (eased - start) / span, with start values spread across most of the scroll range by line index. This means lines begin drawing themselves in sequence rather than all growing simultaneously, producing a much more organic "the field is forming" feel than a single uniform reveal would.

Reversible because drawRange is a pure function of progress

Every line's draw count is recomputed fresh from its local eased window every frame — nothing is accumulated frame to frame. Scrolling back up decreases the shared progress value, every line's local window value drops back toward zero, and setDrawRange shrinks each line back toward its stub near the north pole in the same staggered order, reversed.

Orbiting camera reveals the full 3D structure

Because all 26 field lines are swept around the pole axis at different angles, a single static camera view would flatten much of that structure. The camera continuously orbits around the pole axis throughout the whole scroll range, so the viewer sees the field lines' 3D arrangement from constantly shifting angles rather than a single flat silhouette.

Build with AI

Build, Understand, Optimize, and Extend It With AI

You do not need to reverse-engineer how dozens of curved field lines draw themselves in sequence between two magnetic poles. Paste this snippet's HTML, CSS, and JS into an AI assistant like Claude and ask it to explain how BufferGeometry's setDrawRange produces a stroke-draw reveal with no shader, or how the per-line staggered window formula creates a sequential formation effect. The same assistant can help you extend it — ask it to add small glowing particles that travel along each fully-drawn line from pole to pole, vary line color by field strength (denser near the poles), or add a second pair of poles for a more complex multi-magnet field pattern. Treat the code as a conversation starter, not a finished artifact.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a "scroll-scrubbed magnetic field lines" effect in plain HTML, CSS, and JavaScript using Three.js, GSAP, and GSAP's ScrollTrigger plugin, all loaded from a CDN (no bundler, no build step).

Requirements:
- A pinned section containing a full-size canvas, with a WebGLRenderer and PerspectiveCamera sized to the canvas element (not window.innerWidth/innerHeight) and updated on window resize including aspect ratio.
- Create two glowing pole spheres (a red "north" and a blue "south") positioned a fixed distance apart along one axis, each paired with a matching colored point light and an emissive MeshStandardMaterial.
- Generate roughly two dozen field lines once at startup: for each line, sample points along a parameter from 0 to 1 where the point's position lerps linearly along the pole axis while bulging outward perpendicular to that axis by a sine curve (peaking at the midpoint), with the bulge direction rotated by a different angle per line so the full set sweeps around the axis in 3D. Build each line's geometry via BufferGeometry.setFromPoints and render it as a THREE.Line.
- Set every line's initial geometry.setDrawRange(0, 0) so no lines are visible at rest.
- Register a GSAP tween on a ScrollTrigger targeting the pinned section, with pin: true, start at top top, a numeric scrub, and a multi-hundred-percent end, animating a single plain progress value from 0 to 1 with linear easing.
- Every animation frame, apply smoothstep easing to the scrubbed progress, then for each line remap that eased progress into its own local 0-1 window via a per-line start offset (spread across most of the scroll range based on line index) and a shared span, clamp it, and set that line's geometry.setDrawRange(0, localWindow * totalPointCount) so lines visibly draw themselves in a staggered sequence from the north pole toward the south pole.
- Continuously orbit the camera around the pole axis throughout the scroll range so the 3D arrangement of swept field lines is visible from shifting angles rather than one flat silhouette.
- Confirm scrolling back up reverses the entire reveal smoothly: each line's drawn length shrinks back toward its starting stub in the same staggered order, reversed, since draw range is a pure function of the shared progress value with no accumulated state.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<section class="mfl-stage" id="mflStage">
  <div class="mfl-intro-overlay"><p>Scroll ↓ to draw the magnetic field lines between the poles</p></div>
  <canvas id="mflCanvas"></canvas>
  <div class="mfl-hud"><span id="mflPct">0</span>% of field drawn</div>
</section>
<section class="mfl-bottom"><p>The full magnetic field, arcing from pole to pole.</p></section>

Step by step

How to Use

  1. 1
    Load all three CDN scriptsAdd three.min.js, gsap.min.js, and ScrollTrigger.min.js from the CDN panel, in that order.
  2. 2
    Paste HTML, CSS, and JSTwo glowing pole spheres appear with no visible field lines yet, inside a pinned 3D stage with a live "% of field drawn" read-out.
  3. 3
    Scroll downField lines draw themselves from the north pole toward the south pole in a staggered sequence, while the camera orbits.
  4. 4
    Scroll back upField lines shrink back toward the north pole in the same staggered order, reversed, since drawRange is a pure function of progress.
  5. 5
    Retune the fieldChange LINE_COUNT for a denser or sparser field, or the bow formula for tighter or wider arcs.
  6. 6
    Adjust the pacingChange the ScrollTrigger end value (+=450%) or each line's start/span window to retime the staggered draw-in.

Real-world uses

Common Use Cases

Physics and electromagnetism education
Open a physics course or science-museum page with field lines that visibly draw themselves as students scroll.
Electronics and hardware brand sites
A magnetic field visualization suits sensor, motor, or electromagnetics-adjacent hardware product pages.
Generative and technical-diagram art portfolios
Demonstrate drawRange-based stroke reveal technique with a physically-inspired 3D diagram.
Scroll-story chapter breaks
Use the field draw-in as a mid-page transition, similar in spirit to the scroll tunnel bridge.
Data-visualization style guides
Reference implementation for progressive-reveal stroke animation applied to any curve-based 3D diagram.
Sci-fi or puzzle game menus
A pulsing dual-pole field diagram reads well as an ambient title-screen or tech-tree background element.

Got questions?

Frequently Asked Questions

No. Each line is a hand-parameterized arc — linear travel along the pole axis combined with a sine-shaped bulge perpendicular to it — chosen because it visually resembles the classic bar-magnet field-line pattern seen in iron-filings diagrams. It is a deliberate, cheap approximation rather than a numerical solution to Biot-Savart or Maxwell's equations, which would be unnecessary computational cost for a decorative visualization.

Every field line's full set of sample points is computed once and stored in a THREE.BufferGeometry. THREE.Line renders only the vertex range specified by geometry.setDrawRange(start, count) — by animating count from 0 up to the line's total point count, the line visibly grows from its first point (near the north pole) to its last (near the south pole), entirely through this built-in draw-range mechanism with no shader or clipping-plane trick required.

Every line remaps the single shared scrubbed progress value into its own local 0-1 window via (eased - start) / span, where start is spread across roughly the first 70% of the scroll range based on the line's index. This means lines begin their draw-in at different scroll positions, producing a sequential, organic-feeling formation of the field rather than all 26 lines growing in perfect lockstep.

Yes. Each line's draw count is recomputed fresh every frame purely from its local eased window value, with no state carried between frames. Scrolling up decreases the shared progress value, every line's local window value drops back toward zero, and setDrawRange shrinks each line back toward its starting stub in the same staggered order, reversed.

Yes. Click JSX for a React component, Vue for a Vue 3 SFC, Angular for a standalone component, or Tailwind for a React + Tailwind version. Build the pole meshes, field-line geometries, and GSAP ScrollTrigger inside a mount effect keyed to a canvas ref, and on unmount kill the ScrollTrigger instance, dispose geometries/materials, and call renderer.dispose().