作者如何用 Astro、React Three Fiber、GSAP 和 Lenis 打造 3D 捲動作品集
How I built a 3D scroll portfolio with Astro, React Three Fiber, GSAP and Lenis
產品設計師 nishar multani 使用 Astro、React Three Fiber、Three.js、GSAP、Lenis 和 Tailwind 打造 3D 捲動作品集,並託管於 Cloudflare Pages。
I'm a product designer, and my portfolio is the one project where nobody can tell me what to cut. So I built it as a scroll experience: a hero that morphs into a 3D screen, a cube that turns between case studies, an About card hanging on ropes that you can tilt, drag and stretch, and a work grid you can throw around.
Live: nishar.xyz
It is built with Astro for the pages, React islands with React Three Fiber and Three.js for the 3D, GSAP (ScrollTrigger, Draggable, Inertia) and Lenis for motion, and Tailwind for layout. It is hosted on Cloudflare Pages. I designed it in Figma first, then built it with AI-assisted development (Claude Code, Codex and Cursor) so I could test how it felt in the browser instead of guessing from a prototype.
Making it look right was the easy part. Making it feel right at 60 fps took much longer. These are the problems that cost me the most time.
1. WebGL competed with the first paint
If every Three.js scene starts at page load, shader compilation and texture uploads land at exactly the moment the hero intro plays, and the intro stutters.
The fix was a small gate. WebGL scenes wait until the page has loaded, the hero intro has had time to play (about 2.6 seconds), and the main thread is idle:
export const whenIdle: Promise<void> = new Promise((resolve) => {
const settle = () =>
setTimeout(
() => ('requestIdleCallback' in window
? requestIdleCallback(() => resolve(), { timeout: 3000 })
: resolve()),
3000
);
if (document.readyState === 'complete') settle();
else addEventListener('load', settle, { once: true });
});
Every heavy scene awaits whenIdle before it starts. Nothing visual changed, but the first seconds stopped competing with the 3D.
2. "Off-screen" canvases kept rendering
I paused scenes with IntersectionObserver, but that only tells you whether an element is inside the viewport. A canvas that GSAP has faded to opacity: 0, or that sits in a hidden chapter, still counts as intersecting, so it kept rendering for nothing.
I wrote a paintGate that asks whether the element can actually be seen right now, using checkVisibility:
painted = el.checkVisibility({ opacityProperty: true, visibilityProperty: true });
Each render loop calls it every frame, but the style check itself runs at most every 100 ms, so it is cheap. Layers that are faded out or hidden now stop rendering entirely.
3. Images stalled scrolling
The work grid is a WebGL gallery. Chrome decodes an <img> synchronously on the main thread when you upload it to WebGL, which costs roughly 10 to 30 ms per image, and the galleries load several at once. That adds up to dropped frames.
The fix has two parts:
- Decode off the main thread with
fetchandcreateImageBitmap(blob), and resize during the decode. - Spread the texture uploads out so only one happens per frame.
If createImageBitmap isn't available, the code falls back to a plain <img>.
4. Smooth scroll that trailed my hand
I started with Lenis using a fixed duration of 1.8 seconds, which looks silky in a demo but makes the page trail behind the wheel.
I switched to Lenis's frame-rate-independent lerp (0.11). Each wheel tick now moves the page immediately and it settles in about 0.6 seconds, so the page follows the hand instead of trailing it. Lenis is driven by the GSAP ticker so ScrollTrigger and the smooth scroll stay on the same clock, and touch scrolling is left native on phones.
5. A physics card that behaves the same on every machine
The About card hangs from four ropes. If the physics is tied to frame rate, it feels different on a 60 Hz laptop and a 120 Hz display. I used a fixed-step spring simulation (1/120 second per step) that has no DOM, renderer or frame-rate-dependent interpolation, so the same drag gives the same stretch everywhere.
On phones I removed the tilt, drag and stretch completely. The card stays on fixed ropes and touches scroll the page, because fighting the scroll gesture on a phone is a bad trade for a fun effect.
6. Small things that added up
- A 3D model that was too big: I compressed the studio display model with meshopt, from 3.1 MB to 0.8 MB.
- Fonts and images: I self-hosted the fonts as woff2 and served responsive WebP variants instead of one large image.
-
Reduced motion: the site respects
prefers-reduced-motion. Smooth scroll is switched off for those users. - Pinned build: I pinned the Node version so Cloudflare Pages builds are repeatable.
What I'd tell someone starting
Build the motion you love, then budget as much time for performance as for the design. The visible effects are the easy part. The hard part is making sure they don't cost more than the user can feel.
If you try the site, I'd like feedback on how it performs on your device: nishar.xyz.
來源:DEV Community · dev.to




