Components61 / 118
Spiral Deck
A deck of cards that uncoils into a 3D spiral as you scroll. It starts as one neat stack; the cards sweep out one after another along a curving arc and open into a vortex, small and far at its heart and large and close at its rim. Keep scrolling and it turns, then the camera flies into it until the cards surround you. The camera leans after the cursor and the card under it lifts. Or let it loop on its own.
Scroll-drivenScroll-drivenThree.jsThree.js3D spiral3D spiralCard deckCard deckFly-throughFly-throughGSAPGSAP
Scroll to uncoil the deck
Tweak
LIVELIVEMotion
Driven by
Spiral
1.55
Fly into it at the end
Usage
Follows your tweaks.
usage.tsx
import { SpiralDeck } from "@/components/inreact/spiral-deck";
const cards = [
{ src: "/work/01.jpg" },
{ src: "/work/02.jpg" },
// ...15-25 cards; images, or { draw: (ctx, w, h) => { ... } } to paint one
];
// pins in the page; scrolling uncoils the deck, turns the spiral and flies into it
export function Showreel() {
return <SpiralDeck cards={cards} />;
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/spiral-deck.jsonOr copy the source
Install the dependencies, then save this as components/inreact/spiral-deck.tsx.
terminal
npm install gsap threecomponents/inreact/spiral-deck.tsx
"use client";
import { gsap } from "gsap";
import * as THREE from "three";
import { useEffect, useRef, type CSSProperties } from "react";
/**
* Spiral Deck: a deck of cards that uncoils into a 3D spiral as you scroll.
*
* It starts as one neat stack. Scroll and the deck sweeps out along a curving
* arc, card after card, and opens into a vortex: small and far at its heart,
* large and close at its rim. Keep scrolling and it turns, then the camera
* flies into it until the cards surround you. Each card follows its own lagged
* path, so the deck fans like a hand of cards rather than sliding. The camera
* leans after the cursor and the card under it lifts. Three.js + GSAP.
*
* <SpiralDeck cards={[{ src: "/a.jpg" }, { src: "/b.jpg" }, ...]} /> // pinned in the page
* <SpiralDeck cards={cards} scroll="self" className="h-[640px]" /> // its own scroll box
* <SpiralDeck cards={cards} mode="loop" className="h-[640px]" /> // plays on its own
*
* Cards can be images or anything you draw: pass `draw` to paint a card on a canvas.
*/
export interface SpiralCard {
/** Image for the card face. */
src?: string;
/** Or paint the face yourself (canvas is 512 x 640). */
draw?: (ctx: CanvasRenderingContext2D, w: number, h: number) => void;
alt?: string;
}
export interface SpiralDeckProps {
cards: SpiralCard[];
/** "scroll": scrolling drives it; "loop": it plays on its own. */
mode?: "scroll" | "loop";
/** With mode="scroll": pin in the page, or scroll inside its own box. */
scroll?: "page" | "self";
/** Scroll length, in viewport (or box) heights. */
length?: number;
/** How many turns the spiral makes. */
turns?: number;
/** Fly into the spiral at the end. */
flyThrough?: boolean;
className?: string;
}
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const clamp01 = (x: number) => Math.min(1, Math.max(0, x));
const smooth = (a: number, b: number, x: number) => {
const t = clamp01((x - a) / (b - a));
return t * t * (3 - 2 * t);
};
const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
// rounded-corner alpha, shared by every card
function roundedAlpha(w: number, h: number, r: number) {
const c = document.createElement("canvas");
c.width = w;
c.height = h;
const g = c.getContext("2d")!;
g.fillStyle = "#000";
g.fillRect(0, 0, w, h);
g.fillStyle = "#fff";
g.beginPath();
g.roundRect(2, 2, w - 4, h - 4, r);
g.fill();
const t = new THREE.CanvasTexture(c);
t.colorSpace = THREE.NoColorSpace;
return t;
}
// cover-fit an image into the card canvas
function coverDraw(g: CanvasRenderingContext2D, img: HTMLImageElement, w: number, h: number) {
const ia = img.width / img.height, ca = w / h;
let sw = img.width, sh = img.height, sx = 0, sy = 0;
if (ia > ca) {
sw = img.height * ca;
sx = (img.width - sw) / 2;
} else {
sh = img.width / ca;
sy = (img.height - sh) / 2;
}
g.drawImage(img, sx, sy, sw, sh, 0, 0, w, h);
}
export function SpiralDeck({ cards, mode = "scroll", scroll = "page", length = 4, turns = 1.55, flyThrough = true, className }: SpiralDeckProps) {
const outerRef = useRef<HTMLDivElement>(null);
const hostRef = useRef<HTMLDivElement>(null);
const n = cards.length;
const key = cards.map((c, i) => c.src ?? `draw${i}`).join(",");
useEffect(() => {
const outer = outerRef.current, host = hostRef.current;
if (!outer || !host || n === 0) return;
const still = reduced();
const canvas = document.createElement("canvas");
canvas.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:block;";
host.appendChild(canvas);
let renderer: THREE.WebGLRenderer;
try {
renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true });
} catch {
canvas.remove();
return;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(48, 1, 0.05, 60);
const rig = new THREE.Group(); // the camera leans inside this
rig.add(camera);
scene.add(rig);
// cards
const CW = 512, CH = 640;
const alpha = roundedAlpha(256, 320, 26);
const geo = new THREE.PlaneGeometry(1, CH / CW);
const aniso = renderer.capabilities.getMaxAnisotropy();
type Card = { mesh: THREE.Mesh; mat: THREE.MeshBasicMaterial; tex: THREE.CanvasTexture; t: number; jitter: number; lift: number };
const deck: Card[] = cards.map((c, i) => {
const cv = document.createElement("canvas");
cv.width = CW;
cv.height = CH;
const g = cv.getContext("2d")!;
g.fillStyle = "#1a1a1c";
g.fillRect(0, 0, CW, CH);
const tex = new THREE.CanvasTexture(cv);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = aniso;
if (c.draw) {
c.draw(g, CW, CH);
tex.needsUpdate = true;
} else if (c.src) {
const img = new Image();
img.decoding = "async";
img.onload = () => {
coverDraw(g, img, CW, CH);
tex.needsUpdate = true;
};
img.src = c.src;
}
const mat = new THREE.MeshBasicMaterial({ map: tex, alphaMap: alpha, transparent: true, depthWrite: false, side: THREE.DoubleSide });
const mesh = new THREE.Mesh(geo, mat);
scene.add(mesh);
// a stable per-card wobble, so the stack and the spiral look hand-placed
const jitter = Math.sin(i * 12.9898) * 0.5;
return { mesh, mat, tex, t: n > 1 ? i / (n - 1) : 0, jitter, lift: 0 };
});
let W = 1, H = 1;
const resize = () => {
const r = host.getBoundingClientRect();
W = Math.max(1, r.width);
H = Math.max(1, r.height);
renderer.setSize(W, H, false);
camera.aspect = W / H;
// portrait boxes: pull back so the spiral still fits the width
camera.position.z = 6.2 * Math.max(1, 0.85 / camera.aspect);
camera.updateProjectionMatrix();
};
resize();
const baseZ = () => 6.2 * Math.max(1, 0.85 / camera.aspect);
const ro = new ResizeObserver(resize);
ro.observe(host);
// every card rides the same spiral path; the chain unrolls like a slinky as it opens,
// so the deck stays packed and fans along the curve instead of scattering
const tmp = new THREE.Vector3();
const place = (c: Card, open: number, spin: number) => {
const u = c.t * open; // how far along the spiral this card has travelled
const theta = spin + u * turns * Math.PI * 2 + Math.PI * 0.5;
const r = 0.12 + Math.pow(u, 1.05) * 4.2;
// while packed, the deck keeps its order: the first card (the heart of the spiral) on top
const z = -2.2 + u * 4.6 + (1 - c.t) * 0.7 * (1 - open) * (1 - open);
tmp.set(Math.cos(theta) * r, Math.sin(theta) * r, z);
c.mesh.position.copy(tmp);
// cards turn to follow the ring as they leave the stack, tipping a little while the chain is moving
const follow = smooth(0, 0.12, u);
const tip = Math.sin(Math.PI * open) * 0.32;
c.mesh.rotation.set(-tip * (1 - u), tip * 0.4 * (1 - u), follow * (theta - Math.PI / 2 + c.jitter * 0.3));
const s = 1 + c.lift * 0.08;
c.mesh.scale.set(s, s, s);
c.mesh.renderOrder = Math.round((c.mesh.position.z + 10) * 1000);
};
// drive: scroll or loop
const self = mode === "scroll" && scroll === "self";
const progress = () => {
if (mode !== "scroll") return 0;
if (self) {
const max = outer.scrollHeight - outer.clientHeight;
return max > 0 ? outer.scrollTop / max : 0;
}
const r = outer.getBoundingClientRect();
const max = r.height - window.innerHeight;
return max > 0 ? clamp01(-r.top / max) : 0;
};
const st = { p: 0, clock: 0 };
let loop: gsap.core.Timeline | null = null;
if (mode === "loop") {
const o = st;
loop = gsap.timeline({ repeat: -1, repeatDelay: 0.6 });
loop.to(o, { p: 0.62, duration: 5.5, ease: "power2.inOut", delay: 0.8 }).to(o, { p: flyThrough ? 1 : 0.7, duration: 3.2, ease: "power2.in" }).to(o, { p: 0, duration: 2.2, ease: "power3.inOut" });
if (still) loop.pause(0);
}
// cursor: the camera leans after it, and the card under it lifts
const lean = { x: 0, y: 0 };
const lx = gsap.quickTo(lean, "x", { duration: 1.1, ease: "power3.out" });
const ly = gsap.quickTo(lean, "y", { duration: 1.1, ease: "power3.out" });
const ray = new THREE.Raycaster();
const ndc = new THREE.Vector2(9, 9);
const onMove = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
const x = ((e.clientX - r.left) / r.width) * 2 - 1;
const y = -(((e.clientY - r.top) / r.height) * 2 - 1);
ndc.set(x, y);
if (!still) {
lx(x);
ly(y);
}
};
const onLeave = () => {
ndc.set(9, 9);
lx(0);
ly(0);
};
host.parentElement?.addEventListener("pointermove", onMove);
host.parentElement?.addEventListener("pointerleave", onLeave);
const scroller: HTMLElement | Window = self ? outer : window;
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting));
io.observe(outer);
const tick = (_t: number, dms: number) => {
const dt = Math.min(0.05, dms / 1000);
if (mode === "scroll") {
const target = progress();
st.p += (target - st.p) * (still ? 1 : 1 - Math.exp(-dt * 5.5));
}
st.clock += dt;
if (!visible) return;
const P = st.p;
// the story: stack -> uncoil -> turn -> fly in
const spread = easeInOut(smooth(0.03, 0.55, P));
const spin = P * Math.PI * 1.6 + (still ? 0 : st.clock * 0.05 * spread);
const fly = flyThrough ? smooth(0.68, 1, P) : 0;
camera.position.z = baseZ() - fly * (baseZ() - 0.35);
camera.rotation.z = fly * 0.6;
rig.rotation.y = lean.x * 0.12;
rig.rotation.x = -lean.y * 0.08;
// which card is under the cursor
let hit: THREE.Object3D | null = null;
if (ndc.x < 2) {
ray.setFromCamera(ndc, camera);
const hits = ray.intersectObjects(deck.map((d) => d.mesh), false);
hit = hits.length ? hits[0].object : null;
}
for (const c of deck) {
const goal = c.mesh === hit && !still ? 1 : 0;
c.lift += (goal - c.lift) * Math.min(1, dt * 9);
place(c, spread, spin);
}
renderer.render(scene, camera);
};
gsap.ticker.add(tick);
return () => {
gsap.ticker.remove(tick);
loop?.kill();
io.disconnect();
ro.disconnect();
host.parentElement?.removeEventListener("pointermove", onMove);
host.parentElement?.removeEventListener("pointerleave", onLeave);
void scroller;
deck.forEach((c) => {
c.mat.dispose();
c.tex.dispose();
});
geo.dispose();
alpha.dispose();
renderer.dispose();
canvas.remove();
};
// cards are read once per set; rebuild when they change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, mode, scroll, turns, flyThrough]);
const isScroll = mode === "scroll";
const self = isScroll && scroll === "self";
return (
<div
ref={outerRef}
role="img"
aria-label="A deck of cards that uncoils into a spiral"
className={["relative w-full", self ? "h-full overflow-y-auto overscroll-contain [scrollbar-width:none] [&::-webkit-scrollbar]:hidden" : "", !isScroll ? "h-full" : "", className].filter(Boolean).join(" ")}
style={isScroll && !self ? { height: `${length * 100}vh` } : undefined}
>
{isScroll ? (
<div className={self ? "relative" : "h-full"} style={self ? { height: `${length * 100}%` } : undefined}>
<div className={self ? "sticky top-0" : "sticky top-0 h-screen"} style={self ? ({ height: `${100 / length}%` } as CSSProperties) : undefined}>
<div ref={hostRef} className="absolute inset-0" />
</div>
</div>
) : (
<div ref={hostRef} className="absolute inset-0" />
)}
</div>
);
}
export default SpiralDeck;
One prompt, any AI agent
Paste into Claude Code, Codex, Cursor or Windsurf. It carries your tweaks and the full source, so the agent installs it exactly.
Props
7 propscards{ src?; draw?; alt? }[]The cards: an image, or a draw(ctx, w, h) function that paints the face (512 x 640).mode"scroll" | "loop"Scroll drives it, or it plays on its own.default "scroll"default "scroll"scroll"page" | "self"With mode scroll: pin in the page, or scroll inside its own box.default "page"default "page"lengthnumberScroll length, in viewport heights.default 4default 4turnsnumberHow many turns the spiral makes.default 1.55default 1.55flyThroughbooleanFly into the spiral at the end.default truedefault trueclassNamestringExtra classes (give it a height with mode loop).