Glyph Koi
A koi of light swims a deep navy pond scattered with faint grid points, drawn as a mosaic of translucent tiles that each carry monospace glyphs ticking over as it lives: a pale body with red markings and a red crown, dark eyes, glowing translucent fins and a long forked tail, with a real bloom pass bleeding light off it. It swims like a real fish, a wave running down its body and the tail fin fluttering a beat behind, surging and gliding, sculling its side fins as it hovers and folding them back to dash. Come at it and it snaps into a C and whips away; hold the cursor still and it swims up and nibbles at it; tap the water and it comes to eat.
Tweak
LIVELIVEKoi
Light
Motion
Interactive
It darts from a fast cursor, circles a still one, swims to taps
Usage
Follows your tweaks.
import { GlyphKoi } from "@/components/inreact/glyph-koi";
export function Hero() {
return (
<GlyphKoi
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the water */}
</GlyphKoi>
);
}Install with the CLI
npx shadcn@latest add https://inreact.vercel.app/r/glyph-koi.jsonOr copy the source
Install the dependencies, then save this as components/inreact/glyph-koi.tsx.
npm install gsap"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef, type ReactNode } from "react";
gsap.registerPlugin(ScrollTrigger);
/**
* Glyph Koi: a koi of light, drawn as a mosaic of characters, alive in dark water.
*
* On a deep navy pond scattered with faint grid points, a koi swims, built
* from translucent tiles, each carrying monospace glyphs (X, S, 8, 0) that
* tick over as it lives: a pale body with red markings and a red crown, dark
* eyes, translucent glowing fins and a long forked tail, with a real bloom
* pass bleeding light off it.
*
* It swims like a real fish: a wave runs down its body, growing toward the
* tail, and the tail fin flutters a beat behind; it surges on a few strong
* beats and glides with its side fins spread, sculls them when it hovers,
* folds them back when it dashes, banks into turns and slows through sharp
* ones; a faint wake lights the grid behind it. It reacts: come at it and it
* snaps into a C and whips away, the escape real fish make; hold the cursor
* still and it swims up and nibbles at it; tap the water and a ripple spreads
* and it comes to eat. Scrolling makes it surge. On load it decodes out of noise.
*
* <GlyphKoi className="h-screen w-full">
* <YourHero />
* </GlyphKoi>
*
* WebGL: one scene shader draws the koi in its own body space (so it bends as
* it swims) and the glyph mosaic over it, then bloom at 1/4 and 1/16 size.
* GSAP drives every motion. Pauses off-screen; reduced motion shows a still
* frame; a CSS gradient without WebGL.
*/
export interface GlyphKoiProps {
/** Characters the tiles carry (up to 8). */
glyphs?: string;
/** Deep water colour. */
background?: string;
/** Water colour in the light around the fish. */
field?: string;
/** The koi's pale body colour. */
body?: string;
/** Colour of its markings and of the flaring glyphs. */
accent?: string;
/** Tint of its fins, the bloom and the wake. */
glowColor?: string;
/** Size of the fish. */
size?: number;
/** Tile size, relative to the fish (16 = the reference's mosaic). */
cell?: number;
/** Brightness of the fish. */
intensity?: number;
/** Strength of the bloom. */
glow?: number;
/** How fast it swims and how fast the glyphs tick. */
speed?: number;
/** It reacts to the cursor and to taps. */
interactive?: boolean;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the water. */
children?: ReactNode;
className?: string;
}
const QUAD = `attribute vec2 aQ; varying vec2 vUv; void main(){ vUv = aQ * 0.5 + 0.5; gl_Position = vec4(aQ, 0.0, 1.0); }`;
const SCENE = `
precision highp float;
uniform sampler2D uGlyphs;
uniform vec2 uRes, uMouse, uPulseC, uFishHead, uFishDir;
uniform float uFishLen, uPhase, uAmp, uBend, uFinL, uFinR, uTuck, uRoll;
uniform float uTime, uTick, uCell, uHover, uPulse, uPulseR, uPulseAmp, uScatter, uDecode, uIn, uIntensity, uNGlyph, uPush;
uniform vec3 uBg, uField, uBody, uAccent, uGlowColor;
uniform vec3 uTrail[12];
varying vec2 vUv;
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }
float vnoise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1, 0)), f.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), f.x), f.y);
}
// a fin: a rounded blade from its root, pointing at angle ang (0 = straight back), in body space
float fin(vec2 p, vec2 root, float ang, float len, float wid) {
vec2 q = p - root;
float ca = cos(ang), sa = sin(ang);
vec2 r = vec2(q.x * ca + q.y * sa, -q.x * sa + q.y * ca);
float x = r.x / len;
if (x <= 0.0 || x >= 1.0) return 0.0;
float hw = wid * pow(sin(3.14159 * pow(x, 0.65)), 0.8);
return smoothstep(hw, hw * 0.55, abs(r.y));
}
// the koi in body space: s runs nose (0) to the root of the tail (1), c across; returns colour and coverage
vec4 koi(float s, float c) {
// the body: a rounded head, widest behind it, tapering to a slim tail root; narrower when it banks
float wb = 0.14 * smoothstep(-0.04, 0.17, s) * (1.0 - 0.86 * smoothstep(0.26, 1.0, s));
wb *= 1.0 - 0.1 * abs(uRoll);
float dB = abs(c) / max(wb, 0.001);
float body = (s > -0.03 && s < 1.0) ? smoothstep(1.04, 0.78, dB) : 0.0;
// fins: translucent, glowing; side fins scull or fold back, the tail fin flutters a beat behind the body
float fins = 0.0;
float tuck = uTuck;
fins = max(fins, fin(vec2(s, c), vec2(0.2, 0.105), mix(1.05, 0.35, tuck) + uFinR * 0.35, 0.2, 0.06));
fins = max(fins, fin(vec2(s, c), vec2(0.2, -0.105), -(mix(1.05, 0.35, tuck) + uFinL * 0.35), 0.2, 0.06));
fins = max(fins, fin(vec2(s, c), vec2(0.52, 0.032), 0.75 + uFinL * 0.15, 0.1, 0.03));
fins = max(fins, fin(vec2(s, c), vec2(0.52, -0.032), -(0.75 + uFinR * 0.15), 0.1, 0.03));
float u = (s - 0.93) / 0.46;
if (u > 0.0 && u < 1.0) {
float cf = c - sin(u * 4.0 - uPhase + 1.4) * 0.05 * u;
float fw = 0.02 + 0.2 * u * (1.0 - 0.25 * u);
// a forked tail: the trailing edge reaches further at the two lobes
float edge = 1.23 + 0.16 * clamp(abs(cf) / 0.17, 0.0, 1.0);
float inF = smoothstep(fw, fw * 0.8, abs(cf)) * smoothstep(edge, edge - 0.05, s);
float rays = 0.7 + 0.3 * sin(cf / max(fw, 0.001) * 12.0);
fins = max(fins, inF * rays);
}
// a fine fin along the spine (seen from above)
fins = max(fins, smoothstep(0.016, 0.006, abs(c)) * smoothstep(0.28, 0.34, s) * smoothstep(0.72, 0.62, s) * 0.6);
fins *= 0.58;
vec3 col = vec3(0.0);
float a = 0.0;
if (fins > 0.0) {
col = mix(vec3(0.82, 0.93, 1.0), uGlowColor, 0.45) * (0.75 + 0.25 * fins);
a = fins;
}
if (body > 0.01) {
// round in cross-section: brighter down the spine
float shade = 0.6 + 0.4 * sqrt(max(0.0, 1.0 - dB * dB));
// kohaku markings, and a red crown on the head
// broad patches across the back, not stripes: low-frequency noise, favouring the spine
float pn = vnoise(vec2(s * 4.2, c * 4.0) + 3.7) * 0.72 + vnoise(vec2(s * 9.0, c * 9.0) + 9.1) * 0.28 + 0.14 * (1.0 - dB);
float mark = smoothstep(0.565, 0.62, pn) * smoothstep(0.18, 0.26, s) * (1.0 - smoothstep(0.8, 0.92, s));
mark = max(mark, smoothstep(0.052, 0.04, length(vec2(s - 0.085, c * 1.15))));
vec3 bc = mix(uBody, uAccent, mark);
// the eyes, set wide on the head
float eye = smoothstep(0.019, 0.011, length(vec2(s - 0.05, abs(c) - 0.07)));
bc = mix(bc, vec3(0.04, 0.05, 0.09), eye);
col = mix(col, bc * shade * 0.9, body);
a = max(a, body);
}
return vec4(col, a);
}
void main() {
vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
float T = uCell;
vec2 tile = floor(px / T);
vec2 tc = (tile + 0.5) * T;
vec2 lt = (px - tile * T) / T;
vec2 back = -uFishDir;
vec2 side = vec2(uFishDir.y, -uFishDir.x);
// ---- the water: deep navy, lit around the fish, with faint coloured grid points ----
vec2 mid = uFishHead + back * uFishLen * 0.5;
vec2 lm = (px - mid) / (uFishLen * 1.1);
float lit = exp(-dot(lm, lm) * 1.3);
vec3 col = mix(uBg, uField, lit * 0.85 * uIn);
float hd = hash(tile + 7.0);
vec3 dotc = hd < 0.3 ? vec3(0.25, 0.75, 0.7) : hd < 0.5 ? vec3(0.8, 0.3, 0.3) : hd < 0.7 ? vec3(0.3, 0.7, 0.35) : vec3(0.35, 0.55, 0.9);
float dd = length((lt - 0.5) * T);
float dotm = smoothstep(1.6, 0.4, dd);
col += dotc * dotm * (0.07 + 0.1 * lit) * uIn;
// the wake: the grid lights faintly along the path it just swam
float wake = 0.0;
for (int i = 0; i < 12; i++) {
vec3 tr = uTrail[i];
if (tr.z <= 0.0) continue;
vec2 wv = (tc - tr.xy) / (uFishLen * 0.2);
wake += exp(-dot(wv, wv)) * tr.z;
}
col += uGlowColor * (dotm * 0.5 + 0.025) * min(wake, 1.2) * uIn;
// ripples: a tap, or a nibble at the surface, spreads out lighting the grid points
float rr = length(tc - uPulseC) - uPulse * uPulseR;
float ringBg = exp(-pow(rr / (T * 1.6), 2.0)) * uPulseAmp;
col += uGlowColor * dotm * ringBg * 0.9 + uGlowColor * ringBg * 0.035;
// ---- which part of the koi this tile shows ----
vec2 dm = tc - uMouse;
float md = length(dm);
float near = exp(-md * md / (T * T * 38.0)) * uHover;
float ring = exp(-pow(rr / (T * 2.2), 2.0)) * uPulseAmp;
vec2 tp = tc - (dm / max(md, 1.0)) * near * uPush * T * 0.8;
vec2 f = (tp - uFishHead) / uFishLen;
float al = dot(f, back);
float ac = dot(f, side);
// the swim: a wave down the body, small at the head, large at the tail; a turn curves it
float s0 = clamp(al, 0.0, 1.4);
ac -= uAmp * (0.12 + s0 * s0) * sin(s0 * 5.6 - uPhase) + uBend * s0 * s0;
// decoding: each tile pulls from somewhere else, and they converge
vec2 jit = (hash2(tile) - 0.5) * vec2(1.4, 0.8) * uScatter;
vec4 k = koi(al + jit.x, ac + jit.y);
float d = k.a * uIntensity;
// tiles at the ragged edge come and go; inside they hold
float h1 = hash(tile + 0.37);
float on = smoothstep(0.06 + 0.16 * h1, 0.12 + 0.16 * h1, d);
float boost = 1.0 + near * 0.4 + ring * 0.3;
vec3 tcol = k.rgb * (0.86 + 0.28 * hash(tile + 3.1)) * uIntensity * boost;
vec2 e = smoothstep(vec2(0.0), vec2(0.035), lt) * smoothstep(vec2(1.0), vec2(0.965), lt);
float inset = 0.82 + 0.18 * e.x * e.y;
col = mix(col, tcol + col * 0.12, on * inset * mix(0.6, 0.95, smoothstep(0.3, 0.8, k.a)));
// ---- glyphs: two per tile, ticking over; scrambled under the cursor, while decoding and on a ripple ----
float gx = floor(lt.x * 2.0);
vec2 gid = vec2(tile.x * 2.0 + gx, tile.y);
vec2 gl = vec2(fract(lt.x * 2.0), lt.y);
float hg = hash(gid + 11.0);
float present = step(hg, mix(0.35, 0.8, smoothstep(0.4, 0.8, k.a))) * on;
float rate = 0.35 + hash(gid + 5.0) * 0.6 + near * 14.0 + ring * 20.0 + uDecode * 22.0;
float idx = floor(hash(gid + floor(uTick * rate + hg * 17.0)) * uNGlyph);
vec2 g = (gl - vec2(0.5)) / vec2(0.98, 0.84) + 0.5;
float ink = 0.0;
if (g.x > 0.0 && g.x < 1.0 && g.y > 0.0 && g.y < 1.0) ink = texture2D(uGlyphs, vec2((idx + g.x) / 8.0, g.y)).r;
float L = dot(tcol, vec3(0.3, 0.55, 0.15));
// ink a shade deeper than the tile it sits on; a few flare
vec3 ink1 = L > 0.5 ? tcol * vec3(0.48, 0.48, 0.62) : vec3(0.82, 0.9, 0.98) * (0.55 + 0.6 * L);
float ha = hash(gid + 23.0);
if (ha < 0.06) ink1 = uAccent;
else if (ha < 0.1) ink1 = uGlowColor * 0.95;
col = mix(col, ink1 * (1.0 + ring * 0.5), ink * present * 0.9);
gl_FragColor = vec4(col, 1.0);
}`;
const BLUR = `
precision mediump float;
uniform sampler2D uTex;
uniform vec2 uStep;
varying vec2 vUv;
void main() {
vec4 c = texture2D(uTex, vUv) * 0.2270;
c += (texture2D(uTex, vUv + uStep * 1.3846) + texture2D(uTex, vUv - uStep * 1.3846)) * 0.3162;
c += (texture2D(uTex, vUv + uStep * 3.2308) + texture2D(uTex, vUv - uStep * 3.2308)) * 0.0703;
gl_FragColor = c;
}`;
const BRIGHT = `
precision mediump float;
uniform sampler2D uTex;
varying vec2 vUv;
void main() {
vec3 c = texture2D(uTex, vUv).rgb;
float l = max(c.r, max(c.g, c.b));
gl_FragColor = vec4(c * smoothstep(0.7, 1.0, l), 1.0);
}`;
const COMPOSITE = `
precision mediump float;
uniform sampler2D uScene, uBloom, uWide;
uniform float uGlow, uTime;
uniform vec3 uGlowColor;
varying vec2 vUv;
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
void main() {
vec3 sc = texture2D(uScene, vUv).rgb;
vec3 near = texture2D(uBloom, vUv).rgb;
vec3 wide = texture2D(uWide, vUv).rgb;
// the bloom: tight and white close in, a wider halo tinted toward the glow colour
vec3 bl = near * 0.4 + wide * mix(vec3(1.0), uGlowColor * 1.6, 0.85) * 1.1;
// a touch of soft focus, like light seen through water
vec3 x = mix(sc, near * 1.2, 0.08) + bl * uGlow;
vec3 col = x / (1.0 + max(x - 0.9, 0.0) * 1.2);
col = min(col, vec3(1.0));
vec2 q = vUv - 0.5;
col *= 1.0 - 0.45 * dot(q, q);
col += (hash(gl_FragCoord.xy + fract(uTime * 7.0) * 91.0) - 0.5) / 255.0;
gl_FragColor = vec4(col, 1.0);
}`;
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function rgb(hex: string): [number, number, number] {
let h = hex.replace("#", "");
if (h.length === 3) h = h.replace(/./g, (c) => c + c);
const n = parseInt(h, 16) || 0;
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
}
/** The glyphs, drawn white on black in eight mono cells. */
function glyphAtlas(chars: string) {
const c = document.createElement("canvas");
c.width = 8 * 64;
c.height = 128;
const x = c.getContext("2d")!;
x.fillStyle = "#000";
x.fillRect(0, 0, c.width, c.height);
x.fillStyle = "#fff";
x.textAlign = "center";
x.textBaseline = "middle";
x.font = '500 104px ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace';
const list = Array.from(chars || "X").slice(0, 8);
list.forEach((ch, i) => x.fillText(ch, i * 64 + 32, 70, 60));
return { canvas: c, count: list.length };
}
const rand = (a: number, b: number) => a + Math.random() * (b - a);
const wrapAngle = (a: number) => Math.atan2(Math.sin(a), Math.cos(a));
/** where the koi turns about, as a share of its body length behind the nose */
const PIVOT = 0.38;
export function GlyphKoi({
glyphs = "XS80XS8$",
background = "#000d17",
field = "#0a3a62",
body = "#eef3fb",
accent = "#ff5a48",
glowColor = "#5fd2ff",
size = 1,
cell = 16,
intensity = 1,
glow = 1,
speed = 1,
interactive = true,
scroller,
children,
className,
}: GlyphKoiProps) {
const hostRef = useRef<HTMLDivElement>(null);
const props = useRef({ background, field, body, accent, glowColor, size, cell, intensity, glow, speed, interactive });
const redraw = useRef<(() => void) | null>(null);
useEffect(() => {
props.current = { background, field, body, accent, glowColor, size, cell, intensity, glow, speed, interactive };
redraw.current?.();
}, [background, field, body, accent, glowColor, size, cell, intensity, glow, speed, interactive]);
useEffect(() => {
const host = hostRef.current;
if (!host) return;
if (getComputedStyle(host).position === "static") host.style.position = "relative";
const still = isReducedMotion();
const canvas = document.createElement("canvas");
canvas.setAttribute("aria-hidden", "true");
canvas.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:pan-y;";
host.prepend(canvas);
const gl = canvas.getContext("webgl", { alpha: false, antialias: false, powerPreference: "high-performance" });
if (!gl) {
const p = props.current;
host.style.background = `radial-gradient(40% 50% at 45% 45%, ${p.field}, ${p.background})`;
canvas.remove();
return;
}
const build = (fs: string) => {
const pr = gl.createProgram()!;
for (const [type, src] of [
[gl.VERTEX_SHADER, QUAD],
[gl.FRAGMENT_SHADER, fs],
] as const) {
const sh = gl.createShader(type)!;
gl.shaderSource(sh, src);
gl.compileShader(sh);
if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(sh));
gl.attachShader(pr, sh);
}
gl.linkProgram(pr);
if (!gl.getProgramParameter(pr, gl.LINK_STATUS)) console.error(gl.getProgramInfoLog(pr));
return pr;
};
const sceneProg = build(SCENE);
const blurProg = build(BLUR);
const brightProg = build(BRIGHT);
const compProg = build(COMPOSITE);
const uni = (pr: WebGLProgram, names: string[]) =>
Object.fromEntries(names.map((n) => [n, gl.getUniformLocation(pr, n)])) as Record<string, WebGLUniformLocation | null>;
const US = uni(sceneProg, [
"uGlyphs", "uRes", "uMouse", "uPulseC", "uFishHead", "uFishDir", "uFishLen", "uPhase", "uAmp", "uBend", "uFinL", "uFinR", "uTuck", "uRoll",
"uTime", "uTick", "uCell", "uHover", "uPulse", "uPulseR", "uPulseAmp", "uScatter", "uDecode", "uIn", "uIntensity", "uNGlyph", "uPush",
"uBg", "uField", "uBody", "uAccent", "uGlowColor", "uTrail",
]);
const UB = uni(blurProg, ["uTex", "uStep"]);
const UR = uni(brightProg, ["uTex"]);
const UC = uni(compProg, ["uScene", "uBloom", "uWide", "uGlow", "uTime", "uGlowColor"]);
const quad = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, quad);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const drawQuad = (pr: WebGLProgram) => {
const a = gl.getAttribLocation(pr, "aQ");
gl.bindBuffer(gl.ARRAY_BUFFER, quad);
gl.enableVertexAttribArray(a);
gl.vertexAttribPointer(a, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.disableVertexAttribArray(a);
};
const tex2d = () => {
const t = gl.createTexture()!;
gl.bindTexture(gl.TEXTURE_2D, t);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 255]));
return t;
};
const glyphTex = tex2d();
const atlas = glyphAtlas(glyphs);
gl.bindTexture(gl.TEXTURE_2D, glyphTex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, atlas.canvas);
type Target = { fb: WebGLFramebuffer; tex: WebGLTexture; w: number; h: number };
const target = (tw: number, th: number): Target => {
const tex = tex2d();
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, tw, th, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
const fb = gl.createFramebuffer()!;
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return { fb, tex, w: tw, h: th };
};
let T: { scene: Target; a: Target; b: Target; c: Target; d: Target } | null = null;
const freeTargets = () => {
if (!T) return;
for (const x of Object.values(T)) {
gl.deleteFramebuffer(x.fb);
gl.deleteTexture(x.tex);
}
T = null;
};
let w = 1, h = 1, dpr = 1;
// the body length, nose to tail root; the tail fin adds another 40%
const fishLen = () => props.current.size * 0.46 * Math.min(w, h * 1.2);
const resize = () => {
dpr = Math.min(window.devicePixelRatio || 1, 2);
w = Math.max(1, host.clientWidth);
h = Math.max(1, host.clientHeight);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
const sw = canvas.width, sh = canvas.height;
freeTargets();
T = {
scene: target(sw, sh),
a: target(Math.max(1, sw >> 2), Math.max(1, sh >> 2)),
b: target(Math.max(1, sw >> 2), Math.max(1, sh >> 2)),
c: target(Math.max(1, sw >> 4), Math.max(1, sh >> 4)),
d: target(Math.max(1, sw >> 4), Math.max(1, sh >> 4)),
};
};
resize();
// ---- the fish: where it is (its turning point), which way it faces, how it swims ----
const fish = {
x: w * 0.5,
y: h * 0.5,
th: Math.atan2(-0.73, -0.68),
speed: 0,
turn: 0,
phase: 0,
fins: 0,
tx: w * 0.3,
ty: h * 0.3,
retarget: 0,
};
// every value GSAP moves
const s = {
in: still ? 1 : 0,
scatter: still ? 0 : 1,
decode: still ? 0 : 1,
swim: still ? 0.4 : 0,
thrust: 0,
flee: 0,
cbend: 0,
kick: 0,
curious: 0,
food: 0,
nib: 0,
mx: -1e4,
my: -1e4,
hover: 0,
push: 0,
pulse: 0,
pulseR: 1,
pulseAmp: 0,
pcx: 0,
pcy: 0,
race: 0,
};
const trail = new Float32Array(36);
const trailOut = new Float32Array(36);
let trailHead = 0, trailClock = 0;
let time = 0, tick = 0;
const headPos = () => {
const L = fishLen();
return { x: fish.x + Math.cos(fish.th) * L * (PIVOT + s.nib * 0.04), y: fish.y + Math.sin(fish.th) * L * (PIVOT + s.nib * 0.04) };
};
const blurPass = (src: Target, dst: Target, dx: number, dy: number) => {
gl.bindFramebuffer(gl.FRAMEBUFFER, dst.fb);
gl.viewport(0, 0, dst.w, dst.h);
gl.useProgram(blurProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, src.tex);
gl.uniform1i(UB.uTex, 0);
gl.uniform2f(UB.uStep, dx / src.w, dy / src.h);
drawQuad(blurProg);
};
const draw = () => {
if (!T) return;
const p = props.current;
const X = T;
const glowC = rgb(p.glowColor);
const L = fishLen();
const effort = Math.min(1.6, s.thrust + s.flee * 1.4);
const head = headPos();
gl.bindFramebuffer(gl.FRAMEBUFFER, X.scene.fb);
gl.viewport(0, 0, X.scene.w, X.scene.h);
gl.useProgram(sceneProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, glyphTex);
gl.uniform1i(US.uGlyphs, 0);
gl.uniform2f(US.uRes, X.scene.w, X.scene.h);
gl.uniform2f(US.uMouse, s.mx * dpr, s.my * dpr);
gl.uniform2f(US.uPulseC, s.pcx * dpr, s.pcy * dpr);
gl.uniform2f(US.uFishHead, head.x * dpr, head.y * dpr);
gl.uniform2f(US.uFishDir, Math.cos(fish.th), Math.sin(fish.th));
gl.uniform1f(US.uFishLen, L * dpr);
gl.uniform1f(US.uPhase, fish.phase);
// the wave grows with effort; a turn, or the C of an escape, curves the body
gl.uniform1f(US.uAmp, (0.05 + 0.06 * effort) * s.swim);
gl.uniform1f(US.uBend, -(fish.turn * 0.1 + s.cbend));
gl.uniform1f(US.uFinL, Math.sin(fish.fins));
gl.uniform1f(US.uFinR, Math.sin(fish.fins + 2.4));
gl.uniform1f(US.uTuck, Math.min(1, effort * 0.75));
gl.uniform1f(US.uRoll, Math.max(-1, Math.min(1, fish.turn * 0.35)));
gl.uniform1f(US.uTime, time);
gl.uniform1f(US.uTick, tick);
gl.uniform1f(US.uCell, Math.max(10 * dpr, (p.cell / 500) * L * dpr));
gl.uniform1f(US.uHover, s.hover);
gl.uniform1f(US.uPush, s.push);
gl.uniform1f(US.uPulse, s.pulse);
gl.uniform1f(US.uPulseR, s.pulseR * h * dpr);
gl.uniform1f(US.uPulseAmp, s.pulseAmp);
gl.uniform1f(US.uScatter, s.scatter);
gl.uniform1f(US.uDecode, Math.min(1, s.decode + s.race));
gl.uniform1f(US.uIn, s.in);
gl.uniform1f(US.uIntensity, p.intensity * s.in);
gl.uniform1f(US.uNGlyph, atlas.count);
gl.uniform3fv(US.uBg, rgb(p.background));
gl.uniform3fv(US.uField, rgb(p.field));
gl.uniform3fv(US.uBody, rgb(p.body));
gl.uniform3fv(US.uAccent, rgb(p.accent));
gl.uniform3fv(US.uGlowColor, glowC);
for (let i = 0; i < 12; i++) {
trailOut[i * 3] = trail[i * 3] * dpr;
trailOut[i * 3 + 1] = trail[i * 3 + 1] * dpr;
trailOut[i * 3 + 2] = trail[i * 3 + 2];
}
gl.uniform3fv(US.uTrail, trailOut);
drawQuad(sceneProg);
gl.bindFramebuffer(gl.FRAMEBUFFER, X.a.fb);
gl.viewport(0, 0, X.a.w, X.a.h);
gl.useProgram(brightProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, X.scene.tex);
gl.uniform1i(UR.uTex, 0);
drawQuad(brightProg);
blurPass(X.a, X.b, 1, 0);
blurPass(X.b, X.a, 0, 1);
blurPass(X.a, X.b, 1.6, 0);
blurPass(X.b, X.a, 0, 1.6);
blurPass(X.a, X.c, 1, 0);
blurPass(X.c, X.d, 0, 1);
blurPass(X.d, X.c, 1.8, 0);
blurPass(X.c, X.d, 0, 1.8);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.useProgram(compProg);
[X.scene.tex, X.a.tex, X.d.tex].forEach((tex, i) => {
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, tex);
});
gl.uniform1i(UC.uScene, 0);
gl.uniform1i(UC.uBloom, 1);
gl.uniform1i(UC.uWide, 2);
gl.uniform1f(UC.uGlow, p.glow);
gl.uniform1f(UC.uTime, time);
gl.uniform3fv(UC.uGlowColor, glowC);
drawQuad(compProg);
gl.activeTexture(gl.TEXTURE0);
};
const ro = new ResizeObserver(() => {
resize();
fish.x = Math.min(Math.max(fish.x, w * 0.1), w * 0.9);
fish.y = Math.min(Math.max(fish.y, h * 0.1), h * 0.9);
draw();
});
ro.observe(host);
if (still) {
draw();
redraw.current = draw;
return () => {
redraw.current = null;
ro.disconnect();
freeTargets();
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}
// ---- a ripple on the water: a tap spreads wide, a nibble just a little ----
const ripple = (x: number, y: number, radius: number, amp: number, dur: number) => {
s.pcx = x;
s.pcy = y;
s.pulseR = radius;
gsap.killTweensOf(s, "pulse,pulseAmp");
gsap.fromTo(s, { pulse: 0 }, { pulse: 1, duration: dur, ease: "power2.out" });
gsap.fromTo(s, { pulseAmp: amp }, { pulseAmp: 0, duration: dur, ease: "power1.in" });
};
// ---- where it wants to go: a new spot somewhere in the pond, never right where it is ----
const pickTarget = () => {
const L = fishLen();
for (let i = 0; i < 8; i++) {
const tx = rand(w * 0.2, w * 0.8), ty = rand(h * 0.2, h * 0.8);
if (Math.hypot(tx - fish.x, ty - fish.y) > L * 0.9 || i === 7) {
fish.tx = tx;
fish.ty = ty;
break;
}
}
fish.retarget = rand(4, 8);
};
// ---- the swim rhythm: a few strong tail beats, then a long glide ----
let nextStroke: gsap.core.Tween | null = null;
let strokeTl: gsap.core.Timeline | null = null;
const stroke = (power = rand(0.55, 1)) => {
strokeTl?.kill();
strokeTl = gsap.timeline().to(s, { thrust: power, duration: 0.25, ease: "power2.out" }).to(s, { thrust: 0, duration: rand(1.3, 2.3), ease: "power2.out" });
nextStroke?.kill();
nextStroke = gsap.delayedCall(rand(1.2, 3), () => stroke(s.curious > 0.5 ? rand(0.15, 0.35) : undefined));
};
// ---- on load: the water lights and the koi decodes out of noise, then swims off ----
const intro = gsap.timeline({ paused: true });
intro
.to(s, { in: 1, duration: 1.4, ease: "power2.out" }, 0)
.to(s, { scatter: 0, duration: 2.2, ease: "expo.out" }, 0.15)
.to(s, { decode: 0, duration: 2.6, ease: "power2.inOut" }, 0.15)
.to(s, { swim: 1, duration: 1.4, ease: "power2.inOut" }, 0.7)
.add(() => {
pickTarget();
stroke();
}, 1.3);
intro.play();
// ---- the escape: it snaps its body into a C away from the threat, then whips out of it ----
let lastKick = 0;
const startle = (px: number, py: number, strength = 1) => {
const L = fishLen();
const away = Math.atan2(fish.y - py, fish.x - px);
const delta = wrapAngle(away - fish.th);
const dir = Math.sign(delta) || 1;
fish.tx = Math.min(Math.max(fish.x + Math.cos(away) * L * 1.8, w * 0.2), w * 0.8);
fish.ty = Math.min(Math.max(fish.y + Math.sin(away) * L * 1.8, h * 0.2), h * 0.8);
fish.retarget = 3;
s.curious = 0;
s.food = 0;
gsap.killTweensOf(s, "flee,cbend,kick");
lastKick = 0;
s.kick = 0;
// the turn itself, most of it inside a fifth of a second
gsap.to(s, { kick: delta * 0.8, duration: 0.2, ease: "power2.out" });
gsap
.timeline()
.to(s, { cbend: dir * 0.7 * strength, duration: 0.08, ease: "power2.out" })
.to(s, { cbend: -dir * 0.35 * strength, duration: 0.14, ease: "power2.inOut" })
.to(s, { cbend: 0, duration: 0.5, ease: "power2.out" });
gsap.timeline().to(s, { flee: strength, duration: 0.1, ease: "power3.out" }).to(s, { flee: 0, duration: 1.5, ease: "power2.out" });
stroke(1);
};
// ---- the physics of it, every frame ----
let nibbleClock = 0;
const swimStep = (dt: number) => {
const L = fishLen();
const sp = props.current.speed;
const head = headPos();
fish.retarget -= dt;
// the escape turn, applied as it plays out
fish.th = wrapAngle(fish.th + (s.kick - lastKick));
lastKick = s.kick;
// what it is after: food, your finger, or just somewhere else
let tx = fish.tx, ty = fish.ty;
let approach = false;
if (s.food > 0.5) {
tx = s.pcx;
ty = s.pcy;
approach = true;
} else if (s.curious > 0.5 && s.mx > -1e3) {
tx = s.mx;
ty = s.my;
approach = true;
} else if (Math.hypot(tx - fish.x, ty - fish.y) < L * 0.4 || fish.retarget <= 0) pickTarget();
const dHead = Math.hypot(tx - head.x, ty - head.y);
// the pond's edge: near it, it brakes and turns back toward open water
const over =
Math.max(0, w * 0.12 - head.x, head.x - w * 0.88) / (w * 0.12) + Math.max(0, h * 0.12 - head.y, head.y - h * 0.88) / (h * 0.12);
const wall = Math.min(1.5, over);
if (wall > 0 && !approach) {
tx = w * 0.5;
ty = h * 0.5;
fish.tx = w * rand(0.35, 0.65);
fish.ty = h * rand(0.35, 0.65);
}
const want = Math.atan2(ty - head.y, tx - head.x);
const maxTurn = (0.85 + 1.2 * Math.min(1, s.thrust) + 2.5 * s.flee + 3 * wall + (approach ? 0.8 : 0)) * sp;
const goal = Math.max(-maxTurn, Math.min(maxTurn, wrapAngle(want - fish.th) * 2.6));
fish.turn += (goal - fish.turn) * Math.min(1, dt * 5);
fish.th = wrapAngle(fish.th + fish.turn * dt);
// speed: a slow cruise, surges on each beat, a dash to escape; it slows through sharp turns and as it arrives
let tgt = L * (0.16 + 0.6 * s.thrust + 1.5 * s.flee);
tgt *= 1 - 0.35 * Math.min(1, Math.abs(fish.turn) / 2.2);
tgt *= 1 - 0.45 * Math.min(1, wall);
if (approach) tgt = Math.min(tgt, L * 0.9 * Math.max(0, dHead / L - 0.12));
fish.speed += (tgt * sp * s.swim - fish.speed) * Math.min(1, dt * 3);
fish.x += Math.cos(fish.th) * fish.speed * dt;
fish.y += Math.sin(fish.th) * fish.speed * dt;
// the tail beats with effort; even hovering, it keeps a slow beat and sculls its fins
const effort = s.thrust + s.flee * 1.6;
fish.phase += (2.4 + 7 * effort) * sp * dt * (0.25 + 0.75 * s.swim);
fish.fins += (2.2 + 2.5 * effort + (approach && dHead < L * 0.35 ? 2.5 : 0)) * sp * dt;
// at your finger, or at food: it nibbles
nibbleClock -= dt;
if (approach && dHead < L * 0.2 && nibbleClock <= 0) {
nibbleClock = rand(0.55, 1.1);
gsap.timeline().to(s, { nib: 1, duration: 0.09, ease: "power2.out" }).to(s, { nib: 0, duration: 0.28, ease: "power2.inOut" });
ripple(tx, ty, 0.14, 0.7, 0.9);
if (s.food > 0.5) {
// eaten: a happy little surge, then off again
s.food = 0;
pickTarget();
gsap.delayedCall(0.3, () => stroke(0.8));
}
}
// the wake: drop a light point behind it now and then, each fading out
trailClock += dt;
for (let i = 0; i < 12; i++) trail[i * 3 + 2] = Math.max(0, trail[i * 3 + 2] - dt / 1.8);
if (trailClock > 0.14 && s.swim > 0.2) {
trailClock = 0;
const bx = fish.x - Math.cos(fish.th) * L * 0.5, by = fish.y - Math.sin(fish.th) * L * 0.5;
trail.set([bx, by, Math.min(1, 0.3 + fish.speed / L)], trailHead * 3);
trailHead = (trailHead + 1) % 12;
}
};
// ---- the cursor and taps ----
const mxTo = gsap.quickTo(s, "mx", { duration: 0.3, ease: "power3" });
const myTo = gsap.quickTo(s, "my", { duration: 0.3, ease: "power3" });
const hoverTo = gsap.quickTo(s, "hover", { duration: 0.5, ease: "power2" });
const pushTo = gsap.quickTo(s, "push", { duration: 0.6, ease: "power3" });
const local = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
// hold still a moment and it comes over to see
const settle = gsap.delayedCall(0.9, () => {
if (props.current.interactive && s.hover > 0.5 && s.flee < 0.2) s.curious = 1;
}).pause();
const nearBody = (x: number, y: number, r: number) => {
const L = fishLen();
for (const a of [0.3, 0, -0.3, -0.6]) {
const bx = fish.x + Math.cos(fish.th) * L * a, by = fish.y + Math.sin(fish.th) * L * a;
if (Math.hypot(x - bx, y - by) < L * r) return true;
}
return false;
};
let lp: { x: number; y: number; t: number } | null = null;
const move = (e: PointerEvent) => {
if (!props.current.interactive || intro.isActive()) return;
const p = local(e);
if (!lp) gsap.set(s, { mx: p.x, my: p.y });
mxTo(p.x);
myTo(p.y);
hoverTo(1);
pushTo(1);
const now = performance.now();
const v = lp ? Math.hypot(p.x - lp.x, p.y - lp.y) / Math.max(8, now - lp.t) : 0;
lp = { ...p, t: now };
// moving fast: no longer curious, and if it comes right at the fish, the fish bolts
if (v > 0.35) s.curious = 0;
if (v > 0.55 && nearBody(p.x, p.y, 0.35) && s.flee < 0.3) startle(p.x, p.y, 1);
settle.restart(true);
};
const leave = () => {
lp = null;
hoverTo(0);
pushTo(0);
s.curious = 0;
settle.pause();
};
const down = (e: PointerEvent) => {
if (!props.current.interactive) return;
const p = local(e);
ripple(p.x, p.y, 1.1, 1, 1.8);
if (nearBody(p.x, p.y, 0.32)) startle(p.x, p.y, 1.25);
else {
// food on the water: it turns and goes for it
s.curious = 0;
s.food = 1;
stroke(0.9);
}
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- scroll: it surges, glyphs flickering with the speed ----
const raceTo = gsap.quickTo(s, "race", { duration: 0.4, ease: "power2" });
const calm = gsap.delayedCall(0.25, () => raceTo(0)).pause();
const st = ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
if (intro.isActive()) return;
const v = Math.abs(self.getVelocity());
raceTo(Math.min(0.3, v / 6000));
if (v > 900 && s.thrust < 0.4) stroke(Math.min(1, v / 3000));
calm.restart(true);
},
});
// ---- loop, only while on screen ----
let visible = true;
let prev = performance.now();
const loop = () => {
const now = performance.now();
const dt = Math.min(0.05, (now - prev) / 1000);
prev = now;
if (!visible) return;
time += dt * props.current.speed;
tick += dt * props.current.speed;
swimStep(dt);
draw();
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
io.observe(host);
gsap.ticker.add(loop);
return () => {
gsap.ticker.remove(loop);
intro.kill();
strokeTl?.kill();
nextStroke?.kill();
settle.kill();
calm.kill();
st.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
gsap.killTweensOf(s);
freeTargets();
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [scroller, glyphs]);
return (
<div ref={hostRef} className={["isolate overflow-hidden", className].filter(Boolean).join(" ")} style={{ background }}>
{children && <div className="relative z-10 h-full">{children}</div>}
</div>
);
}
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
15 propsglyphsstringCharacters the tiles carry (up to 8).default "XS80XS8$"default "XS80XS8$"backgroundstringDeep water colour.default "#000d17"default "#000d17"fieldstringWater colour in the light around the fish.default "#0a3a62"default "#0a3a62"bodystringThe koi's pale body colour.default "#eef3fb"default "#eef3fb"accentstringColour of its markings and of the flaring glyphs.default "#ff5a48"default "#ff5a48"glowColorstringTint of its fins, the bloom and the wake.default "#5fd2ff"default "#5fd2ff"sizenumberSize of the fish.default 1default 1cellnumberTile size, relative to the fish (16 = the reference's mosaic).default 16default 16intensitynumberBrightness of the fish.default 1default 1glownumberStrength of the bloom.default 1default 1speednumberHow fast it swims and how fast the glyphs tick.default 1default 1interactivebooleanIt reacts to the cursor and to taps.default truedefault truescrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the water.classNamestringSize it, e.g. h-screen w-full.