Text Animations118 / 118
Gravity Type
A giant word made of rigid bodies. When it scrolls into view the letters drop in and pile up. Grab one and fling it, or sweep your cursor fast to kick them around; they squash when they land hard and throw longer shadows as they rise. Double-click to drop the word again.
Matter.js physicsMatter.js physicsDrag and flingDrag and flingTouch friendlyTouch friendly60fps60fpsReduced-motion safeReduced-motion safe
inReact
Grab and fling a letterDouble-click to drop again
Tweak
LIVELIVEType
800
Colour
#ff4d00
#0a0a0a
#f2f0eb
2
#2b5cff
Physics
1.25
0.12
Shadows
Letters cast soft shadows
Usage
Follows your tweaks.
usage.tsx
import { GravityType } from "@/components/inreact/gravity-type";
export function Footer() {
return (
<footer style={{ background: "#ff4d00" }}>
<GravityType
className="h-[420px]"
text="inReact"
/>
</footer>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/gravity-type.jsonTS + CSS
npx shadcn@latest add https://inreact.vercel.app/r/gravity-type-css.jsonOr copy the source
Install the dependencies, then save this as components/inreact/gravity-type.tsx.
terminal
npm install matter-jscomponents/inreact/gravity-type.tsx
"use client";
import Matter from "matter-js";
import { useEffect, useRef } from "react";
/**
* Gravity Type: a giant word made of rigid bodies.
*
* When it scrolls into view the letters drop in and pile up. Grab one and
* fling it, or sweep the cursor fast to kick them around; letters squash
* when they land hard and cast soft shadows as they rise. Double-click to
* drop the word again. On touch screens a tap knocks a letter flying while
* the page still scrolls.
*
* <GravityType text="inReact" className="h-[420px] w-full" />
*
* Uses matter-js. The word uses the component's font (set font-family on it
* or a parent). Pauses off-screen; with prefers-reduced-motion the word sits
* still. The text is exposed to screen readers.
*/
export interface GravityTypeProps {
/** The word to drop. */
text?: string;
/** Default letter colour. */
color?: string;
/** Per-letter colours by index (falls back to `color`). */
colors?: (string | undefined)[];
/** Colour of a letter while you hold it. */
grabColor?: string;
/** Font weight. */
weight?: number;
/** Gravity strength. */
gravity?: number;
/** How bouncy the letters are (0 to 1). */
bounce?: number;
/** Soft shadows under the letters. */
shadows?: boolean;
className?: string;
}
interface Glyph {
ch: string;
w: number;
h: number;
left: number;
asc: number;
advance: number;
}
type LetterBody = Matter.Body & { glyph: number; squash: number };
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const finePointer = () => typeof window !== "undefined" && window.matchMedia("(pointer: fine)").matches;
export function GravityType({
text = "inReact",
color = "#0a0a0a",
colors = ["#f2f0eb", "#f2f0eb"],
grabColor = "#2b5cff",
weight = 800,
gravity = 1.25,
bounce = 0.12,
shadows = true,
className,
}: GravityTypeProps) {
const hostRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
// live: read every frame; physics settings apply to letters as they are made
const live = useRef({ color, colors, grabColor, gravity, bounce, shadows });
const tune = useRef<(() => void) | null>(null);
useEffect(() => {
live.current = { color, colors, grabColor, gravity, bounce, shadows };
tune.current?.();
});
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();
const fine = finePointer();
const { Engine, Bodies, Body, Composite, Constraint, Query, Events } = Matter;
const engine = Engine.create({ gravity: { x: 0, y: live.current.gravity } });
let w = 0, h = 0, dpr = 1, size = 100;
// the letters rest a little above the canvas edge, so their shadows fade out fully
let floor = 0;
let font = "";
let glyphs: Glyph[] = [];
let bodies: LetterBody[] = [];
let walls: Matter.Body[] = [];
let dropped = false;
let grabbed: Matter.Body | null = null;
tune.current = () => {
engine.gravity.y = live.current.gravity;
for (const b of bodies) b.restitution = live.current.bounce;
};
const measure = () => {
const r = host.getBoundingClientRect();
w = r.width;
h = r.height;
dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
const family = getComputedStyle(host).fontFamily || "Arial";
size = Math.min((w * 1.7) / Math.max(5, text.length), h * 0.9);
font = `${weight} ${size}px ${family}`;
floor = h - Math.max(28, Math.min(64, size * 0.16));
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.font = font;
glyphs = Array.from(text).map((ch) => {
const m = ctx.measureText(ch);
return {
ch,
w: Math.max(4, m.actualBoundingBoxLeft + m.actualBoundingBoxRight),
h: Math.max(4, m.actualBoundingBoxAscent + m.actualBoundingBoxDescent),
left: m.actualBoundingBoxLeft,
asc: m.actualBoundingBoxAscent,
advance: m.width - size * 0.06,
};
});
};
const buildWalls = () => {
Composite.remove(engine.world, walls);
const t = 400;
walls = [
Bodies.rectangle(w / 2, floor + t / 2, w * 3, t, { isStatic: true }),
Bodies.rectangle(-t / 2, h / 2 - h, t, h * 4, { isStatic: true }),
Bodies.rectangle(w + t / 2, h / 2 - h, t, h * 4, { isStatic: true }),
];
Composite.add(engine.world, walls);
};
/** Lay the word out and drop it from above, letter by letter. */
const drop = (immediate = false) => {
Composite.remove(engine.world, bodies);
const total = glyphs.reduce((s, g) => s + g.advance, 0);
let x = (w - total) / 2;
bodies = glyphs
.map((g, i) => {
const cx = x + g.w / 2;
x += g.advance;
if (!g.ch.trim()) return null; // spaces take room but don't fall
const restY = floor - g.h / 2 - 2;
const y = immediate ? restY : -g.h - i * h * 0.35 - Math.random() * 60;
const b = Bodies.rectangle(cx, y, g.w, g.h, {
chamfer: { radius: Math.min(g.w, g.h) * 0.08 },
restitution: live.current.bounce,
friction: 0.6,
frictionAir: 0.012,
density: 0.0018,
angle: immediate ? 0 : (Math.random() - 0.5) * 0.18,
}) as LetterBody;
b.glyph = i;
b.squash = 0;
return b;
})
.filter((b): b is LetterBody => b !== null);
Composite.add(engine.world, bodies);
};
const rebuild = () => {
measure();
buildWalls();
if (dropped) drop(true);
};
rebuild();
// a hard landing squashes the letter for a moment
Events.on(engine, "collisionStart", (ev) => {
for (const pair of ev.pairs) {
for (const b of [pair.bodyA, pair.bodyB] as LetterBody[]) {
if (b.glyph === undefined) continue;
const v = Math.hypot(b.velocity.x, b.velocity.y);
if (v > 4) b.squash = Math.min(1, b.squash + v / 22);
}
}
});
/* ---------- interaction ---------- */
let drag: Matter.Constraint | null = null;
let lx = 0, ly = 0, lt = 0;
const local = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const move = (e: PointerEvent) => {
if (reduced || !dropped) return;
const p = local(e);
if (drag) {
drag.pointA = p;
return;
}
// the grab cursor only over a letter
canvas.style.cursor = Query.point(bodies, p).length ? "grab" : "default";
const now = performance.now();
const dt = Math.max(8, now - lt);
const vx = (p.x - lx) / dt;
const vy = (p.y - ly) / dt;
lx = p.x;
ly = p.y;
lt = now;
const speed = Math.hypot(vx, vy);
if (speed < 0.6 || grabbed) return;
// sweep fast to kick letters
for (const b of bodies) {
const dx = b.position.x - p.x;
const dy = b.position.y - p.y;
const d = Math.hypot(dx, dy);
if (d < 140) {
const k = (1 - d / 140) * Math.min(speed, 4) * 0.0035 * b.mass;
Body.applyForce(b, b.position, { x: vx * k, y: vy * k - k * 0.6 });
}
}
};
const down = (e: PointerEvent) => {
if (reduced || !dropped) return;
const p = local(e);
const hit = Query.point(bodies, p)[0];
if (!hit) return;
if (!fine) {
// touch: knock the letter flying, keep the page scrollable
Body.setVelocity(hit, { x: (Math.random() - 0.5) * 14, y: -18 });
Body.setAngularVelocity(hit, (Math.random() - 0.5) * 0.5);
return;
}
canvas.setPointerCapture(e.pointerId);
canvas.style.cursor = "grabbing";
const local0 = Matter.Vector.rotate(Matter.Vector.sub(p, hit.position), -hit.angle);
drag = Constraint.create({ pointA: p, bodyB: hit, pointB: local0, stiffness: 0.2, damping: 0.08, length: 0 });
Composite.add(engine.world, drag);
grabbed = hit;
};
const up = (e: PointerEvent) => {
if (drag) {
Composite.remove(engine.world, drag);
drag = null;
grabbed = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(e.pointerId)) canvas.releasePointerCapture(e.pointerId);
}
};
const redrop = () => !reduced && drop(false);
canvas.addEventListener("pointermove", move);
canvas.addEventListener("pointerdown", down);
canvas.addEventListener("pointerup", up);
canvas.addEventListener("pointercancel", up);
canvas.addEventListener("dblclick", redrop);
/* ---------- render ---------- */
const draw = () => {
const L = live.current;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, w, h);
ctx.font = font;
ctx.textBaseline = "alphabetic";
ctx.textAlign = "left";
for (const b of bodies) {
const g = glyphs[b.glyph];
// squash springs back to shape
b.squash *= 0.86;
const sq = b.squash;
ctx.save();
ctx.translate(b.position.x, b.position.y);
ctx.rotate(b.angle);
ctx.scale(1 + sq * 0.12, 1 - sq * 0.14);
if (L.shadows) {
// a letter lifted off the pile throws a longer, softer shadow
const lift = Math.max(0, Math.min(1, (floor - (b.position.y + g.h / 2)) / (h * 0.6)));
ctx.shadowColor = `rgba(0,0,0,${0.32 - lift * 0.12})`;
ctx.shadowBlur = 12 + lift * 30;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 8 + lift * 26;
}
ctx.fillStyle = b === grabbed ? L.grabColor : (L.colors[b.glyph] ?? L.color);
ctx.fillText(g.ch, -g.w / 2 + g.left, -g.h / 2 + g.asc);
ctx.restore();
}
};
/* ---------- loop ---------- */
let raf = 0;
let visible = false;
let last = 0;
let acc = 0;
const STEP = 1000 / 60;
const frame = (now: number) => {
raf = requestAnimationFrame(frame);
acc += Math.min(100, last ? now - last : STEP);
last = now;
while (acc >= STEP) {
Engine.update(engine, STEP);
acc -= STEP;
}
// letters that escape get dropped back in
for (const b of bodies) {
if (b.position.y > h + 600 || b.position.x < -400 || b.position.x > w + 400) {
Body.setPosition(b, { x: w / 2, y: -200 });
Body.setVelocity(b, { x: 0, y: 0 });
}
}
draw();
};
const sync = () => {
const go = visible && !document.hidden && !reduced;
if (go && !raf) {
last = 0;
raf = requestAnimationFrame(frame);
} else if (!go && raf) {
cancelAnimationFrame(raf);
raf = 0;
}
};
const io = new IntersectionObserver(
([e]) => {
visible = e.isIntersecting;
if (visible && !dropped) {
dropped = true;
drop(reduced);
if (reduced) draw();
}
sync();
},
{ threshold: 0.25 },
);
io.observe(host);
document.addEventListener("visibilitychange", sync);
let lastW = w;
const ro = new ResizeObserver(() => {
const r = host.getBoundingClientRect();
if (Math.abs(r.width - lastW) > 2) {
lastW = r.width;
rebuild();
if (reduced) draw();
}
});
ro.observe(host);
document.fonts?.ready.then(() => {
rebuild();
if (reduced && dropped) draw();
});
return () => {
tune.current = null;
cancelAnimationFrame(raf);
io.disconnect();
ro.disconnect();
document.removeEventListener("visibilitychange", sync);
canvas.removeEventListener("pointermove", move);
canvas.removeEventListener("pointerdown", down);
canvas.removeEventListener("pointerup", up);
canvas.removeEventListener("pointercancel", up);
canvas.removeEventListener("dblclick", redrop);
Events.off(engine, "collisionStart");
Composite.clear(engine.world, false);
Engine.clear(engine);
};
}, [text, weight]);
return (
<div ref={hostRef} className={["w-full", className].filter(Boolean).join(" ")}>
<span style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0 0 0 0)", whiteSpace: "nowrap" }}>{text}</span>
<canvas
ref={canvasRef}
aria-hidden="true"
style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block", touchAction: "pan-y" }}
/>
</div>
);
}
›Plain CSS variant (no Tailwind)
components/inreact/gravity-type.tsx
"use client";
import Matter from "matter-js";
import { useEffect, useRef } from "react";
import "./gravity-type.css";
/**
* Gravity Type: a giant word made of rigid bodies.
*
* When it scrolls into view the letters drop in and pile up. Grab one and
* fling it, or sweep the cursor fast to kick them around; letters squash
* when they land hard and cast soft shadows as they rise. Double-click to
* drop the word again. On touch screens a tap knocks a letter flying while
* the page still scrolls.
*
* <GravityType text="inReact" className="h-[420px] w-full" />
*
* Uses matter-js. The word uses the component's font (set font-family on it
* or a parent). Pauses off-screen; with prefers-reduced-motion the word sits
* still. The text is exposed to screen readers.
*/
export interface GravityTypeProps {
/** The word to drop. */
text?: string;
/** Default letter colour. */
color?: string;
/** Per-letter colours by index (falls back to `color`). */
colors?: (string | undefined)[];
/** Colour of a letter while you hold it. */
grabColor?: string;
/** Font weight. */
weight?: number;
/** Gravity strength. */
gravity?: number;
/** How bouncy the letters are (0 to 1). */
bounce?: number;
/** Soft shadows under the letters. */
shadows?: boolean;
className?: string;
}
interface Glyph {
ch: string;
w: number;
h: number;
left: number;
asc: number;
advance: number;
}
type LetterBody = Matter.Body & { glyph: number; squash: number };
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const finePointer = () => typeof window !== "undefined" && window.matchMedia("(pointer: fine)").matches;
export function GravityType({
text = "inReact",
color = "#0a0a0a",
colors = ["#f2f0eb", "#f2f0eb"],
grabColor = "#2b5cff",
weight = 800,
gravity = 1.25,
bounce = 0.12,
shadows = true,
className,
}: GravityTypeProps) {
const hostRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
// live: read every frame; physics settings apply to letters as they are made
const live = useRef({ color, colors, grabColor, gravity, bounce, shadows });
const tune = useRef<(() => void) | null>(null);
useEffect(() => {
live.current = { color, colors, grabColor, gravity, bounce, shadows };
tune.current?.();
});
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();
const fine = finePointer();
const { Engine, Bodies, Body, Composite, Constraint, Query, Events } = Matter;
const engine = Engine.create({ gravity: { x: 0, y: live.current.gravity } });
let w = 0, h = 0, dpr = 1, size = 100;
// the letters rest a little above the canvas edge, so their shadows fade out fully
let floor = 0;
let font = "";
let glyphs: Glyph[] = [];
let bodies: LetterBody[] = [];
let walls: Matter.Body[] = [];
let dropped = false;
let grabbed: Matter.Body | null = null;
tune.current = () => {
engine.gravity.y = live.current.gravity;
for (const b of bodies) b.restitution = live.current.bounce;
};
const measure = () => {
const r = host.getBoundingClientRect();
w = r.width;
h = r.height;
dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
const family = getComputedStyle(host).fontFamily || "Arial";
size = Math.min((w * 1.7) / Math.max(5, text.length), h * 0.9);
font = `${weight} ${size}px ${family}`;
floor = h - Math.max(28, Math.min(64, size * 0.16));
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.font = font;
glyphs = Array.from(text).map((ch) => {
const m = ctx.measureText(ch);
return {
ch,
w: Math.max(4, m.actualBoundingBoxLeft + m.actualBoundingBoxRight),
h: Math.max(4, m.actualBoundingBoxAscent + m.actualBoundingBoxDescent),
left: m.actualBoundingBoxLeft,
asc: m.actualBoundingBoxAscent,
advance: m.width - size * 0.06,
};
});
};
const buildWalls = () => {
Composite.remove(engine.world, walls);
const t = 400;
walls = [
Bodies.rectangle(w / 2, floor + t / 2, w * 3, t, { isStatic: true }),
Bodies.rectangle(-t / 2, h / 2 - h, t, h * 4, { isStatic: true }),
Bodies.rectangle(w + t / 2, h / 2 - h, t, h * 4, { isStatic: true }),
];
Composite.add(engine.world, walls);
};
/** Lay the word out and drop it from above, letter by letter. */
const drop = (immediate = false) => {
Composite.remove(engine.world, bodies);
const total = glyphs.reduce((s, g) => s + g.advance, 0);
let x = (w - total) / 2;
bodies = glyphs
.map((g, i) => {
const cx = x + g.w / 2;
x += g.advance;
if (!g.ch.trim()) return null; // spaces take room but don't fall
const restY = floor - g.h / 2 - 2;
const y = immediate ? restY : -g.h - i * h * 0.35 - Math.random() * 60;
const b = Bodies.rectangle(cx, y, g.w, g.h, {
chamfer: { radius: Math.min(g.w, g.h) * 0.08 },
restitution: live.current.bounce,
friction: 0.6,
frictionAir: 0.012,
density: 0.0018,
angle: immediate ? 0 : (Math.random() - 0.5) * 0.18,
}) as LetterBody;
b.glyph = i;
b.squash = 0;
return b;
})
.filter((b): b is LetterBody => b !== null);
Composite.add(engine.world, bodies);
};
const rebuild = () => {
measure();
buildWalls();
if (dropped) drop(true);
};
rebuild();
// a hard landing squashes the letter for a moment
Events.on(engine, "collisionStart", (ev) => {
for (const pair of ev.pairs) {
for (const b of [pair.bodyA, pair.bodyB] as LetterBody[]) {
if (b.glyph === undefined) continue;
const v = Math.hypot(b.velocity.x, b.velocity.y);
if (v > 4) b.squash = Math.min(1, b.squash + v / 22);
}
}
});
/* ---------- interaction ---------- */
let drag: Matter.Constraint | null = null;
let lx = 0, ly = 0, lt = 0;
const local = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const move = (e: PointerEvent) => {
if (reduced || !dropped) return;
const p = local(e);
if (drag) {
drag.pointA = p;
return;
}
// the grab cursor only over a letter
canvas.style.cursor = Query.point(bodies, p).length ? "grab" : "default";
const now = performance.now();
const dt = Math.max(8, now - lt);
const vx = (p.x - lx) / dt;
const vy = (p.y - ly) / dt;
lx = p.x;
ly = p.y;
lt = now;
const speed = Math.hypot(vx, vy);
if (speed < 0.6 || grabbed) return;
// sweep fast to kick letters
for (const b of bodies) {
const dx = b.position.x - p.x;
const dy = b.position.y - p.y;
const d = Math.hypot(dx, dy);
if (d < 140) {
const k = (1 - d / 140) * Math.min(speed, 4) * 0.0035 * b.mass;
Body.applyForce(b, b.position, { x: vx * k, y: vy * k - k * 0.6 });
}
}
};
const down = (e: PointerEvent) => {
if (reduced || !dropped) return;
const p = local(e);
const hit = Query.point(bodies, p)[0];
if (!hit) return;
if (!fine) {
// touch: knock the letter flying, keep the page scrollable
Body.setVelocity(hit, { x: (Math.random() - 0.5) * 14, y: -18 });
Body.setAngularVelocity(hit, (Math.random() - 0.5) * 0.5);
return;
}
canvas.setPointerCapture(e.pointerId);
canvas.style.cursor = "grabbing";
const local0 = Matter.Vector.rotate(Matter.Vector.sub(p, hit.position), -hit.angle);
drag = Constraint.create({ pointA: p, bodyB: hit, pointB: local0, stiffness: 0.2, damping: 0.08, length: 0 });
Composite.add(engine.world, drag);
grabbed = hit;
};
const up = (e: PointerEvent) => {
if (drag) {
Composite.remove(engine.world, drag);
drag = null;
grabbed = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(e.pointerId)) canvas.releasePointerCapture(e.pointerId);
}
};
const redrop = () => !reduced && drop(false);
canvas.addEventListener("pointermove", move);
canvas.addEventListener("pointerdown", down);
canvas.addEventListener("pointerup", up);
canvas.addEventListener("pointercancel", up);
canvas.addEventListener("dblclick", redrop);
/* ---------- render ---------- */
const draw = () => {
const L = live.current;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, w, h);
ctx.font = font;
ctx.textBaseline = "alphabetic";
ctx.textAlign = "left";
for (const b of bodies) {
const g = glyphs[b.glyph];
// squash springs back to shape
b.squash *= 0.86;
const sq = b.squash;
ctx.save();
ctx.translate(b.position.x, b.position.y);
ctx.rotate(b.angle);
ctx.scale(1 + sq * 0.12, 1 - sq * 0.14);
if (L.shadows) {
// a letter lifted off the pile throws a longer, softer shadow
const lift = Math.max(0, Math.min(1, (floor - (b.position.y + g.h / 2)) / (h * 0.6)));
ctx.shadowColor = `rgba(0,0,0,${0.32 - lift * 0.12})`;
ctx.shadowBlur = 12 + lift * 30;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 8 + lift * 26;
}
ctx.fillStyle = b === grabbed ? L.grabColor : (L.colors[b.glyph] ?? L.color);
ctx.fillText(g.ch, -g.w / 2 + g.left, -g.h / 2 + g.asc);
ctx.restore();
}
};
/* ---------- loop ---------- */
let raf = 0;
let visible = false;
let last = 0;
let acc = 0;
const STEP = 1000 / 60;
const frame = (now: number) => {
raf = requestAnimationFrame(frame);
acc += Math.min(100, last ? now - last : STEP);
last = now;
while (acc >= STEP) {
Engine.update(engine, STEP);
acc -= STEP;
}
// letters that escape get dropped back in
for (const b of bodies) {
if (b.position.y > h + 600 || b.position.x < -400 || b.position.x > w + 400) {
Body.setPosition(b, { x: w / 2, y: -200 });
Body.setVelocity(b, { x: 0, y: 0 });
}
}
draw();
};
const sync = () => {
const go = visible && !document.hidden && !reduced;
if (go && !raf) {
last = 0;
raf = requestAnimationFrame(frame);
} else if (!go && raf) {
cancelAnimationFrame(raf);
raf = 0;
}
};
const io = new IntersectionObserver(
([e]) => {
visible = e.isIntersecting;
if (visible && !dropped) {
dropped = true;
drop(reduced);
if (reduced) draw();
}
sync();
},
{ threshold: 0.25 },
);
io.observe(host);
document.addEventListener("visibilitychange", sync);
let lastW = w;
const ro = new ResizeObserver(() => {
const r = host.getBoundingClientRect();
if (Math.abs(r.width - lastW) > 2) {
lastW = r.width;
rebuild();
if (reduced) draw();
}
});
ro.observe(host);
document.fonts?.ready.then(() => {
rebuild();
if (reduced && dropped) draw();
});
return () => {
tune.current = null;
cancelAnimationFrame(raf);
io.disconnect();
ro.disconnect();
document.removeEventListener("visibilitychange", sync);
canvas.removeEventListener("pointermove", move);
canvas.removeEventListener("pointerdown", down);
canvas.removeEventListener("pointerup", up);
canvas.removeEventListener("pointercancel", up);
canvas.removeEventListener("dblclick", redrop);
Events.off(engine, "collisionStart");
Composite.clear(engine.world, false);
Engine.clear(engine);
};
}, [text, weight]);
return (
<div ref={hostRef} className={["gravity-type", className].filter(Boolean).join(" ")}>
<span style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0 0 0 0)", whiteSpace: "nowrap" }}>{text}</span>
<canvas
ref={canvasRef}
aria-hidden="true"
style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block", touchAction: "pan-y" }}
/>
</div>
);
}
components/inreact/gravity-type.css
.gravity-type {
width: 100%;
}
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
9 propstextstringThe word to drop. Uses the component's font; exposed to screen readers.default "inReact"default "inReact"colorstringDefault letter colour.default "#0a0a0a"default "#0a0a0a"colorsstring[]Per-letter colours by index; the rest use color.default ["#f2f0eb", "#f2f0eb"]default ["#f2f0eb", "#f2f0eb"]grabColorstringColour of a letter while you hold it.default "#2b5cff"default "#2b5cff"weightnumberFont weight.default 800default 800gravitynumberGravity strength.default 1.25default 1.25bouncenumberHow bouncy the letters are, 0 to 1.default 0.12default 0.12shadowsbooleanSoft shadows under the letters.default truedefault trueclassNamestringGive it a height, e.g. h-[420px].