Components63 / 118
Flip Wheel
A rolodex of cards that flips one at a time. Portrait cards stand on a horizontal drum; the ones still to come lean back behind the front card, so their tops peek out as a stack of colour strips, and each step the front card tips forward over the hinge and lies down toward you while the stack moves up one. It flips on its own; a wheel, a drag or a click turns it by hand, and the drum leans after the cursor.
RolodexRolodexThree.jsThree.jsCard flipCard flipAutoplayAutoplayDrag & wheelDrag & wheelGSAPGSAP
Click, drag or scroll to flipMove: the drum leans
Tweak
LIVELIVEMotion
Flip on its own
0.7s
Drum
7.5°
Usage
Follows your tweaks.
usage.tsx
import { FlipWheel } from "@/components/inreact/flip-wheel";
const cards = [
{ src: "/work/01.jpg" },
{ src: "/work/02.jpg" },
// ...8-20 portrait images, or { draw: (ctx, w, h) => { ... } } to paint one
];
export function Showcase() {
return <FlipWheel cards={cards} className="h-[80vh]" />;
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/flip-wheel.jsonOr copy the source
Install the dependencies, then save this as components/inreact/flip-wheel.tsx.
terminal
npm install gsap threecomponents/inreact/flip-wheel.tsx
"use client";
import { gsap } from "gsap";
import * as THREE from "three";
import { useEffect, useRef } from "react";
/**
* Flip Wheel: a rolodex of cards that flips one at a time.
*
* Portrait cards stand on a horizontal drum. The ones still to come lean back
* behind the front card, so their tops peek out as a stack of colour strips;
* each step the front card slides down toward you, reclining as it goes, and
* the stack moves up one. It flips on its own; a wheel, a drag or a
* click turns it by hand, and the drum leans after the cursor. Three.js + GSAP.
*
* <FlipWheel cards={[{ src: "/a.jpg" }, { src: "/b.jpg" }, ...]} className="h-[640px]" />
*/
export interface FlipCard {
src?: string;
/** Or paint the face yourself (canvas is 512 x 640). */
draw?: (ctx: CanvasRenderingContext2D, w: number, h: number) => void;
alt?: string;
}
export interface FlipWheelProps {
cards: FlipCard[];
/** Flip on its own. */
autoplay?: boolean;
/** Seconds each card rests at the front. */
interval?: number;
/** Degrees between cards on the drum. */
spacing?: number;
onChange?: (index: number) => void;
className?: string;
}
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const D2R = Math.PI / 180;
function roundTex() {
const c = document.createElement("canvas");
c.width = 256;
c.height = 320;
const g = c.getContext("2d")!;
g.fillStyle = "#000";
g.fillRect(0, 0, 256, 320);
g.fillStyle = "#fff";
g.beginPath();
g.roundRect(1, 1, 254, 318, 16);
g.fill();
const t = new THREE.CanvasTexture(c);
t.colorSpace = THREE.NoColorSpace;
return t;
}
function cover(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 FlipWheel({ cards, autoplay = true, interval = 0.7, spacing = 7.5, onChange, className }: FlipWheelProps) {
const rootRef = useRef<HTMLDivElement>(null);
const hostRef = useRef<HTMLDivElement>(null);
const cb = useRef(onChange);
cb.current = onChange;
const n = cards.length;
const key = cards.map((c, i) => c.src ?? `draw${i}`).join(",");
useEffect(() => {
const root = rootRef.current, host = hostRef.current;
if (!root || !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(40, 1, 0.1, 80);
const drum = new THREE.Group(); // tilts after the cursor
scene.add(drum);
const aniso = renderer.capabilities.getMaxAnisotropy();
// card geometry: hinged at its bottom edge, so it rotates about the drum's axis
const W = 1.9, H = 2.4, R = 0.18;
const geo = new THREE.PlaneGeometry(W, H);
geo.translate(0, R + H / 2, 0);
const round = roundTex();
const faces = cards.map((c) => {
const cv = document.createElement("canvas");
cv.width = 512;
cv.height = 640;
const g = cv.getContext("2d")!;
g.fillStyle = "#d9d9dc";
g.fillRect(0, 0, 512, 640);
const tex = new THREE.CanvasTexture(cv);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = aniso;
if (c.draw) {
c.draw(g, 512, 640);
tex.needsUpdate = true;
} else if (c.src) {
const img = new Image();
img.decoding = "async";
img.onload = () => {
cover(g, img, 512, 640);
tex.needsUpdate = true;
};
img.src = c.src;
}
return tex;
});
// enough slots to fill the drum's visible arc, repeating the set
const SLOTS = Math.max(n, Math.ceil(200 / spacing));
const slots = Array.from({ length: SLOTS }, (_, j) => {
const mat = new THREE.MeshBasicMaterial({ map: faces[j % n], alphaMap: round, transparent: true, side: THREE.DoubleSide, depthWrite: false });
const mesh = new THREE.Mesh(geo, mat);
drum.add(mesh);
return { mesh, mat, j };
});
const resize = () => {
const r = host.getBoundingClientRect();
const w = Math.max(1, r.width), h = Math.max(1, r.height);
renderer.setSize(w, h, false);
camera.aspect = w / h;
// keep the composition: pull back on wide boxes so the drum still fills the height
// low and in front: the waiting stack climbs away above, flipped cards lie below as trapezoids
const z = 8.4 * Math.max(1, 0.8 / camera.aspect);
camera.position.set(0, 0.9, z);
camera.lookAt(0, 1.15, -0.6);
camera.updateProjectionMatrix();
};
resize();
const ro = new ResizeObserver(resize);
ro.observe(host);
// s: which card is at the front (fractional while flipping)
const st = { s: 0 };
let target = 0;
const flipTo = (to: number, dur = 0.7) => {
target = to;
gsap.to(st, { s: to, duration: still ? 0 : dur, ease: "power2.inOut", overwrite: true });
};
let timer: gsap.core.Tween | null = null;
let hover = false;
const schedule = () => {
timer?.kill();
if (!autoplay || still || hover) return;
timer = gsap.delayedCall(interval + 0.7, () => {
flipTo(Math.round(target) + 1);
schedule();
});
};
schedule();
// by hand: wheel, drag (down flips forward), click
let wheelLock = 0;
const onWheel = (e: WheelEvent) => {
if (Math.abs(e.deltaY) < 8) return;
const now = performance.now();
if (now < wheelLock) return;
e.preventDefault();
wheelLock = now + 380;
flipTo(Math.round(target) + Math.sign(e.deltaY), 0.6);
schedule();
};
let drag: { y: number; s: number; moved: boolean } | null = null;
const onDown = (e: PointerEvent) => {
drag = { y: e.clientY, s: st.s, moved: false };
gsap.killTweensOf(st);
timer?.kill();
};
const look = { x: 0, y: 0 };
const lx = gsap.quickTo(look, "x", { duration: 1.1, ease: "power3.out" });
const ly = gsap.quickTo(look, "y", { duration: 1.1, ease: "power3.out" });
const onMove = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
if (!still) {
lx(((e.clientX - r.left) / r.width) * 2 - 1);
ly(((e.clientY - r.top) / r.height) * 2 - 1);
}
if (!drag) return;
const dy = e.clientY - drag.y;
if (Math.abs(dy) > 4) drag.moved = true;
st.s = drag.s + dy / (r.height * 0.35);
target = st.s;
};
const onUp = () => {
if (!drag) return;
const moved = drag.moved;
drag = null;
flipTo(moved ? Math.round(st.s) : Math.round(target) + 1, 0.55);
schedule();
};
const onEnter = () => {
hover = true;
timer?.kill();
};
const onLeave = () => {
hover = false;
lx(0);
ly(0);
onUp();
schedule();
};
root.addEventListener("wheel", onWheel, { passive: false });
root.addEventListener("pointerdown", onDown);
root.addEventListener("pointermove", onMove, { passive: true });
window.addEventListener("pointerup", onUp);
root.addEventListener("pointerenter", onEnter);
root.addEventListener("pointerleave", onLeave);
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting));
io.observe(root);
let current = -1;
// the layout: waiting cards climb like a staircase (up and back), the front card tips forward
// over its bottom edge, and flipped cards lie flat, sliding toward you and down
const CLIMB = 0.2, BACK = 0.26, LEAN = -3;
const SLIDE = 2.3, DROP = 1.25, REST = -60; // passed cards slide down toward you, reclining (tops away), faces still showing
const tick = () => {
if (!visible) return;
drum.rotation.y = look.x * 0.1;
drum.rotation.x = look.y * 0.04;
for (const sl of slots) {
let k = sl.j - st.s;
k = ((k % SLOTS) + SLOTS) % SLOTS;
if (k > SLOTS - 5) k -= SLOTS;
const m = sl.mesh;
if (k >= 0) {
m.position.set(0, k * CLIMB, -k * BACK);
m.rotation.x = (LEAN - k * 0.4) * D2R;
} else if (k > -1) {
// flipping: tips forward over the hinge
// passing: slides down toward you while reclining back
const u = -k;
const d = u * u * (3 - 2 * u);
m.position.set(0, -d * DROP, d * SLIDE);
m.rotation.x = (LEAN + u * (REST - LEAN)) * D2R;
} else {
const f = -k;
m.position.set(0, -f * DROP, f * SLIDE);
m.rotation.x = REST * D2R;
}
m.visible = k < 14 && k > -4;
// far stack first, then the front, then the flipped ones (nearest last)
m.renderOrder = k >= 0 ? 1000 - Math.round(k * 10) : 2000 + Math.round(-k * 10);
}
renderer.render(scene, camera);
const idx = ((Math.round(st.s) % n) + n) % n;
if (idx !== current) {
current = idx;
cb.current?.(idx);
}
};
gsap.ticker.add(tick);
return () => {
gsap.ticker.remove(tick);
timer?.kill();
gsap.killTweensOf(st);
root.removeEventListener("wheel", onWheel);
root.removeEventListener("pointerdown", onDown);
root.removeEventListener("pointermove", onMove);
window.removeEventListener("pointerup", onUp);
root.removeEventListener("pointerenter", onEnter);
root.removeEventListener("pointerleave", onLeave);
io.disconnect();
ro.disconnect();
slots.forEach((s) => s.mat.dispose());
faces.forEach((f) => f.dispose());
round.dispose();
geo.dispose();
renderer.dispose();
canvas.remove();
};
// cards are read once per set; rebuild when they change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, autoplay, interval, spacing]);
return (
<div
ref={rootRef}
role="img"
aria-label="A rolodex of cards flipping one at a time"
className={["relative h-full w-full cursor-grab touch-none select-none", className].filter(Boolean).join(" ")}
>
<div ref={hostRef} className="absolute inset-0" />
</div>
);
}
export default FlipWheel;
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
6 propscards{ src?; draw?; alt? }[]The cards: portrait images, or draw(ctx, w, h) to paint one (512 x 640).autoplaybooleanFlip on its own (pauses on hover).default truedefault trueintervalnumberSeconds each card rests at the front.default 0.7default 0.7spacingnumberDegrees between cards on the drum.default 7.5default 7.5onChange(index) => voidCalled when a new card reaches the front.classNamestringGive it a height.