Animations106 / 118
Glass Gyro
Nested rings of tinted glass, each turning on its own axis inside the next, like a gyroscope made of light. It leans toward your cursor with each inner ring following a beat later; flick it and the rings spin up and coast down; click and every ring swings into one plane as concentric halos, holds, and falls back into a staggered tumble.
three.jsthree.jsGlassGlassGimbalGimbalBloomBloomSpringsSpringsGSAPGSAP
Lean it with your cursor, flick to spinClick to align the rings
Tweak
LIVELIVEGlass
#6366ff
5
1.00
Motion
1.0×
Usage
Follows your tweaks.
usage.tsx
import { GlassGyro } from "@/components/inreact/glass-gyro";
export function Hero() {
return (
<section className="h-[640px] bg-black">
<GlassGyro tint="#6366ff" rings={5} glow={1} speed={1} />
</section>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/glass-gyro.jsonOr copy the source
Install the dependencies, then save this as components/inreact/glass-gyro.tsx.
terminal
npm install gsap threecomponents/inreact/glass-gyro.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef } from "react";
import * as THREE from "three";
import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";
import { OutputPass } from "three/addons/postprocessing/OutputPass.js";
import { RenderPass } from "three/addons/postprocessing/RenderPass.js";
import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js";
/**
* Glass Gyro: nested rings of tinted glass, each turning on its own axis inside the next,
* like a gyroscope made of light. Rendered live in three.js.
*
* - The rings are real bevelled bands of glass: fresnel edges, iridescent film, a soft bloom.
* - It leans toward the cursor, and each inner ring follows a beat later (follow-through).
* - Drag to spin the rings up; they coast down again like real momentum.
* - Click (or Enter / Space): it winds up, then every ring swings into one plane, a set of
* concentric halos, overshoots, holds, and lets go back into a staggered tumble.
*
* <div className="h-[600px] bg-black"><GlassGyro /></div>
*/
export type GlassGyroProps = {
/** Tint of the glass. */
tint?: string;
/** Number of rings, 3 to 7. */
rings?: number;
/** Speed of the idle tumble (0 = still, 1 = default). */
speed?: number;
/** Strength of the glow on the edges, 0 to 2. */
glow?: number;
/** Background colour. */
background?: string;
/** Called when the rings align. */
onAlign?: () => void;
className?: string;
};
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// a flat band of glass: a rectangle (with rounded corners) turned round the axis
function bandGeometry(R: number, w: number, h: number) {
const r = Math.min(w, h) * 0.2; // corner radius: the bevels are what catch the light
const pts: THREE.Vector2[] = [];
const corner = (cx: number, cy: number, a0: number) => {
for (let i = 0; i <= 6; i++) {
const a = a0 + (i / 6) * (Math.PI / 2);
pts.push(new THREE.Vector2(cx + Math.cos(a) * r, cy + Math.sin(a) * r));
}
};
const ri = R - w, ro = R, hy = h / 2;
corner(ro - r, -hy + r, -Math.PI / 2); // bottom outer
corner(ro - r, hy - r, 0); // top outer
corner(ri + r, hy - r, Math.PI / 2); // top inner
corner(ri + r, -hy + r, Math.PI); // bottom inner
pts.push(pts[0].clone());
const g = new THREE.LatheGeometry(pts, 160);
g.computeVertexNormals();
return g;
}
export function GlassGyro({ tint = "#6366ff", rings = 5, speed = 1, glow = 1, background = "#000000", onAlign, className }: GlassGyroProps) {
const rootRef = useRef<HTMLDivElement>(null);
const live = useRef({ speed, glow, onAlign });
live.current = { speed, glow, onAlign };
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const still = reduced();
const N = Math.max(3, Math.min(7, Math.round(rings)));
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: false, alpha: false, powerPreference: "high-performance" });
} catch {
canvas.remove();
return;
}
const dpr = Math.min(2, Math.max(1.25, window.devicePixelRatio || 1));
renderer.setPixelRatio(dpr);
renderer.setClearColor(new THREE.Color(background), 1);
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
// the studio the glass reflects: a dark room, a big soft key above, cool and violet strips round it
const pmrem = new THREE.PMREMGenerator(renderer);
const studio = new THREE.Scene();
const envMat = new THREE.ShaderMaterial({
side: THREE.BackSide,
vertexShader: "varying vec3 vD;\nvoid main(){ vD = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
fragmentShader: `
varying vec3 vD;
float panel(vec3 d, vec3 c, float r){ return smoothstep(r, r * 0.45, acos(clamp(dot(d, normalize(c)), -1.0, 1.0))); }
void main(){
vec3 d = normalize(vD);
vec3 c = vec3(0.01, 0.01, 0.025);
// a big soft lavender wash above: the gentle gradient on the flat faces of the bands
c += vec3(0.45, 0.45, 1.0) * smoothstep(-0.2, 0.9, d.y) * 0.4;
c += vec3(1.0) * panel(d, vec3(-0.3, 0.9, 0.4), 0.45) * 3.0;
c += vec3(0.4, 0.85, 1.0) * panel(d, vec3(0.95, 0.1, 0.3), 0.32) * 2.6;
c += vec3(0.95, 0.45, 1.0) * panel(d, vec3(-0.95, -0.2, 0.2), 0.3) * 2.2;
c += vec3(1.0) * panel(d, vec3(0.5, -0.5, 0.7), 0.15) * 3.0;
c += vec3(0.6, 0.9, 1.0) * panel(d, vec3(0.1, -0.95, 0.2), 0.35) * 1.0;
c += vec3(0.5, 0.4, 1.0) * panel(d, vec3(0.0, 0.2, -1.0), 0.6) * 1.2;
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;
// the glass: added light (so overlapping rings glow through each other, never sorted wrong),
// bright at grazing angles like real glass, with an iridescent film
const tintC = new THREE.Color(tint);
const uni = { uFlash: { value: 0 }, uTint: { value: tintC.clone() } };
const glass = new THREE.MeshPhysicalMaterial({
color: new THREE.Color(1, 1, 1).lerp(tintC, 0.32),
metalness: 0,
roughness: 0.1,
transmission: 1,
thickness: 0.18,
ior: 1.5,
attenuationColor: tintC,
attenuationDistance: 0.5,
envMapIntensity: 2.2,
iridescence: 0.8,
iridescenceIOR: 1.45,
iridescenceThicknessMinimum: 200,
iridescenceThicknessMaximum: 620,
clearcoat: 1,
clearcoatRoughness: 0.05,
side: THREE.DoubleSide,
});
glass.onBeforeCompile = (sh) => {
Object.assign(sh.uniforms, uni);
sh.fragmentShader =
"uniform float uFlash;\nuniform vec3 uTint;\n" +
sh.fragmentShader.replace(
"#include <opaque_fragment>",
`{
// glass is clear face-on and bright edge-on: the body fades where you look straight through it
float facing = abs(dot(normalize(vViewPosition), normal));
float edge = pow(1.0 - facing, 2.0);
// tinted glass: light gathers in the body, brightest where you look along it (the edges)
outgoingLight += mix(uTint, vec3(0.75, 0.75, 1.0), 0.12) * (0.05 + 0.45 * edge);
outgoingLight += vec3(0.6, 0.88, 1.0) * pow(edge, 2.5) * 0.9;
outgoingLight *= 1.0 + 0.6 * uFlash;
}
#include <opaque_fragment>`,
);
};
// the gimbal: each ring sits inside the one before and turns on its own axis
const root3 = new THREE.Group(); // leans toward the cursor
const squashG = new THREE.Group(); // squash and stretch
root3.add(squashG);
scene.add(root3);
const geos: THREE.BufferGeometry[] = [];
type Ring = { pivot: THREE.Group; axis: THREE.Vector3; angle: number; vel: number; idle: number; lean: { x: number; v: number; y: number; vy: number }; base: THREE.Quaternion };
const ringsArr: Ring[] = [];
let parent: THREE.Object3D = squashG;
// every axis lies in the ring's own plane, so turning to 'flat' brings each ring back into one shared plane
const axes = [new THREE.Vector3(1, 0, 0), new THREE.Vector3(0, 1, 0), new THREE.Vector3(0.7, 0.7, 0).normalize(), new THREE.Vector3(-0.7, 0.7, 0).normalize()];
for (let i = 0; i < N; i++) {
const R = 1 - i * (0.56 / Math.max(1, N - 1));
const geo = bandGeometry(R, 0.12 - 0.02 * (i / N), 0.11 - 0.02 * (i / N));
geos.push(geo);
const pivot = new THREE.Group();
const mesh = new THREE.Mesh(geo, glass);
mesh.rotation.x = Math.PI / 2; // lathe bands stand along y: lay them in the pivot's xy plane
pivot.add(mesh);
parent.add(pivot);
const idle = (0.35 + 0.18 * i) * (i % 2 ? -1 : 1);
ringsArr.push({ pivot, axis: axes[i % axes.length], angle: i * 0.9, vel: idle, idle, lean: { x: 0, v: 0, y: 0, vy: 0 }, base: new THREE.Quaternion() });
parent = pivot;
}
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 bloom = new UnrealBloomPass(new THREE.Vector2(4, 4), 0.3, 0.4, 0.8);
composer.addPass(bloom);
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;
const span = 3.0;
const t = Math.tan(THREE.MathUtils.degToRad(15));
camera.position.set(0, 0, Math.max(span / (2 * t), span / (2 * t * camera.aspect)));
camera.lookAt(0, 0, 0);
camera.updateProjectionMatrix();
};
resize();
const ro = new ResizeObserver(resize);
ro.observe(root);
// ---- motion ----
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 tiltX = sp(0, 32, 8), tiltY = sp(0, 32, 8);
const squash = sp(1, 170, 8);
const alignAmt = sp(0, 60, 9); // 0 = free tumble, 1 = every ring in one plane
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;
let busy = false;
let flash = 0, flashGoal = 0;
const calls: gsap.core.Tween[] = [];
const q = new THREE.Quaternion(), qa = new THREE.Quaternion();
let t = 0;
const align = () => {
if (busy) return;
busy = true;
live.current.onAlign?.();
if (still) {
alignAmt.x = alignAmt.t = 1;
calls.push(gsap.delayedCall(1.6, () => ((alignAmt.x = alignAmt.t = 0), (busy = false))));
return;
}
// anticipation: it gathers in
squash.t = 0.9;
calls.push(
gsap.delayedCall(0.14, () => {
// swing every ring into one plane: a set of concentric halos, with overshoot
squash.t = 1;
squash.v += 2.4;
alignAmt.t = 1;
flashGoal = 1;
}),
gsap.delayedCall(1.7, () => {
// and let go: they fall back into the tumble one after another (each keeps its momentum)
alignAmt.t = 0;
ringsArr.forEach((r, i) => (r.vel = r.idle * (2.6 - i * 0.15)));
busy = false;
}),
);
};
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting), { rootMargin: "80px" });
io.observe(root);
const tick = (_time: number, dms: number) => {
const dt = Math.min(0.05, Math.max(0.001, dms / 1000));
t += dt;
const L2 = live.current;
const px = pointer ? (pointer.x / W) * 2 - 1 : 0;
const py = pointer ? -((pointer.y / H) * 2 - 1) : 0;
tiltX.t = still ? 0 : -py * 0.45;
tiltY.t = still ? 0 : px * 0.6;
[tiltX, tiltY, squash, alignAmt].forEach((s) => step(s, dt));
root3.rotation.set(tiltX.x + 0.35, tiltY.x - 0.25, 0.1);
root3.position.y = still ? 0 : Math.sin(t * 0.8) * 0.03;
const s = Math.max(0.7, squash.x);
squashG.scale.set(1 / Math.sqrt(s), s, 1 / Math.sqrt(s));
const al = Math.min(1.15, Math.max(0, alignAmt.x));
ringsArr.forEach((r, i) => {
// momentum: spun-up rings coast back down to their idle turn
const idle = still ? 0 : r.idle * L2.speed;
r.vel += (idle - r.vel) * (1 - Math.exp(-dt * 0.7));
if (!busy || alignAmt.t === 0) r.angle += r.vel * dt;
// follow-through: each inner ring leans after the cursor a beat later than its parent
const k = 60 / (1 + i * 0.6), c = 2 * Math.sqrt(k) * 0.55;
const gx = (tiltX.v * -0.04) * (1 + i * 0.3), gy = (tiltY.v * 0.04) * (1 + i * 0.3);
r.lean.v += (k * (gx - r.lean.x) - c * r.lean.v) * dt;
r.lean.x += r.lean.v * dt;
r.lean.vy += (k * (gy - r.lean.y) - c * r.lean.vy) * dt;
r.lean.y += r.lean.vy * dt;
// aligned, every ring's turn goes to the nearest flat position
const flat = Math.round(r.angle / Math.PI) * Math.PI;
const a = r.angle + (flat - r.angle) * al;
if (al > 0.999 && busy) r.angle = flat;
q.setFromAxisAngle(r.axis, a);
qa.setFromEuler(new THREE.Euler(r.lean.x, r.lean.y, 0));
r.pivot.quaternion.copy(qa).multiply(q);
});
flash += (flashGoal - flash) * (1 - Math.exp(-dt * 20));
flashGoal *= Math.exp(-dt * 3);
uni.uFlash.value = flash;
bloom.strength = (0.28 + 0.25 * flash) * L2.glow;
if (visible && W) composer.render(dt);
};
gsap.ticker.add(tick);
const local = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const onMove = (e: PointerEvent) => {
pointer = local(e);
if (!drag) return;
const now = performance.now();
const ddt = Math.max(8, now - drag.t) / 1000;
const v = Math.hypot(e.clientX - drag.x, e.clientY - drag.y) / Math.min(W, H) / ddt;
const dir = Math.sign(e.clientX - drag.x || 1);
// a flick spins the rings up: the outer ones most, alternating directions
ringsArr.forEach((r, i) => {
const boost = Math.min(9, v * (2.2 - i * 0.18)) * dir * (i % 2 ? -1 : 1);
r.vel += (boost - r.vel) * 0.25;
});
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 };
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) align();
};
const onLeave = () => {
if (!drag) pointer = null;
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
align();
}
};
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);
geos.forEach((g) => g.dispose());
glass.dispose();
env.dispose();
pmrem.dispose();
composer.dispose();
renderer.dispose();
canvas.remove();
};
}, [tint, rings, background]);
return (
<div
ref={rootRef}
tabIndex={0}
role="button"
aria-label="Glass gyroscope. Drag to spin it, press to align the rings."
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 GlassGyro;
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 propstintstringTint of the glass.default "#6366ff"default "#6366ff"ringsnumberNumber of rings, 3 to 7.default 5default 5speednumberSpeed of the idle tumble (0 = still).default 1default 1glownumberStrength of the glow on the edges, 0 to 2.default 1default 1backgroundstringBackground colour.default "#000000"default "#000000"onAlign() => voidCalled when the rings align.classNamestringExtra classes (it fills its parent).