Animations116 / 118
Digit Knot
A trefoil knot printed all over with flowing digits that wrap round the tube and catch the light. Steer the flow with your cursor, hover to swell the surface like a lens and scramble the digits beneath it, drag to turn it, click to send a pulse along the whole knot.
three.jsthree.jsShaderShaderTypographyTypographyKnotKnotSpringsSpringsGSAPGSAP
Hover to magnify, drag to turnClick to send a pulse
Tweak
LIVELIVECharacters
#f2f2f2
Motion
1.0×
Usage
Follows your tweaks.
usage.tsx
import { DigitKnot } from "@/components/inreact/digit-knot";
export function Hero() {
return (
<section className="h-[600px] bg-black">
<DigitKnot chars="0123456789" color="#f2f2f2" speed={1} />
</section>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/digit-knot.jsonOr copy the source
Install the dependencies, then save this as components/inreact/digit-knot.tsx.
terminal
npm install gsap threecomponents/inreact/digit-knot.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef } from "react";
import * as THREE from "three";
/**
* Digit Knot: a trefoil knot whose skin is a grid of digits flowing along it, rendered live in
* three.js. The digits are mapped onto the tube itself (not drawn over the screen), so they wrap
* round it, foreshorten as it turns away and catch the light like print on a surface.
*
* - The digits flow along the knot; move across the stage to steer how fast, and which way.
* - Hover the knot: the surface under your cursor swells like a lens and its digits brighten and
* scramble.
* - Drag to turn the knot; it coasts like a body in space and settles back to its slow turn.
* - Click (or Enter / Space): a pulse runs along the whole knot from where you clicked, swelling the
* tube and rolling every digit it passes.
*
* <div className="h-[600px] bg-black"><DigitKnot /></div>
*/
export type DigitKnotProps = {
/** The characters printed on the knot, cycled along each row. */
chars?: string;
/** Character colour. */
color?: string;
/** Tube colour. */
tube?: string;
/** Flow speed multiplier. */
speed?: number;
/** Background colour. */
background?: string;
className?: string;
};
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// the trefoil's centre line, matching three's TorusKnotGeometry (p = 2, q = 3, radius 1)
const knotAt = (u: number, out = new THREE.Vector3()) => {
const cu = Math.cos(u), su = Math.sin(u), qu = 1.5 * u, cs = Math.cos(qu);
return out.set((2 + cs) * 0.5 * cu, (2 + cs) * su * 0.5, Math.sin(qu) * 0.5);
};
export function DigitKnot({ chars = "0123456789", color = "#f2f2f2", tube = "#060607", speed = 1, background = "#000000", className }: DigitKnotProps) {
const rootRef = useRef<HTMLDivElement>(null);
const live = useRef({ color, tube, speed });
live.current = { color, tube, speed };
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const still = reduced();
const glyphs = Array.from(chars.length ? chars : "0123456789").slice(0, 32);
const G = glyphs.length;
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;
}
const dpr = Math.min(2, Math.max(1.5, window.devicePixelRatio || 1));
renderer.setPixelRatio(dpr);
renderer.setClearColor(new THREE.Color(background), 1);
const scene = new THREE.Scene();
// ---- the glyph atlas ----
const S = 128;
const atlasC = document.createElement("canvas");
atlasC.width = S * G;
atlasC.height = S;
{
const g = atlasC.getContext("2d")!;
g.fillStyle = "#fff";
g.textAlign = "center";
g.textBaseline = "middle";
g.font = `800 ${Math.round(S * 0.9)}px "Inter", "Helvetica Neue", Arial, sans-serif`;
glyphs.forEach((ch, i) => g.fillText(ch, i * S + S / 2, S * 0.54));
}
const atlas = new THREE.CanvasTexture(atlasC);
atlas.colorSpace = THREE.NoColorSpace;
atlas.minFilter = THREE.LinearMipmapLinearFilter;
atlas.generateMipmaps = true;
atlas.anisotropy = 8;
// ---- the knot ----
const TUBE = 0.36;
const geo = new THREE.TorusKnotGeometry(1, TUBE, 900, 96, 2, 3);
// its centre-line length, so the cells come out square
let len = 0;
{
const a = new THREE.Vector3(), b = new THREE.Vector3();
knotAt(0, a);
for (let i = 1; i <= 2000; i++) {
knotAt((i / 2000) * Math.PI * 4, b);
len += a.distanceTo(b);
a.copy(b);
}
}
const ROWS = 18; // cells round the tube
const COLS = Math.round((len / (2 * Math.PI * TUBE)) * ROWS); // cells along it
const U = {
uAtlas: { value: atlas },
uG: { value: G },
uCells: { value: new THREE.Vector2(COLS, ROWS) },
uFlow: { value: 0 },
uTime: { value: 0 },
uColor: { value: new THREE.Color(color) },
uTube: { value: new THREE.Color(tube) },
uHover: { value: new THREE.Vector2(-9, -9) },
uHoverAmt: { value: 0 },
uPulse: { value: new THREE.Vector2(0, 9) }, // x: along-tube position (0..1), y: age
uAspect: { value: len / (2 * Math.PI * TUBE) },
};
const mat = new THREE.ShaderMaterial({
uniforms: U,
vertexShader: `
uniform vec2 uHover, uPulse;
uniform float uHoverAmt, uAspect;
varying vec2 vUv;
varying vec3 vN;
varying vec3 vV;
varying float vLens;
varying float vWave;
float wrapd(float a){ return a - floor(a + 0.5); }
void main(){
vUv = uv;
// distance on the tube's own surface (length runs uAspect times further than the girth)
vec2 d = vec2(wrapd(uv.x - uHover.x) * uAspect, wrapd(uv.y - uHover.y));
float lens = uHoverAmt * exp(-dot(d, d) / 0.012);
float along = abs(wrapd(uv.x - uPulse.x)) * uAspect;
float wave = exp(-pow((along - uPulse.y * 2.2) / 0.16, 2.0)) * exp(-uPulse.y * 0.7) * step(uPulse.y, 6.0);
vLens = lens;
vWave = wave;
vec3 p = position + normal * (lens * 0.07 + wave * 0.06);
vec4 mv = modelViewMatrix * vec4(p, 1.0);
vN = normalize(normalMatrix * normal);
vV = normalize(-mv.xyz);
gl_Position = projectionMatrix * mv;
}`,
fragmentShader: `
uniform sampler2D uAtlas;
uniform float uG, uFlow, uTime;
uniform vec2 uCells;
uniform vec3 uColor, uTube;
varying vec2 vUv;
varying vec3 vN;
varying vec3 vV;
varying float vLens;
varying float vWave;
float h21(vec2 p){ return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
void main(){
// the digit grid, sliding along the knot
vec2 g = vec2((vUv.x + uFlow) * uCells.x, vUv.y * uCells.y);
vec2 cell = floor(g);
vec2 f = fract(g);
// each row counts upward along the tube, offset per row, so the print reads as runs of digits
float k = mod(cell.x + cell.y * 3.0, uG);
// the lens scrambles; the pulse rolls every digit it passes on by one
float scr = step(0.25, vLens) * floor(h21(cell + floor(uTime * 14.0)) * uG);
k = mod(k + scr + floor(vWave * 1.999), uG);
// glyph cell, rotated so each digit stands along the tube, with an inset margin
vec2 q = (vec2(f.y, 1.0 - f.x) - 0.5) / 0.94 + 0.5;
vec2 auv = vec2((k + clamp(q.x, 0.0, 1.0)) / uG, q.y);
vec2 dg = vec2(1.0 / uG, 1.0) / 0.94;
vec2 dx = vec2(dFdx(g.y), -dFdx(g.x)) * dg, dy = vec2(dFdy(g.y), -dFdy(g.x)) * dg;
float ink = textureGrad(uAtlas, auv, dx, dy).a;
ink *= step(0.0, q.x) * step(q.x, 1.0) * step(0.0, q.y) * step(q.y, 1.0);
// soft studio light: key from the upper left, a dim fill, a thin rim
vec3 L = normalize(vec3(-0.55, 0.75, 0.55));
float diff = max(dot(vN, L), 0.0);
float light = 0.16 + 0.84 * diff + 0.14 * pow(1.0 - max(dot(vN, vV), 0.0), 3.0);
float spec = pow(max(dot(reflect(-L, vN), vV), 0.0), 24.0) * 0.18;
vec3 c = uTube * (0.4 + light) + uColor * ink * light * (1.0 + vLens * 0.8 + vWave * 1.2) + vec3(spec);
gl_FragColor = vec4(c, 1.0);
}`,
});
const knot = new THREE.Mesh(geo, mat);
const spin = new THREE.Group();
spin.add(knot);
const tilt = new THREE.Group();
tilt.add(spin);
scene.add(tilt);
const camera = new THREE.PerspectiveCamera(30, 1, 0.1, 50);
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);
camera.aspect = W / H;
const t = Math.tan(THREE.MathUtils.degToRad(15));
camera.position.set(0, 0, Math.max(4.6 / (2 * t), 4.6 / (2 * t * camera.aspect)));
camera.lookAt(0, 0, 0);
camera.updateProjectionMatrix();
};
resize();
const ro = new ResizeObserver(resize);
ro.observe(root);
// ---- motion ----
type Sp = { x: number; v: number; t: number };
const step = (s: Sp, k: number, c: number, dt: number) => {
const h = dt / 2;
for (let i = 0; i < 2; i++) {
s.v += (k * (s.t - s.x) - c * s.v) * h;
s.x += s.v * h;
}
};
const rot = { x: 0, y: 0, vx: 0, vy: 0 };
const flowV: Sp = { x: 0.012, v: 0, t: 0.012 };
const tiltX: Sp = { x: 0, v: 0, t: 0 }, tiltY: Sp = { x: 0, v: 0, t: 0 };
const hoverAmt: Sp = { x: 0, v: 0, t: 0 };
let t = 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 ray = new THREE.Raycaster();
const ndc = new THREE.Vector2();
const pick = (px: number, py: number) => {
ndc.set((px / W) * 2 - 1, -(py / H) * 2 + 1);
ray.setFromCamera(ndc, camera);
const h = ray.intersectObject(knot, false)[0];
return h && h.uv ? h.uv.clone() : null;
};
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 L = live.current;
U.uColor.value.set(L.color);
U.uTube.value.set(L.tube);
U.uTime.value = t;
// flow: a steady drift, steered by where the cursor sits across the stage
const px = pointer ? (pointer.x / W) * 2 - 1 : 0;
const py = pointer ? -((pointer.y / H) * 2 - 1) : 0;
flowV.t = still ? 0 : (0.012 + px * 0.03) * L.speed;
step(flowV, 8, 5, dt);
U.uFlow.value = (U.uFlow.value + flowV.x * dt) % 1;
// turning: a slow drift plus whatever a drag threw, coasting out
if (!drag) {
const f = Math.exp(-dt * 1.1);
rot.vx *= f;
rot.vy *= f;
rot.y += (rot.vy + (still ? 0 : 0.12)) * dt;
rot.x += rot.vx * dt;
rot.x *= Math.exp(-dt * 0.6); // drift back toward the front view
}
spin.rotation.set(rot.x, Math.sin(t * 0.31) * 0.22, rot.y); // turns in its own plane, with a gentle sway
tiltX.t = still ? 0 : -py * 0.18;
tiltY.t = still ? 0 : px * 0.24;
step(tiltX, 22, 7, dt);
step(tiltY, 22, 7, dt);
tilt.rotation.set(tiltX.x, tiltY.x, 0);
// the lens follows the cursor across the surface
const uv = pointer && !drag && !still ? pick(pointer.x, pointer.y) : null;
hoverAmt.t = uv ? 1 : 0;
if (uv) {
const cur = U.uHover.value;
if (cur.x < -1) cur.copy(uv);
// follow on the wrapped surface
const du = uv.x - cur.x - Math.round(uv.x - cur.x), dv = uv.y - cur.y - Math.round(uv.y - cur.y);
const k = 1 - Math.exp(-dt * 20);
cur.set(cur.x + du * k, cur.y + dv * k);
}
step(hoverAmt, 50, 11, dt);
U.uHoverAmt.value = Math.max(0, hoverAmt.x);
if (U.uPulse.value.y < 9) U.uPulse.value.y += dt;
if (visible && W) renderer.render(scene, camera);
};
gsap.ticker.add(tick);
// ---- input ----
const pos = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const pulse = (px?: number, py?: number) => {
if (still) return;
const uv = px !== undefined && py !== undefined ? pick(px, py) : null;
U.uPulse.value.set(uv ? uv.x : 0, 0);
};
const onMove = (e: PointerEvent) => {
pointer = pos(e);
if (!drag) return;
const now = performance.now();
const dx = e.clientX - drag.x, dy = e.clientY - drag.y;
const ddt = Math.max(8, now - drag.t) / 1000;
const k = 3.2 / Math.max(260, Math.min(W, H));
rot.y += dx * k;
rot.x += dy * k;
rot.vy = THREE.MathUtils.lerp(rot.vy, (dx * k) / ddt, 0.5);
rot.vx = THREE.MathUtils.lerp(rot.vx, (dy * k) / ddt, 0.5);
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 };
rot.vx = rot.vy = 0;
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 */
}
if (performance.now() - drag.t > 90) rot.vx = rot.vy = 0;
drag = null;
if (moved < 7 && quick) {
rot.vx = rot.vy = 0;
const p = pos(e);
pulse(p.x, p.y);
}
};
const onLeave = () => {
if (!drag) pointer = null;
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
pulse();
}
};
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);
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();
mat.dispose();
atlas.dispose();
renderer.dispose();
canvas.remove();
};
}, [chars, background]);
return (
<div
ref={rootRef}
tabIndex={0}
role="button"
aria-label="A knot covered in flowing digits. Hover to magnify, drag to turn it, press Enter to send a pulse."
className={["relative h-full min-h-[260px] w-full cursor-grab touch-none overflow-hidden outline-none select-none active:cursor-grabbing", className].filter(Boolean).join(" ")}
style={{ background }}
/>
);
}
export default DigitKnot;
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 propscharsstringThe characters printed on the knot, cycled along each row.default "0123456789"default "0123456789"colorstringCharacter colour.default "#f2f2f2"default "#f2f2f2"tubestringTube colour.default "#060607"default "#060607"speednumberFlow speed multiplier.default 1default 1backgroundstringBackground colour.default "#000000"default "#000000"classNamestringExtra classes (it fills its parent).