Animations104 / 118
Lumen Capsule
A scratched glass capsule floating in the dark, lit only by its rim. It tumbles slowly like an object in space while the studio light turns with your cursor, catching the iridescent film and every hairline scratch. Fling it and it coasts, leaving echoes; click and it winds up, splits open, hangs, then clacks shut with a squash.
three.jsthree.jsPhysical glassPhysical glassDispersionDispersionIridescenceIridescenceSpringsSpringsGSAPGSAP
Drag to fling itClick to split it open
Tweak
LIVELIVELight
Rim light
Glass
0.60
Motion
1.0×
Spin echoes
Usage
Follows your tweaks.
usage.tsx
import { LumenCapsule } from "@/components/inreact/lumen-capsule";
export function Hero() {
return (
<section className="h-[640px] bg-black">
<LumenCapsule palette="daylight" scratches={0.6} speed={1} />
</section>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/lumen-capsule.jsonOr copy the source
Install the dependencies, then save this as components/inreact/lumen-capsule.tsx.
terminal
npm install gsap threecomponents/inreact/lumen-capsule.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef } from "react";
import * as THREE from "three";
import { AfterimagePass } from "three/addons/postprocessing/AfterimagePass.js";
import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";
import { OutputPass } from "three/addons/postprocessing/OutputPass.js";
import { RenderPass } from "three/addons/postprocessing/RenderPass.js";
/**
* Lumen Capsule: a scratched glass capsule floating in the dark, lit only by its rim.
*
* - It tumbles slowly on its own, coasting like an object in space.
* - The studio light behind it follows the cursor, so the rim, the iridescent
* film and the scratches catch the light wherever you point.
* - Drag to fling it: it keeps spinning, then drifts back to its slow tumble.
* Spun fast, it leaves fading echoes of itself.
* - Click (or Enter / Space) to split it open. It winds up, the halves spring
* apart and tilt, hang for a moment, then clack shut with a squash.
*
* <div className="h-[560px] bg-black"><LumenCapsule palette="daylight" /></div>
*/
export type LumenPalette = "daylight" | "aurora" | "ember";
export type LumenCapsuleProps = {
/** Colours of the rim light: warm back light + cool side band. */
palette?: LumenPalette;
/** Speed of the idle tumble (0 = still, 1 = default). */
speed?: number;
/** How visible the surface scratches are, 0 to 1. */
scratches?: number;
/** Fading echoes when it spins fast. */
trails?: boolean;
/** Background colour of the canvas. */
background?: string;
/** Called each time the capsule splits open. */
onSplit?: () => void;
className?: string;
};
const PALETTES: Record<LumenPalette, { back: number; band: number; scr: number }> = {
daylight: { back: 0xffe9c7, band: 0x2f63ff, scr: 0xdfe8ff },
aurora: { back: 0xb8fff0, band: 0x7a4dff, scr: 0xd8fff6 },
ember: { back: 0xffb36b, band: 0xff2f6d, scr: 0xffe2cf },
};
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// capsule proportions (one half is built from the cut plane up)
const R = 0.62; // radius
const L = 0.85; // half the straight wall
const B = 0.014; // bevel at the cut: a hairline seam that catches the light
function halfGeometry() {
const pts: THREE.Vector2[] = [];
pts.push(new THREE.Vector2(0.0001, 0));
for (let i = 1; i <= 5; i++) pts.push(new THREE.Vector2(((R - B) * i) / 5, 0));
for (let i = 1; i <= 6; i++) {
const a = -Math.PI / 2 + (i / 6) * (Math.PI / 2);
pts.push(new THREE.Vector2(R - B + B * Math.cos(a), B + B * Math.sin(a)));
}
for (let i = 1; i <= 10; i++) pts.push(new THREE.Vector2(R, B + ((L - B) * i) / 10));
for (let i = 1; i <= 36; i++) {
const a = (i / 36) * (Math.PI / 2);
pts.push(new THREE.Vector2(Math.max(0.0001, R * Math.cos(a)), L + R * Math.sin(a)));
}
const g = new THREE.LatheGeometry(pts, 128);
g.computeVertexNormals();
return g;
}
// fine hairline scratches, like worn acrylic: a tileable grey mask
function paintScratches() {
const S = 1024;
const c = document.createElement("canvas");
c.width = c.height = S;
const x = c.getContext("2d")!;
x.fillStyle = "#000";
x.fillRect(0, 0, S, S);
x.lineCap = "round";
const stroke = (len: number, w: number, a: number) => {
const px = Math.random() * S, py = Math.random() * S;
const ang = Math.random() * Math.PI * 2;
const bend = (Math.random() - 0.5) * len * 0.5;
const ex = px + Math.cos(ang) * len, ey = py + Math.sin(ang) * len;
const mx = (px + ex) / 2 - Math.sin(ang) * bend, my = (py + ey) / 2 + Math.cos(ang) * bend;
// draw it in all wrapped positions so the tile repeats seamlessly
for (const ox of [-S, 0, S])
for (const oy of [-S, 0, S]) {
x.strokeStyle = `rgba(255,255,255,${a})`;
x.lineWidth = w;
x.beginPath();
x.moveTo(px + ox, py + oy);
x.quadraticCurveTo(mx + ox, my + oy, ex + ox, ey + oy);
x.stroke();
}
};
for (let i = 0; i < 1500; i++) stroke(6 + Math.random() * 46, 0.6 + Math.random() * 0.8, 0.12 + Math.random() * 0.4);
for (let i = 0; i < 70; i++) stroke(90 + Math.random() * 200, 0.7 + Math.random() * 0.6, 0.18 + Math.random() * 0.25);
return c;
}
export function LumenCapsule({ palette = "daylight", speed = 1, scratches = 0.6, trails = true, background = "#000000", onSplit, className }: LumenCapsuleProps) {
const rootRef = useRef<HTMLDivElement>(null);
const live = useRef({ speed, scratches, trails, onSplit });
live.current = { speed, scratches, trails, onSplit };
const splitRef = useRef<() => void>(() => {});
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const still = reduced();
const pal = PALETTES[palette] ?? PALETTES.daylight;
const canvas = document.createElement("canvas");
canvas.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:block;";
root.appendChild(canvas);
let renderer: THREE.WebGLRenderer;
try {
renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: false, powerPreference: "high-performance" });
} catch {
canvas.remove();
return;
}
// supersampled: the rim is a hairline of light, and it shimmers without it
const dpr = Math.min(2, Math.max(1.5, window.devicePixelRatio || 1));
renderer.setPixelRatio(dpr);
renderer.setClearColor(new THREE.Color(background), 1);
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = 1.0;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
// the studio, for reflections only: a warm light wall behind the capsule (the rim),
// a cool band around its sides (the inner edge), a small key softbox, darkness in front
const pmrem = new THREE.PMREMGenerator(renderer);
const studio = new THREE.Scene();
const envMat = new THREE.ShaderMaterial({
side: THREE.BackSide,
depthWrite: false,
uniforms: { uBack: { value: new THREE.Color(pal.back) }, uBand: { value: new THREE.Color(pal.band) } },
vertexShader: "varying vec3 vD;\nvoid main(){ vD = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
fragmentShader: `
varying vec3 vD;
uniform vec3 uBack;
uniform vec3 uBand;
void main(){
vec3 d = normalize(vD);
float th = acos(clamp(-d.z, -1.0, 1.0)); // angle from straight behind
float ring = exp(-pow((th - 0.5) / 0.2, 2.0));
float band = exp(-pow((th - 1.02) / 0.11, 2.0));
vec3 c = uBack * ring * 4.2 * (0.7 + 0.3 * d.y);
c += uBand * band * 1.5 * (0.6 + 0.4 * -d.y);
float key = smoothstep(0.8, 0.97, dot(d, normalize(vec3(-0.55, 0.7, 0.45))));
c += vec3(1.0, 0.97, 0.92) * key * 1.3;
float fill = smoothstep(0.9, 0.99, dot(d, normalize(vec3(0.8, -0.35, 0.3))));
c += uBand * fill * 1.6;
gl_FragColor = vec4(c, 1.0);
}`,
});
const envSphere = new THREE.Mesh(new THREE.SphereGeometry(10, 64, 32), envMat);
studio.add(envSphere);
const env = pmrem.fromScene(studio, 0.02).texture;
envSphere.geometry.dispose();
envMat.dispose();
scene.environment = env;
scene.environmentIntensity = 1.0;
// the glass
const scrCanvas = paintScratches();
const scrTex = new THREE.CanvasTexture(scrCanvas);
scrTex.wrapS = scrTex.wrapT = THREE.RepeatWrapping;
scrTex.colorSpace = THREE.NoColorSpace;
scrTex.anisotropy = renderer.capabilities.getMaxAnisotropy();
const uni = {
uGlow: { value: new THREE.Color(pal.back) },
uDeep: { value: new THREE.Color(pal.band) },
uScr: { value: scrTex },
uScratch: { value: live.current.scratches },
uFlash: { value: 0 },
uScrTint: { value: new THREE.Color(pal.scr) },
};
const glass = new THREE.MeshPhysicalMaterial({
color: 0x000000, // no diffuse: everything you see is reflected or bent light
metalness: 0,
roughness: 0.16,
ior: 1.45,
iridescence: 0.7,
iridescenceIOR: 1.3,
iridescenceThicknessMinimum: 180,
iridescenceThicknessMaximum: 640,
clearcoat: 0.7,
clearcoatRoughness: 0.06,
specularIntensity: 1,
envMapIntensity: 1.35,
});
glass.onBeforeCompile = (sh) => {
Object.assign(sh.uniforms, uni);
sh.vertexShader = "varying vec3 vObjP;\nvarying vec3 vObjN;\nvarying vec3 vWP;\nvarying vec3 vAx;\n" + sh.vertexShader.replace("#include <begin_vertex>", "#include <begin_vertex>\nvObjP = position;\nvObjN = normal;\nvWP = (modelMatrix * vec4(position, 1.0)).xyz;\nvAx = normalize(mat3(modelMatrix) * vec3(0.0, 1.0, 0.0));");
sh.fragmentShader =
"uniform sampler2D uScr;\nuniform float uScratch;\nuniform float uFlash;\nuniform vec3 uScrTint;\nuniform vec3 uGlow;\nuniform vec3 uDeep;\nvarying vec3 vObjP;\nvarying vec3 vObjN;\nvarying vec3 vWP;\nvarying vec3 vAx;\n" +
sh.fragmentShader.replace(
"#include <opaque_fragment>",
`{
// scratches, mapped from three sides so they never stretch over the domes
vec3 bw = pow(abs(normalize(vObjN)), vec3(4.0));
bw /= (bw.x + bw.y + bw.z);
float sc = texture2D(uScr, vObjP.yz * 0.55).r * bw.x + texture2D(uScr, vObjP.xz * 0.55).r * bw.y + texture2D(uScr, vObjP.xy * 0.55).r * bw.z;
float fr = 1.0 - abs(dot(normalize(vViewPosition), normal));
vec3 body = vec3(0.0);
{
// the domes are lenses: seen at an angle they gather the light from behind and glow,
// red, green and blue each bent by their own index (dispersion). Looking down the
// length (end on) the light has too far to travel, so the glass stays dark.
vec3 nW = normalize(inverseTransformDirection(normal, viewMatrix));
vec3 vW = normalize(vWP - cameraPosition);
float dome = smoothstep(${(L * 0.92).toFixed(3)}, ${(L + R * 0.75).toFixed(3)}, vObjP.y);
float oblique = 1.0 - abs(dot(vW, normalize(vAx)));
vec3 iors = vec3(1.42, 1.5, 1.6);
for (int k = 0; k < 3; k++) {
float io = iors[k];
vec3 r1 = refract(vW, nW, 1.0 / io);
vec3 n2 = normalize(nW - 2.0 * dot(nW, r1) * r1);
vec3 r2 = refract(r1, -n2, io);
if (dot(r2, r2) < 0.01) r2 = reflect(r1, -n2);
vec3 d = envMapRotation * r2;
float th = acos(clamp(-d.z, -1.0, 1.0));
body[k] = uGlow[k] * (exp(-pow(th / 0.5, 2.0)) * 1.3 + 0.75 * exp(-pow((th - 0.95) / 0.16, 2.0)));
}
body *= dome * pow(oblique, 1.4) * 1.7;
// the long wall: deep, faintly tinted glass
body += uDeep * 0.05 * (1.0 - dome) * (1.0 - fr);
}
outgoingLight += body;
float lum = min(1.0, dot(body, vec3(0.3, 0.5, 0.2)));
outgoingLight += uScrTint * sc * uScratch * (0.1 + 1.6 * fr * fr + 0.9 * lum + 0.6 * uFlash);
outgoingLight *= 1.0 + 0.22 * uFlash;
}
#include <opaque_fragment>`,
);
};
const geo = halfGeometry();
// rig: drift (bob + lean toward the cursor) > spin (tumble) > body (squash) > halves
const drift = new THREE.Group();
const spin = new THREE.Group();
const body = new THREE.Group();
const topG = new THREE.Group();
const botG = new THREE.Group();
const top = new THREE.Mesh(geo, glass);
const bot = new THREE.Mesh(geo, glass);
bot.rotation.x = Math.PI;
topG.add(top);
botG.add(bot);
body.add(topG, botG);
spin.add(body);
drift.add(spin);
scene.add(drift);
// start on the diagonal, like a pill laid across the frame
spin.quaternion.setFromEuler(new THREE.Euler(0.35, 0.2, -Math.PI / 4));
// camera
const camera = new THREE.PerspectiveCamera(30, 1, 0.1, 100);
const composer = new EffectComposer(renderer, new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType, samples: 4 }));
composer.addPass(new RenderPass(scene, camera));
const after = new AfterimagePass(0);
composer.addPass(after);
composer.addPass(new OutputPass());
let W = 0, H = 0;
const resize = () => {
const w = root.clientWidth, h = root.clientHeight;
if (w < 2 || h < 2 || (w === W && h === H)) return;
W = w;
H = h;
renderer.setSize(W, H, false);
composer.setPixelRatio(dpr);
composer.setSize(W, H);
camera.aspect = W / H;
// keep the capsule (with room to split) inside the shorter side
const span = 4.9;
const t = Math.tan(THREE.MathUtils.degToRad(15));
const d = Math.max(span / (2 * t), span / (2 * t * camera.aspect));
camera.position.set(0, 0, d);
camera.lookAt(0, 0, 0);
camera.updateProjectionMatrix();
};
resize();
const ro = new ResizeObserver(resize);
ro.observe(root);
// ---- motion ----
const w = new THREE.Vector3(); // angular velocity (world, rad/s)
const idleAxis = new THREE.Vector3(1, -1, 0.3).normalize();
const dq = new THREE.Quaternion();
const tmp = new THREE.Vector3();
// springs: x, v, target, stiffness, damping
type Spring = { x: number; v: number; t: number; k: number; c: number };
const sp = (x: number, k: number, c: number): Spring => ({ x, v: 0, t: x, k, c });
const step = (s: Spring, dt: number) => {
const h = dt / 2;
for (let i = 0; i < 2; i++) {
s.v += (s.k * (s.t - s.x) - s.c * s.v) * h;
s.x += s.v * h;
}
};
const leanX = sp(0, 38, 9), leanY = sp(0, 38, 9); // capsule leans toward the cursor
const lightX = sp(0, 22, 8), lightY = sp(0, 22, 8); // the studio turns with the cursor
const gap = sp(0.002, 120, 11); // distance between the halves
const open = sp(0, 90, 10); // the halves' tilt as they part
const squash = sp(1, 260, 9); // along the capsule's axis
let flash = 0;
let trail = 0;
let pointer: { x: number; y: number } | null = null;
let drag: { x: number; y: number; t: number; sx: number; sy: number; t0: number; id: number } | null = null;
const dragW = new THREE.Vector3();
let busy = false;
let closing = false;
const calls: gsap.core.Tween[] = [];
const split = () => {
if (busy) return;
busy = true;
live.current.onSplit?.();
if (still) {
gap.t = 0.5;
calls.push(gsap.delayedCall(1.1, () => ((gap.t = 0.002), (busy = false))));
return;
}
// anticipation: it winds up along its length
squash.t = 0.88;
calls.push(
gsap.delayedCall(0.13, () => {
// release: the halves spring apart and tip, the whole thing stretches a touch
squash.t = 1;
squash.v += 2.6;
gap.t = 0.62;
open.t = 1;
tmp.set(Math.random() - 0.5, Math.random() - 0.5, 0).normalize().multiplyScalar(1.4);
w.add(tmp);
}),
gsap.delayedCall(1.15, () => {
// and they come back together, fast, for the clack
gap.k = 260;
gap.c = 16;
gap.t = 0.002;
open.t = 0;
closing = true;
}),
);
};
splitRef.current = split;
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting), { rootMargin: "80px" });
io.observe(root);
let t = 0;
const tick = (_time: number, dms: number) => {
const dt = Math.min(0.05, Math.max(0.001, dms / 1000));
t += dt;
const L2 = live.current;
// angular velocity: dragging steers it directly; let go and it coasts, settling back to its idle tumble
const idle = still ? 0 : 0.62 * L2.speed;
if (drag) w.lerp(dragW, 1 - Math.exp(-dt * 22));
else {
tmp.copy(idleAxis).multiplyScalar(idle);
w.lerp(tmp, 1 - Math.exp(-dt * 0.55));
}
dragW.multiplyScalar(Math.exp(-dt * 6)); // a held, unmoving drag slows to rest
const sp0 = w.length();
if (sp0 > 1e-5) {
dq.setFromAxisAngle(tmp.copy(w).divideScalar(sp0), sp0 * dt);
spin.quaternion.premultiply(dq).normalize();
}
// lean and light follow the cursor; when it leaves, they ease home
const px = pointer ? pointer.x : 0, py = pointer ? pointer.y : 0;
leanX.t = still ? 0 : py * -0.22;
leanY.t = still ? 0 : px * 0.3;
lightX.t = py * 0.75;
lightY.t = -px * 1.1;
[leanX, leanY, lightX, lightY, gap, open, squash].forEach((s) => step(s, dt));
drift.rotation.set(leanX.x, leanY.x, 0);
drift.position.set(px * 0.18, (still ? 0 : Math.sin(t * 0.9) * 0.07) + py * 0.12, 0);
scene.environmentRotation.set(lightX.x, lightY.x + (still ? 0 : t * 0.05), 0);
// the halves
const g = Math.max(0.002, gap.x);
topG.position.y = g / 2;
botG.position.y = -g / 2;
topG.rotation.x = open.x * 0.24;
botG.rotation.x = open.x * 0.24;
topG.rotation.z = open.x * 0.1;
botG.rotation.z = -open.x * 0.1;
if (closing && gap.x < 0.03) {
// clack: the shock squashes it and wobbles out, a quick flash runs through the scratches
closing = false;
squash.v -= 3.4;
flash = 1;
gap.k = 120;
gap.c = 11;
calls.push(gsap.delayedCall(0.45, () => (busy = false)));
}
const s = Math.max(0.6, squash.x);
body.scale.set(1 / Math.sqrt(s), s, 1 / Math.sqrt(s));
flash *= Math.exp(-dt * 5);
uni.uFlash.value = flash;
uni.uScratch.value = L2.scratches;
// echoes only when it is really spinning
const want = L2.trails && !still ? Math.min(0.55, Math.max(0, (sp0 - 2.4) * 0.2)) : 0;
trail += (want - trail) * (1 - Math.exp(-dt * 8));
(after.uniforms as { damp: { value: number } }).damp.value = trail;
if (visible && W) composer.render(dt);
};
gsap.ticker.add(tick);
// ---- input ----
const norm = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
return { x: ((e.clientX - r.left) / r.width) * 2 - 1, y: -(((e.clientY - r.top) / r.height) * 2 - 1) };
};
const onMove = (e: PointerEvent) => {
pointer = norm(e);
if (!drag) return;
const now = performance.now();
const ddt = Math.max(8, now - drag.t) / 1000;
const dx = (e.clientX - drag.x) / root.clientWidth, dy = (e.clientY - drag.y) / root.clientHeight;
// a trackball: sideways drags turn it around the vertical, vertical drags around the horizontal
const k = 9;
dragW.set((dy / ddt) * k * 0.6, (dx / ddt) * k * 0.6, 0).clampLength(0, 14);
drag.x = e.clientX;
drag.y = e.clientY;
drag.t = now;
};
const onDown = (e: PointerEvent) => {
if (e.button !== 0) return;
const now = performance.now();
drag = { x: e.clientX, y: e.clientY, t: now, sx: e.clientX, sy: e.clientY, t0: now, id: e.pointerId };
dragW.copy(w);
try {
root.setPointerCapture(e.pointerId);
} catch {
/* synthetic pointers can't be captured */
}
};
const onUp = (e: PointerEvent) => {
if (!drag) return;
const moved = Math.hypot(e.clientX - drag.sx, e.clientY - drag.sy);
const quick = performance.now() - drag.t0 < 450;
try {
root.releasePointerCapture(drag.id);
} catch {
/* not captured */
}
drag = null;
if (moved < 7 && quick) split();
};
const onLeave = () => {
if (!drag) pointer = null;
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
split();
} else if (e.key.startsWith("Arrow")) {
e.preventDefault();
const a = 2.4;
if (e.key === "ArrowLeft") w.y -= a;
if (e.key === "ArrowRight") w.y += a;
if (e.key === "ArrowUp") w.x -= a;
if (e.key === "ArrowDown") w.x += a;
}
};
root.addEventListener("pointermove", onMove);
root.addEventListener("pointerdown", onDown);
root.addEventListener("pointerup", onUp);
root.addEventListener("pointercancel", onUp);
root.addEventListener("pointerleave", onLeave);
root.addEventListener("keydown", onKey);
return () => {
gsap.ticker.remove(tick);
calls.forEach((c) => c.kill());
io.disconnect();
ro.disconnect();
root.removeEventListener("pointermove", onMove);
root.removeEventListener("pointerdown", onDown);
root.removeEventListener("pointerup", onUp);
root.removeEventListener("pointercancel", onUp);
root.removeEventListener("pointerleave", onLeave);
root.removeEventListener("keydown", onKey);
geo.dispose();
glass.dispose();
scrTex.dispose();
env.dispose();
pmrem.dispose();
composer.dispose();
renderer.dispose();
canvas.remove();
};
}, [palette, background]);
return (
<div
ref={rootRef}
tabIndex={0}
role="button"
aria-label="Glass capsule. Drag to spin it, press to split it open."
className={["relative h-full min-h-[280px] w-full cursor-grab touch-none overflow-hidden outline-none select-none active:cursor-grabbing", className].filter(Boolean).join(" ")}
style={{ background }}
/>
);
}
export default LumenCapsule;
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 propspalette"daylight" | "aurora" | "ember"Colours of the rim light.default "daylight"default "daylight"scratchesnumberHow visible the surface scratches are, 0 to 1.default 0.6default 0.6speednumberSpeed of the idle tumble (0 = still).default 1default 1trailsbooleanFading echoes when it spins fast.default truedefault truebackgroundstringCanvas background.default "#000000"default "#000000"onSplit() => voidCalled each time it splits open.classNamestringExtra classes (it fills its parent).