Text Animations53 / 118
Shatter Type
A word cut into triangular glass shards. Bring your cursor close and fracture lines appear; push into it and the shards lift, spin, cast shadows and flash their cracked edges before springing home. Click and the whole word blows apart, then puts itself back together.
Canvas 2DCanvas 2DPhysicsPhysicsZero dependenciesZero dependencies60fps60fpsReduced-motion safeReduced-motion safe
BREAK
Push into the glassClick to blow it up
Tweak
LIVELIVEType
Colour
#f2f0eb
#b9b6ae
#ff4d00
#4d7bff
Glass
34px
1.00
1.00
1.00
Usage
Follows your tweaks.
usage.tsx
import { ShatterType } from "@/components/inreact/shatter-type";
export function Hero() {
return (
<ShatterType
className="h-[360px] w-full"
text="BREAK"
/>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/shatter-type.jsonTS + CSS
npx shadcn@latest add https://inreact.vercel.app/r/shatter-type-css.jsonOr copy the source
No dependencies. Save this as components/inreact/shatter-type.tsx.
components/inreact/shatter-type.tsx
"use client";
import { useEffect, useRef } from "react";
/**
* Shatter Type: a word cut into triangular glass shards.
*
* Bring the cursor close and fracture lines appear; push into it and shards
* lift, spin, cast shadows and flash their cracked edges, then spring home.
* Click to blow the whole word apart; it reassembles itself.
*
* <ShatterType text="BREAK" className="h-[360px] w-full" />
*
* The word uses the component's font (set font-family on it or a parent).
* Canvas 2D, no dependencies. Pauses off-screen; with prefers-reduced-motion
* the word stays whole. The text is exposed to screen readers.
*/
export interface ShatterTypeProps {
/** The word or short phrase to shatter. */
text?: string;
/** Top colour of the type. */
color?: string;
/** Bottom colour of the type (a soft vertical fade). */
colorBottom?: string;
/** Font weight of the type. */
weight?: number;
/** First crack colour. */
crackA?: string;
/** Second crack colour. */
crackB?: string;
/** Approximate shard size in px. Smaller = more, finer shards. */
shardSize?: number;
/** How far around the cursor the glass reacts. */
radius?: number;
/** How hard the cursor pushes shards away. */
force?: number;
/** How violently a click blows the word apart. */
blast?: number;
className?: string;
}
interface Shard {
pts: number[]; // triangle x,y pairs (rest pose)
cx: number;
cy: number;
bx: number; // bbox in css px
by: number;
bw: number;
bh: number;
hue: 0 | 1; // which crack colour its edges flash
dx: number;
dy: number;
rot: number;
lift: number;
vx: number;
vy: number;
vr: number;
vl: number;
}
interface World {
shards: Shard[];
text: HTMLCanvasElement;
w: number;
h: number;
dpr: number;
}
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function rgba(hex: string, a: number) {
const h = hex.replace("#", "");
const n = parseInt(h.length === 3 ? h.replace(/./g, "$&$&") : h, 16);
return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})`;
}
export function ShatterType({
text = "BREAK",
color = "#f2f0eb",
colorBottom = "#b9b6ae",
weight = 900,
crackA = "#ff4d00",
crackB = "#4d7bff",
shardSize = 34,
radius = 1,
force = 1,
blast = 1,
className,
}: ShatterTypeProps) {
const hostRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
// live settings: physics reads them every frame; the look rebuilds the shards
const live = useRef({ text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast });
const rebuild = useRef<(() => void) | null>(null);
useEffect(() => {
live.current = { text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast };
}, [text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast]);
useEffect(() => {
rebuild.current?.();
}, [text, color, colorBottom, weight, shardSize]);
useEffect(() => {
const host = hostRef.current;
const canvas = canvasRef.current;
if (!host || !canvas) return;
if (getComputedStyle(host).position === "static") host.style.position = "relative";
const ctx = canvas.getContext("2d")!;
const reduced = isReducedMotion();
let world: World | null = null;
const ptr = { x: -1e4, y: -1e4, in: false, vx: 0, vy: 0, lx: 0, ly: 0 };
let raf = 0;
let last = 0;
let visible = false;
/* ---------- cut the word into shards ---------- */
const build = () => {
const L = live.current;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const w = Math.max(1, canvas.clientWidth);
const h = Math.max(1, canvas.clientHeight);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
// pre-render the word once
const textCanvas = document.createElement("canvas");
textCanvas.width = canvas.width;
textCanvas.height = canvas.height;
const t = textCanvas.getContext("2d", { willReadFrequently: true })!;
t.setTransform(dpr, 0, 0, dpr, 0, 0);
const family = getComputedStyle(host).fontFamily || "Arial";
const word = L.text.trim() || " ";
let size = h * 0.5;
t.font = `${L.weight} ${size}px ${family}`;
const fit = (w * 0.86) / Math.max(1, t.measureText(word).width);
if (fit < 1) size *= fit;
t.font = `${L.weight} ${size}px ${family}`;
t.textAlign = "center";
t.textBaseline = "middle";
const g = t.createLinearGradient(0, h * 0.2, 0, h * 0.7);
g.addColorStop(0, L.color);
g.addColorStop(1, L.colorBottom);
t.fillStyle = g;
t.fillText(word, w / 2, h * 0.42);
// jittered grid, each cell split along a random diagonal: shards read as broken glass
const data = t.getImageData(0, 0, textCanvas.width, textCanvas.height).data;
const cell = Math.max(14, L.shardSize);
const cols = Math.max(10, Math.round(w / cell));
const rows = Math.max(6, Math.round(h / cell));
const gx = w / cols;
const gy = h / rows;
const P: number[][] = [];
for (let j = 0; j <= rows; j++) {
const row: number[] = [];
for (let i = 0; i <= cols; i++) {
const edge = i === 0 || j === 0 || i === cols || j === rows;
row.push(i * gx + (edge ? 0 : (Math.random() - 0.5) * gx * 0.85), j * gy + (edge ? 0 : (Math.random() - 0.5) * gy * 0.85));
}
P.push(row);
}
const at = (i: number, j: number) => [P[j][i * 2], P[j][i * 2 + 1]];
const covered = (pts: number[]) => {
const xs = [pts[0], pts[2], pts[4]];
const ys = [pts[1], pts[3], pts[5]];
const x0 = Math.max(0, Math.floor(Math.min(...xs) * dpr));
const x1 = Math.min(textCanvas.width - 1, Math.ceil(Math.max(...xs) * dpr));
const y0 = Math.max(0, Math.floor(Math.min(...ys) * dpr));
const y1 = Math.min(textCanvas.height - 1, Math.ceil(Math.max(...ys) * dpr));
for (let y = y0; y <= y1; y += 3)
for (let x = x0; x <= x1; x += 3) if (data[(y * textCanvas.width + x) * 4 + 3] > 40) return true;
return false;
};
const shards: Shard[] = [];
for (let j = 0; j < rows; j++)
for (let i = 0; i < cols; i++) {
const a = at(i, j), b = at(i + 1, j), c = at(i + 1, j + 1), d = at(i, j + 1);
const tris = Math.random() < 0.5 ? [[...a, ...b, ...c], [...a, ...c, ...d]] : [[...a, ...b, ...d], [...b, ...c, ...d]];
for (const pts of tris) {
if (!covered(pts)) continue;
const xs = [pts[0], pts[2], pts[4]];
const ys = [pts[1], pts[3], pts[5]];
const bx = Math.min(...xs) - 1;
const by = Math.min(...ys) - 1;
shards.push({
pts,
cx: (xs[0] + xs[1] + xs[2]) / 3,
cy: (ys[0] + ys[1] + ys[2]) / 3,
bx,
by,
bw: Math.max(...xs) - bx + 2,
bh: Math.max(...ys) - by + 2,
hue: Math.random() < 0.5 ? 0 : 1,
dx: 0, dy: 0, rot: 0, lift: 0, vx: 0, vy: 0, vr: 0, vl: 0,
});
}
}
world = { shards, text: textCanvas, w, h, dpr };
if (!raf) draw();
};
/* ---------- one frame: physics, then drawing ---------- */
const physics = (dt: number) => {
if (!world) return;
const L = live.current;
const step = Math.min(dt * 60, 2);
const R = Math.min(world.w, world.h) * 0.42 * L.radius;
const speed = Math.min(40, Math.hypot(ptr.vx, ptr.vy));
ptr.vx *= 0.8;
ptr.vy *= 0.8;
for (const s of world.shards) {
if (ptr.in && !reduced) {
const dx = s.cx + s.dx - ptr.x;
const dy = s.cy + s.dy - ptr.y;
const d = Math.hypot(dx, dy) + 0.01;
if (d < R) {
const k = Math.pow(1 - d / R, 2);
const push = k * (1.6 + speed * 0.12) * L.force;
s.vx += (dx / d) * push * step;
s.vy += (dy / d) * push * step;
s.vr += (dx > 0 ? 1 : -1) * k * 0.01 * step;
s.vl += k * 0.03 * step;
}
}
// springs home
s.vx = (s.vx - s.dx * 0.06 * step) * Math.pow(0.84, step);
s.vy = (s.vy - s.dy * 0.06 * step) * Math.pow(0.84, step);
s.vr = (s.vr - s.rot * 0.07 * step) * Math.pow(0.82, step);
s.vl = (s.vl - s.lift * 0.08 * step) * Math.pow(0.8, step);
s.dx += s.vx * step;
s.dy += s.vy * step;
s.rot += s.vr * step;
s.lift += s.vl * step;
}
};
const path = (s: Shard) => {
ctx.beginPath();
ctx.moveTo(s.pts[0], s.pts[1]);
ctx.lineTo(s.pts[2], s.pts[3]);
ctx.lineTo(s.pts[4], s.pts[5]);
ctx.closePath();
};
const draw = () => {
if (!world) return;
const L = live.current;
const wld = world;
ctx.setTransform(wld.dpr, 0, 0, wld.dpr, 0, 0);
ctx.clearRect(0, 0, wld.w, wld.h);
const R = Math.min(wld.w, wld.h) * 0.42 * L.radius;
const cracks = [L.crackA, L.crackB];
// The resting word is drawn whole, so it has no hairline seams between
// shards. Only shards that are actually moving are cut out of it and
// drawn on top, in their new pose.
const moving = (s: Shard) => Math.abs(s.dx) + Math.abs(s.dy) > 0.25 || Math.abs(s.rot) > 0.002 || s.lift > 0.004;
ctx.drawImage(wld.text, 0, 0, wld.text.width, wld.text.height, 0, 0, wld.w, wld.h);
const active = wld.shards.filter(moving);
if (active.length) {
ctx.save();
ctx.globalCompositeOperation = "destination-out";
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
for (const s of active) {
path(s);
ctx.fill();
ctx.stroke(); // covers the anti-aliased rim of the hole
}
ctx.restore();
}
// fracture lines preview around the cursor on the resting glass
if (ptr.in && !reduced) {
ctx.lineWidth = 1;
ctx.lineJoin = "round";
for (const s of wld.shards) {
if (moving(s)) continue;
const near = Math.max(0, 1 - Math.hypot(s.cx - ptr.x, s.cy - ptr.y) / (R * 1.15)) * 0.4;
if (near < 0.06) continue;
path(s);
ctx.strokeStyle = rgba(cracks[s.hue], near);
ctx.stroke();
}
}
// back to front by lift, so raised shards overlap the rest
const order = active.sort((a, b) => a.lift - b.lift);
for (const s of order) {
const lift = Math.max(0, s.lift);
const scale = 1 + lift * 0.35;
const place = () => {
ctx.translate(s.cx + s.dx, s.cy + s.dy);
ctx.rotate(s.rot);
ctx.scale(scale, scale);
ctx.translate(-s.cx, -s.cy);
};
// a raised shard casts a soft black shadow onto the surface
if (lift > 0.02) {
ctx.save();
ctx.translate(lift * 6, lift * 12);
place();
path(s);
ctx.fillStyle = `rgba(0,0,0,${Math.min(0.5, lift * 0.55)})`;
ctx.fill();
ctx.restore();
}
ctx.save();
place();
path(s);
ctx.save();
ctx.clip();
ctx.drawImage(wld.text, s.bx * wld.dpr, s.by * wld.dpr, s.bw * wld.dpr, s.bh * wld.dpr, s.bx, s.by, s.bw, s.bh);
// a tilted shard catches the studio light across its face
const tilt = Math.min(1, Math.abs(s.rot) * 3 + lift * 0.8);
if (tilt > 0.03) {
const sheen = ctx.createLinearGradient(s.bx, s.by, s.bx + s.bw, s.by + s.bh);
sheen.addColorStop(0, `rgba(255,255,255,${0.32 * tilt})`);
sheen.addColorStop(0.55, "rgba(255,255,255,0)");
ctx.fillStyle = sheen;
ctx.fillRect(s.bx, s.by, s.bw, s.bh);
}
ctx.restore();
// cracked edges flash while displaced
const disp = Math.min(1, (Math.abs(s.dx) + Math.abs(s.dy)) / 30 + Math.abs(s.rot) * 2);
let near = 0;
if (ptr.in && !reduced) {
const d = Math.hypot(s.cx + s.dx - ptr.x, s.cy + s.dy - ptr.y);
near = Math.max(0, 1 - d / (R * 1.15)) * 0.4;
}
const a = Math.max(disp, near);
if (a > 0.06) {
ctx.lineWidth = 1;
ctx.lineJoin = "round";
ctx.strokeStyle = rgba(cracks[s.hue], a);
ctx.stroke();
}
ctx.restore();
}
};
/* ---------- input ---------- */
const move = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
const x = e.clientX - r.left;
const y = e.clientY - r.top;
if (ptr.in) {
ptr.vx = x - ptr.lx;
ptr.vy = y - ptr.ly;
}
ptr.x = ptr.lx = x;
ptr.y = ptr.ly = y;
ptr.in = true;
};
const leave = () => {
ptr.in = false;
ptr.x = ptr.y = -1e4;
};
const boom = (e: PointerEvent) => {
if (!world || reduced) return;
const L = live.current;
const r = canvas.getBoundingClientRect();
const x = e.clientX - r.left;
const y = e.clientY - r.top;
for (const s of world.shards) {
const dx = s.cx + s.dx - x;
const dy = s.cy + s.dy - y;
const d = Math.hypot(dx, dy) + 1;
const f = (2600 / (d + 60)) * L.blast;
s.vx += (dx / d) * f + (Math.random() - 0.5) * 6 * L.blast;
s.vy += (dy / d) * f + (Math.random() - 0.5) * 6 * L.blast;
s.vr += (Math.random() - 0.5) * 0.6 * L.blast;
s.vl += (0.4 + Math.random() * 0.4) * Math.min(1.6, L.blast);
}
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", boom);
/* ---------- lifecycle ---------- */
build();
rebuild.current = build;
document.fonts?.ready.then(build);
let lastW = canvas.clientWidth;
let lastH = canvas.clientHeight;
const ro = new ResizeObserver(() => {
if (Math.abs(canvas.clientWidth - lastW) > 2 || Math.abs(canvas.clientHeight - lastH) > 2) {
lastW = canvas.clientWidth;
lastH = canvas.clientHeight;
build();
}
});
ro.observe(canvas);
const frame = (now: number) => {
raf = requestAnimationFrame(frame);
const dt = last ? Math.min(0.05, (now - last) / 1000) : 1 / 60;
last = now;
physics(dt);
draw();
};
const sync = () => {
const run = visible && !document.hidden;
if (run && !raf) {
last = 0;
raf = requestAnimationFrame(frame);
} else if (!run && raf) {
cancelAnimationFrame(raf);
raf = 0;
}
};
const io = new IntersectionObserver(([e]) => {
visible = e.isIntersecting;
sync();
});
io.observe(host);
document.addEventListener("visibilitychange", sync);
return () => {
rebuild.current = null;
cancelAnimationFrame(raf);
io.disconnect();
ro.disconnect();
document.removeEventListener("visibilitychange", sync);
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", boom);
};
}, []);
return (
<div ref={hostRef} className={["overflow-hidden", className].filter(Boolean).join(" ")}>
<canvas ref={canvasRef} aria-hidden="true" style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block" }} />
<span
style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0 0 0 0)", whiteSpace: "nowrap" }}
>
{text}
</span>
</div>
);
}
›Plain CSS variant (no Tailwind)
components/inreact/shatter-type.tsx
"use client";
import { useEffect, useRef } from "react";
import "./shatter-type.css";
/**
* Shatter Type: a word cut into triangular glass shards.
*
* Bring the cursor close and fracture lines appear; push into it and shards
* lift, spin, cast shadows and flash their cracked edges, then spring home.
* Click to blow the whole word apart; it reassembles itself.
*
* <ShatterType text="BREAK" className="h-[360px] w-full" />
*
* The word uses the component's font (set font-family on it or a parent).
* Canvas 2D, no dependencies. Pauses off-screen; with prefers-reduced-motion
* the word stays whole. The text is exposed to screen readers.
*/
export interface ShatterTypeProps {
/** The word or short phrase to shatter. */
text?: string;
/** Top colour of the type. */
color?: string;
/** Bottom colour of the type (a soft vertical fade). */
colorBottom?: string;
/** Font weight of the type. */
weight?: number;
/** First crack colour. */
crackA?: string;
/** Second crack colour. */
crackB?: string;
/** Approximate shard size in px. Smaller = more, finer shards. */
shardSize?: number;
/** How far around the cursor the glass reacts. */
radius?: number;
/** How hard the cursor pushes shards away. */
force?: number;
/** How violently a click blows the word apart. */
blast?: number;
className?: string;
}
interface Shard {
pts: number[]; // triangle x,y pairs (rest pose)
cx: number;
cy: number;
bx: number; // bbox in css px
by: number;
bw: number;
bh: number;
hue: 0 | 1; // which crack colour its edges flash
dx: number;
dy: number;
rot: number;
lift: number;
vx: number;
vy: number;
vr: number;
vl: number;
}
interface World {
shards: Shard[];
text: HTMLCanvasElement;
w: number;
h: number;
dpr: number;
}
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function rgba(hex: string, a: number) {
const h = hex.replace("#", "");
const n = parseInt(h.length === 3 ? h.replace(/./g, "$&$&") : h, 16);
return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})`;
}
export function ShatterType({
text = "BREAK",
color = "#f2f0eb",
colorBottom = "#b9b6ae",
weight = 900,
crackA = "#ff4d00",
crackB = "#4d7bff",
shardSize = 34,
radius = 1,
force = 1,
blast = 1,
className,
}: ShatterTypeProps) {
const hostRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
// live settings: physics reads them every frame; the look rebuilds the shards
const live = useRef({ text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast });
const rebuild = useRef<(() => void) | null>(null);
useEffect(() => {
live.current = { text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast };
}, [text, color, colorBottom, weight, crackA, crackB, shardSize, radius, force, blast]);
useEffect(() => {
rebuild.current?.();
}, [text, color, colorBottom, weight, shardSize]);
useEffect(() => {
const host = hostRef.current;
const canvas = canvasRef.current;
if (!host || !canvas) return;
if (getComputedStyle(host).position === "static") host.style.position = "relative";
const ctx = canvas.getContext("2d")!;
const reduced = isReducedMotion();
let world: World | null = null;
const ptr = { x: -1e4, y: -1e4, in: false, vx: 0, vy: 0, lx: 0, ly: 0 };
let raf = 0;
let last = 0;
let visible = false;
/* ---------- cut the word into shards ---------- */
const build = () => {
const L = live.current;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const w = Math.max(1, canvas.clientWidth);
const h = Math.max(1, canvas.clientHeight);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
// pre-render the word once
const textCanvas = document.createElement("canvas");
textCanvas.width = canvas.width;
textCanvas.height = canvas.height;
const t = textCanvas.getContext("2d", { willReadFrequently: true })!;
t.setTransform(dpr, 0, 0, dpr, 0, 0);
const family = getComputedStyle(host).fontFamily || "Arial";
const word = L.text.trim() || " ";
let size = h * 0.5;
t.font = `${L.weight} ${size}px ${family}`;
const fit = (w * 0.86) / Math.max(1, t.measureText(word).width);
if (fit < 1) size *= fit;
t.font = `${L.weight} ${size}px ${family}`;
t.textAlign = "center";
t.textBaseline = "middle";
const g = t.createLinearGradient(0, h * 0.2, 0, h * 0.7);
g.addColorStop(0, L.color);
g.addColorStop(1, L.colorBottom);
t.fillStyle = g;
t.fillText(word, w / 2, h * 0.42);
// jittered grid, each cell split along a random diagonal: shards read as broken glass
const data = t.getImageData(0, 0, textCanvas.width, textCanvas.height).data;
const cell = Math.max(14, L.shardSize);
const cols = Math.max(10, Math.round(w / cell));
const rows = Math.max(6, Math.round(h / cell));
const gx = w / cols;
const gy = h / rows;
const P: number[][] = [];
for (let j = 0; j <= rows; j++) {
const row: number[] = [];
for (let i = 0; i <= cols; i++) {
const edge = i === 0 || j === 0 || i === cols || j === rows;
row.push(i * gx + (edge ? 0 : (Math.random() - 0.5) * gx * 0.85), j * gy + (edge ? 0 : (Math.random() - 0.5) * gy * 0.85));
}
P.push(row);
}
const at = (i: number, j: number) => [P[j][i * 2], P[j][i * 2 + 1]];
const covered = (pts: number[]) => {
const xs = [pts[0], pts[2], pts[4]];
const ys = [pts[1], pts[3], pts[5]];
const x0 = Math.max(0, Math.floor(Math.min(...xs) * dpr));
const x1 = Math.min(textCanvas.width - 1, Math.ceil(Math.max(...xs) * dpr));
const y0 = Math.max(0, Math.floor(Math.min(...ys) * dpr));
const y1 = Math.min(textCanvas.height - 1, Math.ceil(Math.max(...ys) * dpr));
for (let y = y0; y <= y1; y += 3)
for (let x = x0; x <= x1; x += 3) if (data[(y * textCanvas.width + x) * 4 + 3] > 40) return true;
return false;
};
const shards: Shard[] = [];
for (let j = 0; j < rows; j++)
for (let i = 0; i < cols; i++) {
const a = at(i, j), b = at(i + 1, j), c = at(i + 1, j + 1), d = at(i, j + 1);
const tris = Math.random() < 0.5 ? [[...a, ...b, ...c], [...a, ...c, ...d]] : [[...a, ...b, ...d], [...b, ...c, ...d]];
for (const pts of tris) {
if (!covered(pts)) continue;
const xs = [pts[0], pts[2], pts[4]];
const ys = [pts[1], pts[3], pts[5]];
const bx = Math.min(...xs) - 1;
const by = Math.min(...ys) - 1;
shards.push({
pts,
cx: (xs[0] + xs[1] + xs[2]) / 3,
cy: (ys[0] + ys[1] + ys[2]) / 3,
bx,
by,
bw: Math.max(...xs) - bx + 2,
bh: Math.max(...ys) - by + 2,
hue: Math.random() < 0.5 ? 0 : 1,
dx: 0, dy: 0, rot: 0, lift: 0, vx: 0, vy: 0, vr: 0, vl: 0,
});
}
}
world = { shards, text: textCanvas, w, h, dpr };
if (!raf) draw();
};
/* ---------- one frame: physics, then drawing ---------- */
const physics = (dt: number) => {
if (!world) return;
const L = live.current;
const step = Math.min(dt * 60, 2);
const R = Math.min(world.w, world.h) * 0.42 * L.radius;
const speed = Math.min(40, Math.hypot(ptr.vx, ptr.vy));
ptr.vx *= 0.8;
ptr.vy *= 0.8;
for (const s of world.shards) {
if (ptr.in && !reduced) {
const dx = s.cx + s.dx - ptr.x;
const dy = s.cy + s.dy - ptr.y;
const d = Math.hypot(dx, dy) + 0.01;
if (d < R) {
const k = Math.pow(1 - d / R, 2);
const push = k * (1.6 + speed * 0.12) * L.force;
s.vx += (dx / d) * push * step;
s.vy += (dy / d) * push * step;
s.vr += (dx > 0 ? 1 : -1) * k * 0.01 * step;
s.vl += k * 0.03 * step;
}
}
// springs home
s.vx = (s.vx - s.dx * 0.06 * step) * Math.pow(0.84, step);
s.vy = (s.vy - s.dy * 0.06 * step) * Math.pow(0.84, step);
s.vr = (s.vr - s.rot * 0.07 * step) * Math.pow(0.82, step);
s.vl = (s.vl - s.lift * 0.08 * step) * Math.pow(0.8, step);
s.dx += s.vx * step;
s.dy += s.vy * step;
s.rot += s.vr * step;
s.lift += s.vl * step;
}
};
const path = (s: Shard) => {
ctx.beginPath();
ctx.moveTo(s.pts[0], s.pts[1]);
ctx.lineTo(s.pts[2], s.pts[3]);
ctx.lineTo(s.pts[4], s.pts[5]);
ctx.closePath();
};
const draw = () => {
if (!world) return;
const L = live.current;
const wld = world;
ctx.setTransform(wld.dpr, 0, 0, wld.dpr, 0, 0);
ctx.clearRect(0, 0, wld.w, wld.h);
const R = Math.min(wld.w, wld.h) * 0.42 * L.radius;
const cracks = [L.crackA, L.crackB];
// The resting word is drawn whole, so it has no hairline seams between
// shards. Only shards that are actually moving are cut out of it and
// drawn on top, in their new pose.
const moving = (s: Shard) => Math.abs(s.dx) + Math.abs(s.dy) > 0.25 || Math.abs(s.rot) > 0.002 || s.lift > 0.004;
ctx.drawImage(wld.text, 0, 0, wld.text.width, wld.text.height, 0, 0, wld.w, wld.h);
const active = wld.shards.filter(moving);
if (active.length) {
ctx.save();
ctx.globalCompositeOperation = "destination-out";
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
for (const s of active) {
path(s);
ctx.fill();
ctx.stroke(); // covers the anti-aliased rim of the hole
}
ctx.restore();
}
// fracture lines preview around the cursor on the resting glass
if (ptr.in && !reduced) {
ctx.lineWidth = 1;
ctx.lineJoin = "round";
for (const s of wld.shards) {
if (moving(s)) continue;
const near = Math.max(0, 1 - Math.hypot(s.cx - ptr.x, s.cy - ptr.y) / (R * 1.15)) * 0.4;
if (near < 0.06) continue;
path(s);
ctx.strokeStyle = rgba(cracks[s.hue], near);
ctx.stroke();
}
}
// back to front by lift, so raised shards overlap the rest
const order = active.sort((a, b) => a.lift - b.lift);
for (const s of order) {
const lift = Math.max(0, s.lift);
const scale = 1 + lift * 0.35;
const place = () => {
ctx.translate(s.cx + s.dx, s.cy + s.dy);
ctx.rotate(s.rot);
ctx.scale(scale, scale);
ctx.translate(-s.cx, -s.cy);
};
// a raised shard casts a soft black shadow onto the surface
if (lift > 0.02) {
ctx.save();
ctx.translate(lift * 6, lift * 12);
place();
path(s);
ctx.fillStyle = `rgba(0,0,0,${Math.min(0.5, lift * 0.55)})`;
ctx.fill();
ctx.restore();
}
ctx.save();
place();
path(s);
ctx.save();
ctx.clip();
ctx.drawImage(wld.text, s.bx * wld.dpr, s.by * wld.dpr, s.bw * wld.dpr, s.bh * wld.dpr, s.bx, s.by, s.bw, s.bh);
// a tilted shard catches the studio light across its face
const tilt = Math.min(1, Math.abs(s.rot) * 3 + lift * 0.8);
if (tilt > 0.03) {
const sheen = ctx.createLinearGradient(s.bx, s.by, s.bx + s.bw, s.by + s.bh);
sheen.addColorStop(0, `rgba(255,255,255,${0.32 * tilt})`);
sheen.addColorStop(0.55, "rgba(255,255,255,0)");
ctx.fillStyle = sheen;
ctx.fillRect(s.bx, s.by, s.bw, s.bh);
}
ctx.restore();
// cracked edges flash while displaced
const disp = Math.min(1, (Math.abs(s.dx) + Math.abs(s.dy)) / 30 + Math.abs(s.rot) * 2);
let near = 0;
if (ptr.in && !reduced) {
const d = Math.hypot(s.cx + s.dx - ptr.x, s.cy + s.dy - ptr.y);
near = Math.max(0, 1 - d / (R * 1.15)) * 0.4;
}
const a = Math.max(disp, near);
if (a > 0.06) {
ctx.lineWidth = 1;
ctx.lineJoin = "round";
ctx.strokeStyle = rgba(cracks[s.hue], a);
ctx.stroke();
}
ctx.restore();
}
};
/* ---------- input ---------- */
const move = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
const x = e.clientX - r.left;
const y = e.clientY - r.top;
if (ptr.in) {
ptr.vx = x - ptr.lx;
ptr.vy = y - ptr.ly;
}
ptr.x = ptr.lx = x;
ptr.y = ptr.ly = y;
ptr.in = true;
};
const leave = () => {
ptr.in = false;
ptr.x = ptr.y = -1e4;
};
const boom = (e: PointerEvent) => {
if (!world || reduced) return;
const L = live.current;
const r = canvas.getBoundingClientRect();
const x = e.clientX - r.left;
const y = e.clientY - r.top;
for (const s of world.shards) {
const dx = s.cx + s.dx - x;
const dy = s.cy + s.dy - y;
const d = Math.hypot(dx, dy) + 1;
const f = (2600 / (d + 60)) * L.blast;
s.vx += (dx / d) * f + (Math.random() - 0.5) * 6 * L.blast;
s.vy += (dy / d) * f + (Math.random() - 0.5) * 6 * L.blast;
s.vr += (Math.random() - 0.5) * 0.6 * L.blast;
s.vl += (0.4 + Math.random() * 0.4) * Math.min(1.6, L.blast);
}
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", boom);
/* ---------- lifecycle ---------- */
build();
rebuild.current = build;
document.fonts?.ready.then(build);
let lastW = canvas.clientWidth;
let lastH = canvas.clientHeight;
const ro = new ResizeObserver(() => {
if (Math.abs(canvas.clientWidth - lastW) > 2 || Math.abs(canvas.clientHeight - lastH) > 2) {
lastW = canvas.clientWidth;
lastH = canvas.clientHeight;
build();
}
});
ro.observe(canvas);
const frame = (now: number) => {
raf = requestAnimationFrame(frame);
const dt = last ? Math.min(0.05, (now - last) / 1000) : 1 / 60;
last = now;
physics(dt);
draw();
};
const sync = () => {
const run = visible && !document.hidden;
if (run && !raf) {
last = 0;
raf = requestAnimationFrame(frame);
} else if (!run && raf) {
cancelAnimationFrame(raf);
raf = 0;
}
};
const io = new IntersectionObserver(([e]) => {
visible = e.isIntersecting;
sync();
});
io.observe(host);
document.addEventListener("visibilitychange", sync);
return () => {
rebuild.current = null;
cancelAnimationFrame(raf);
io.disconnect();
ro.disconnect();
document.removeEventListener("visibilitychange", sync);
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", boom);
};
}, []);
return (
<div ref={hostRef} className={["shatter-type", className].filter(Boolean).join(" ")}>
<canvas ref={canvasRef} aria-hidden="true" style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block" }} />
<span
style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0 0 0 0)", whiteSpace: "nowrap" }}
>
{text}
</span>
</div>
);
}
components/inreact/shatter-type.css
.shatter-type {
overflow: hidden;
}
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
11 propstextstringThe word or short phrase to shatter. Uses the component's font; exposed to screen readers.default "BREAK"default "BREAK"colorstringTop colour of the type.default "#f2f0eb"default "#f2f0eb"colorBottomstringBottom colour of the type, a soft vertical fade.default "#b9b6ae"default "#b9b6ae"weightnumberFont weight of the type.default 900default 900crackAstringFirst crack colour.default "#ff4d00"default "#ff4d00"crackBstringSecond crack colour.default "#4d7bff"default "#4d7bff"shardSizenumberApproximate shard size in px. Smaller means more, finer shards.default 34default 34radiusnumberHow far around the cursor the glass reacts.default 1default 1forcenumberHow hard the cursor pushes shards away.default 1default 1blastnumberHow violently a click blows the word apart.default 1default 1classNamestringSize it, e.g. h-[360px] w-full.