Frosted Glass
A kitten presses its paws against a pane of fogged, condensation-beaded glass, and it is alive: it scratches at the glass in bouts, its claws cutting thin clear lines through the fog, breathes, looks around, turns toward your cursor and reaches for it. Run your cursor over it to wipe the fog away: the kitten shows through clearer, beads of water gather along the edges of the wipe, and the fog creeps back in a mottled film. A click breathes a bloom of fresh fog onto the glass and startles it into scrabbling, drops now and then run down the pane leaving clear trails, and scrolling pushes in with depth parallax.
Tweak
LIVELIVETone
Glass
Water beads
Beads along the edges of wipes and trails
Motion
Kitten moves
Scratches at the glass, breathes, looks around and reacts to you
Interactive
The cursor wipes the fog, clicks breathe on the glass
Usage
Follows your tweaks.
import { FrostedGlass } from "@/components/inreact/frosted-glass";
export function Hero() {
return (
<FrostedGlass
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the glass */}
</FrostedGlass>
);
}Install with the CLI
npx shadcn@latest add https://inreact.vercel.app/r/frosted-glass.jsonOr copy the source
Install the dependencies, then save this as components/inreact/frosted-glass.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);
/**
* Frosted Glass: a photo seen through a fogged, beaded window.
*
* A kitten presses its paws against a pane of condensation-covered glass. Run
* the cursor over it and you wipe the fog away: the kitten shows through
* clearer, beads of water gather along the edges of the wipe, and the fog
* slowly creeps back in a mottled film. Click to breathe on the glass, and a
* cloud of fresh fog blooms out from that spot. Now and then a drop runs down
* the pane, stalling and racing, leaving a clear trail behind it. Scrolling
* pushes in and shifts the scene in depth (the paws are nearest). On load the
* fog thins and the kitten emerges behind it.
*
* <FrostedGlass className="h-screen w-full">
* <YourHero />
* </FrostedGlass>
*
* One WebGL shader over three images (the fogged photo, a wiped-clear version
* and a depth map); the wipe and breath live in a small field updated in JS.
* GSAP drives every motion. Pauses off-screen; reduced motion shows a still
* frame; the plain photo without WebGL.
*/
export interface FrostedGlassProps {
/** The fogged photo. */
image?: string;
/** The same photo with the fog wiped away (shown where the glass is wiped). */
clearImage?: string;
/** Depth map of the photo: R = depth, white = near (parallax); G = the body (breathing). */
depthImage?: string;
/** Rig mask for the kitten's head (B channel). */
rigImage?: string;
/** The paws as layers: R/G = fog/clear photo minus the glass behind the paws. */
pawsImage?: string;
/** Colour of the darkest shadows. */
shadow?: string;
/** Colour of the brightest light. */
light?: string;
/** Fog density: 0 is nearly clear glass, 1 the photo as shot, more is thicker. */
fog?: number;
/** Size of the cursor's wipe. */
brush?: number;
/** Seconds a wiped patch takes to fog over again. */
fogBack?: number;
/** How often drops run down the glass (0 = never). */
droplets?: number;
/** Water beads along the edges of wipes and trails. */
beads?: boolean;
/** The kitten moves: scratches at the glass, breathes, looks around and reacts to you. Turn off for your own photo. */
moving?: boolean;
/** Cursor wipes, clicks breathe fog. */
interactive?: boolean;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the glass. */
children?: ReactNode;
className?: string;
}
const VERT = `attribute vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;
const FRAG = `
precision highp float;
uniform sampler2D uFog, uClear, uDepth, uWipe, uRig, uPaws;
uniform vec2 uRes, uPar, uGrid;
uniform float uAspect, uTime, uZoom, uVeil, uFogAmt, uBeads;
uniform vec3 uShadow, uLight;
uniform vec3 uDrops[8];
// the kitten's rig: each paw is a rigid layer (offset xy, lift z, wrist turn w);
// the head (offset xy, tilt z, lean w), the ears, breathing
uniform vec4 uPawL, uPawR;
uniform vec4 uHead;
uniform vec2 uEars;
uniform float uBreath;
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);
}
// p: centred, in units of the height, y up -> s: 0..1 across the element, y down
vec2 toS(vec2 p) { return vec2(p.x * uRes.y / uRes.x + 0.5, 0.5 - p.y); }
// s -> the photo: cover-fit, framed on the kitten, pushed in
vec2 baseUV(vec2 s) {
float ha = uRes.x / uRes.y;
vec2 uv;
if (ha > uAspect) {
float k = uAspect / ha;
uv = vec2(s.x, clamp(0.45, k * 0.5, 1.0 - k * 0.5) + (s.y - 0.5) * k);
} else {
uv = vec2(0.5 + (s.x - 0.5) * ha / uAspect, s.y);
}
vec2 f = vec2(0.5, 0.46);
return f + (uv - f) / uZoom;
}
// the kitten moves: each part of the photo is pulled by its own soft mask, so a paw drags its arm
// and the fog around it stretches gently instead of leaving a ghost behind
vec2 rigWarp(vec2 uv, out float press) {
vec3 m = texture2D(uRig, uv).rgb;
// the head tilts about the neck and leans in toward the glass
vec2 neck = vec2(0.55, 0.6);
float a = -uHead.z * m.b;
vec2 hp = uv - neck;
uv = neck + vec2(hp.x * cos(a) - hp.y * sin(a), hp.x * sin(a) + hp.y * cos(a));
uv -= m.b * uHead.xy;
vec2 hc = vec2(0.55, 0.42);
uv = hc + (uv - hc) / (1.0 + uHead.w * m.b * 0.035);
// the ears flick: each bends about its root, the tip moving most
float e = texture2D(uDepth, uv).b;
if (e > 0.003) {
bool left = uv.x < 0.56;
vec2 root = left ? vec2(0.41, 0.385) : vec2(0.715, 0.395);
float ang = (left ? uEars.x : uEars.y) * e;
vec2 q = uv - root;
q.x *= uAspect;
q = vec2(q.x * cos(ang) - q.y * sin(ang), q.x * sin(ang) + q.y * cos(ang));
q.x /= uAspect;
uv = root + q;
}
// breathing swells the body from the bottom of the frame
float bm = texture2D(uDepth, uv).g;
uv.y = 1.0 - (1.0 - uv.y) / (1.0 + uBreath * bm);
press = m.b * uHead.w;
return uv;
}
vec2 toUV(vec2 s) {
float press;
vec2 uv = rigWarp(baseUV(s), press);
float d = texture2D(uDepth, uv).r;
uv += uPar * (d - 0.4) * 0.03;
return clamp(uv, 0.002, 0.998);
}
// a paw as a solid layer, pulled from the photo as a matte: how opaque it is (from how much darker
// it is than the glass behind it) and its colour premultiplied by that. Returns (fog, clear, alpha).
vec3 pawMatte(vec2 q) {
vec2 d = texture2D(uPaws, q).rg * 2.0 - 1.0;
float f = texture2D(uFog, q).r, c = texture2D(uClear, q).r;
float pf = f - d.x, pc = c - d.y;
float a = clamp(-d.x / max(pf - 0.02, 0.05), 0.0, 1.0);
return vec3(f - (1.0 - a) * pf, c - (1.0 - a) * pc, a);
}
// the paw where it is now: it slides and turns as one piece; lifted off the glass it goes soft and pale
vec3 pawLayer(vec2 uv, vec4 P, vec2 piv, float side) {
vec2 q = uv - P.xy - piv;
q.x *= uAspect;
float a = -P.w;
q = vec2(q.x * cos(a) - q.y * sin(a), q.x * sin(a) + q.y * cos(a));
q.x /= uAspect;
q += piv;
if ((q.x - 0.5) * side < 0.1) return vec3(0.0);
q = clamp(q, 0.001, 0.999);
vec3 m = pawMatte(q);
if (P.z > 0.01) {
vec3 acc = m;
for (int i = 0; i < 8; i++) {
float b = float(i) * 2.39996;
float rr = (0.004 + 0.012 * fract(float(i) * 0.618)) * P.z;
acc += pawMatte(clamp(q + vec2(cos(b), sin(b)) * rr * vec2(1.0, uAspect), 0.001, 0.999));
}
m = mix(m, acc / 9.0, min(1.0, P.z * 1.5));
m *= 1.0 - 0.36 * P.z;
}
return m;
}
// how sharply the wipe changes around s: high only along the edge of a wipe or trail
float wipeEdge(vec2 s) {
vec2 o = uGrid * 1.3;
float a = texture2D(uWipe, s + vec2(o.x, 0.0)).r, b = texture2D(uWipe, s - vec2(o.x, 0.0)).r;
float c = texture2D(uWipe, s + vec2(0.0, o.y)).r, d = texture2D(uWipe, s - vec2(0.0, o.y)).r;
return clamp((max(max(a, b), max(c, d)) - min(min(a, b), min(c, d))) * 2.2, 0.0, 1.0);
}
// a water bead: a tiny lens that shows the room behind it flipped, a dark rim and a highlight
// returns (coverage, grey)
vec2 shadeDrop(vec2 d, float r, vec2 c) {
float l = length(d);
float aa = 1.6 / (uRes.y * r);
float a = 1.0 - smoothstep(1.0 - aa, 1.0, l);
if (a <= 0.0) return vec2(0.0);
float hgt = sqrt(max(0.0, 1.0 - l * l));
float inside = texture2D(uClear, toUV(toS(c - d * r * 2.6))).r;
float g = inside * 1.04 + 0.05;
// the rim: darker, heaviest along the bottom where the water is thickest
g *= 1.0 - 0.5 * smoothstep(0.5, 1.0, l) * (0.65 - 0.35 * d.y);
// a soft caustic at the bottom, a crisp highlight up top
g += 0.12 * smoothstep(0.35, 0.9, -d.y) * smoothstep(1.0, 0.75, l);
g += 0.42 * pow(max(0.0, dot(normalize(vec3(d, hgt)), normalize(vec3(-0.35, 0.55, 0.75)))), 24.0);
return vec2(a, g);
}
vec2 beadLayer(vec2 p, float cell, float salt) {
vec2 id = floor(p / cell);
vec2 h = hash2(id + salt);
vec2 c = (id + 0.25 + 0.5 * h) * cell;
float r = cell * (0.11 + 0.14 * hash(id + salt + 7.1));
vec2 sc = toS(c);
float w = clamp(texture2D(uWipe, sc).r, 0.0, 1.0);
// beads gather along the edges of a wipe; a few stay scattered on the wiped glass
float edge = wipeEdge(sc);
float prob = (edge * 0.95 + w * 0.08) * uBeads;
if (hash(id + salt + 3.3) > prob) return vec2(0.0);
vec2 d = (p - c) / r;
d.y *= 1.06;
return shadeDrop(d, r, c);
}
void main() {
vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;
vec2 s = toS(p);
float press;
vec2 uv = rigWarp(baseUV(s), press);
uv = clamp(uv + uPar * (texture2D(uDepth, uv).r - 0.4) * 0.03, 0.002, 0.998);
// the glass with the paws taken out, then each paw laid back over it where it is now
vec2 home = texture2D(uPaws, uv).rg * 2.0 - 1.0;
vec2 plate = vec2(texture2D(uFog, uv).r, texture2D(uClear, uv).r) - home;
vec3 pl = pawLayer(uv, uPawL, vec2(0.11, 0.5), -1.0);
plate = plate * (1.0 - pl.z) + pl.xy;
vec3 pr = pawLayer(uv, uPawR, vec2(0.82, 0.73), 1.0);
plate = clamp(plate * (1.0 - pr.z) + pr.xy, 0.0, 1.0);
float fog = plate.x;
// clear glass scatters no light, so the wiped view sits a touch darker than the fog
float clear = mix(plate.y, fog, 0.12) * 0.9;
// fog density: below 1 thin it toward the clear view, above 1 thicken it with a milky film
float fogged = mix(clear, fog, clamp(uFogAmt, 0.0, 1.0));
fogged = mix(fogged, 0.86, clamp((uFogAmt - 1.0) * 0.55, 0.0, 0.6));
// the wipe: the fog comes back as a mottled film, thinnest patches first
vec4 wf = texture2D(uWipe, s);
float n = vnoise(p * 7.0) * 0.6 + vnoise(p * 21.0) * 0.4;
float eff = smoothstep(n * 0.5, n * 0.5 + 0.35, wf.r);
// the face pressed to the glass comes through a little clearer
fogged = mix(fogged, clear, press * 0.35);
float g = mix(fogged, clear, eff);
// condensation pushed aside piles up as a thicker, brighter film along the wipe's edge
float rim = wipeEdge(s) * (1.0 - eff);
g = mix(g, min(1.0, fogged * 1.05 + 0.035), rim * 0.4);
// breath: fresh fog blooms out, blurring and brightening what is behind, with a ragged edge
float nb = vnoise(p * 6.0 + 3.1) * 0.65 + vnoise(p * 17.0 - 1.7) * 0.35;
float b = smoothstep(nb * 0.55, nb * 0.55 + 0.4, wf.g) * min(1.0, wf.g * 1.6);
if (b > 0.004) {
vec3 pp = vec3(0.0);
float acc = 0.0;
for (int i = 0; i < 12; i++) {
float a = float(i) * 2.39996;
float rr = 0.012 + 0.03 * fract(float(i) * 0.618);
acc += texture2D(uFog, toUV(toS(p + vec2(cos(a), sin(a)) * rr))).r;
}
float blur = acc / 12.0;
g = mix(g, mix(blur, 0.94, 0.55), b * 0.88);
}
// beads along the wipe edges and the trails
vec2 bd = beadLayer(p, 0.024, 0.0);
g = mix(g, bd.y, bd.x);
bd = beadLayer(p, 0.014, 17.0);
g = mix(g, bd.y, bd.x * 0.9);
// the running drops: a fat head with a short tail above
for (int i = 0; i < 8; i++) {
vec3 dr = uDrops[i];
if (dr.z <= 0.0) continue;
vec2 d = (p - dr.xy) / dr.z;
if (d.y > 0.0) d.y *= 0.62;
d.x *= 1.0 + 0.12 * max(d.y, 0.0);
vec2 hd = shadeDrop(d, dr.z, dr.xy);
g = mix(g, hd.y, hd.x);
}
// on load: the glass starts milky and the kitten emerges as it thins
float veil = smoothstep(0.0, 0.35, uVeil * 1.35 - n * 0.35);
g = mix(g, 0.93, veil);
vec3 col = mix(uShadow, uLight, clamp(g, 0.0, 1.0));
vec2 q = gl_FragCoord.xy / uRes - 0.5;
col *= 1.0 - 0.1 * dot(q * vec2(1.1, 0.9), q * vec2(1.1, 0.9));
col += (hash(gl_FragCoord.xy + fract(uTime * 7.0) * 91.0) - 0.5) / 160.0;
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 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];
}
const rand = (a: number, b: number) => a + Math.random() * (b - a);
interface Paw {
x: number;
y: number;
lift: number;
w: number;
rx: number;
ry: number;
rl: number;
scratch: boolean;
tl: gsap.core.Timeline | null;
claws: [number, number][];
prev: ({ x: number; y: number } | null)[];
home: [number, number];
/** the wrist, which the paw turns about */
piv: [number, number];
/** down the forearm, in photo units: the line a stroke follows */
axis: [number, number];
}
const mkPaw = (claws: [number, number][], home: [number, number], piv: [number, number], axis: [number, number]): Paw => ({
x: 0, y: 0, lift: 0, w: 0, rx: 0, ry: 0, rl: 0, scratch: false, tl: null, claws, prev: claws.map(() => null), home, piv, axis,
});
interface Drop {
x: number;
y: number;
r: number;
v: number;
tv: number;
t: number;
}
export function FrostedGlass({
image = "/backgrounds/frosted-cat/cat.jpg",
clearImage = "/backgrounds/frosted-cat/clear.jpg",
depthImage = "/backgrounds/frosted-cat/depth.png",
rigImage = "/backgrounds/frosted-cat/rig.png",
pawsImage = "/backgrounds/frosted-cat/diff.png",
shadow = "#030305",
light = "#f6f6fc",
fog = 1,
brush = 1,
fogBack = 5,
droplets = 1,
beads = true,
moving = true,
interactive = true,
scroller,
children,
className,
}: FrostedGlassProps) {
const hostRef = useRef<HTMLDivElement>(null);
const props = useRef({ shadow, light, fog, brush, fogBack, droplets, beads, moving, interactive });
const redraw = useRef<(() => void) | null>(null);
useEffect(() => {
props.current = { shadow, light, fog, brush, fogBack, droplets, beads, moving, interactive };
redraw.current?.();
}, [shadow, light, fog, brush, fogBack, droplets, beads, moving, 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;visibility:hidden;";
host.prepend(canvas);
const gl = canvas.getContext("webgl", { alpha: false, antialias: false });
if (!gl) {
host.style.background = `url("${image}") center 45% / cover`;
canvas.remove();
return;
}
const prog = gl.createProgram()!;
for (const [type, src] of [
[gl.VERTEX_SHADER, VERT],
[gl.FRAGMENT_SHADER, FRAG],
] 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(prog, sh);
}
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) console.error(gl.getProgramInfoLog(prog));
gl.useProgram(prog);
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const aPos = gl.getAttribLocation(prog, "aPos");
gl.enableVertexAttribArray(aPos);
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
const U = Object.fromEntries(
["uFog", "uClear", "uDepth", "uWipe", "uRes", "uPar", "uGrid", "uAspect", "uTime", "uZoom", "uVeil", "uFogAmt", "uBeads", "uShadow", "uLight", "uDrops", "uRig", "uPaws", "uPawL", "uPawR", "uHead", "uEars", "uBreath"].map((n) => [
n,
gl.getUniformLocation(prog, n),
]),
) as Record<string, WebGLUniformLocation | null>;
const makeTex = (unit: number) => {
const t = gl.createTexture()!;
gl.activeTexture(gl.TEXTURE0 + unit);
gl.bindTexture(gl.TEXTURE_2D, t);
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.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([200, 200, 200, 255]));
return t;
};
const texs = [makeTex(0), makeTex(1), makeTex(2), makeTex(3), makeTex(4), makeTex(5)];
gl.uniform1i(U.uFog, 0);
gl.uniform1i(U.uClear, 1);
gl.uniform1i(U.uDepth, 2);
gl.uniform1i(U.uWipe, 3);
gl.uniform1i(U.uRig, 4);
gl.uniform1i(U.uPaws, 5);
// ---- the wipe field: R = how wiped (held above 1 so it stays clear a moment), G = breath ----
let GW = 1, GH = 200;
let wipe = new Float32Array(1), breath = new Float32Array(1), bytes = new Uint8Array(4);
let w = 1, h = 1;
const resize = () => {
const 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);
gl.viewport(0, 0, canvas.width, canvas.height);
const nw = Math.max(30, Math.min(480, Math.round((GH * w) / h)));
if (nw !== GW) {
GW = nw;
wipe = new Float32Array(GW * GH);
breath = new Float32Array(GW * GH);
bytes = new Uint8Array(GW * GH * 4);
}
};
resize();
// stamp a soft round brush at s (0..1, y down), radius r in units of the height
const stamp = (sx: number, sy: number, r: number, hold: number) => {
const cx = sx * GW, cy = sy * GH, rr = Math.max(0.8, r * GH);
const x0 = Math.max(0, Math.floor(cx - rr)), x1 = Math.min(GW - 1, Math.ceil(cx + rr));
const y0 = Math.max(0, Math.floor(cy - rr)), y1 = Math.min(GH - 1, Math.ceil(cy + rr));
for (let y = y0; y <= y1; y++)
for (let x = x0; x <= x1; x++) {
const d = Math.hypot(x + 0.5 - cx, y + 0.5 - cy) / rr;
if (d >= 1) continue;
const t = Math.min(1, Math.max(0, (d - 0.5) / 0.5));
const f = 1 - t * t * (3 - 2 * t);
const i = y * GW + x;
if (hold * f > wipe[i]) wipe[i] = hold * f;
breath[i] *= 1 - f * 0.6;
}
};
const breathe = (sx: number, sy: number, r: number, a: number) => {
const cx = sx * GW, cy = sy * GH, rr = Math.max(0.8, r * GH);
const x0 = Math.max(0, Math.floor(cx - rr)), x1 = Math.min(GW - 1, Math.ceil(cx + rr));
const y0 = Math.max(0, Math.floor(cy - rr)), y1 = Math.min(GH - 1, Math.ceil(cy + rr));
for (let y = y0; y <= y1; y++)
for (let x = x0; x <= x1; x++) {
const d = Math.hypot(x + 0.5 - cx, y + 0.5 - cy) / rr;
if (d >= 1) continue;
const f = Math.pow(1 - d * d, 2.4) * a;
const i = y * GW + x;
if (f > breath[i]) breath[i] = f;
wipe[i] *= 1 - f * 0.25;
}
};
const upload = () => {
for (let i = 0, j = 0; i < wipe.length; i++, j += 4) {
bytes[j] = Math.min(255, wipe[i] * 255);
bytes[j + 1] = Math.min(255, breath[i] * 255);
}
gl.activeTexture(gl.TEXTURE3);
gl.bindTexture(gl.TEXTURE_2D, texs[3]);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GW, GH, 0, gl.RGBA, gl.UNSIGNED_BYTE, bytes);
};
// ---- state: every value moved by GSAP ----
const ZOOM = 1.05;
const s = { veil: still ? 0 : 1, zoom: still ? ZOOM : ZOOM + 0.1, push: 0, px: 0, py: 0, sy: 0 };
let aspect = 1452 / 1797;
let time = 0;
const drops: Drop[] = [];
const dropBuf = new Float32Array(24);
// the kitten's rig, in photo units: each paw's offset and how far it is lifted off the glass
// (scratch bouts) plus a reach toward your finger; the head's idle look, tilt and lean
const L = mkPaw([[0.006, 0.433], [0.045, 0.381], [0.091, 0.368], [0.18, 0.4]], [0.09, 0.43], [0.11, 0.5], [0.34, 0.94]);
const R = mkPaw([[0.747, 0.595], [0.827, 0.582], [0.867, 0.598], [0.915, 0.653]], [0.81, 0.66], [0.82, 0.73], [-0.1, 0.99]);
const k = { hx: 0, hy: 0, ht: 0, bt: 0, lean: 0, lx: 0, ly: 0, lt: 0, br: 0, el: 0, er: 0, elk: 0, erk: 0 };
const pawU = (P: Paw) => [P.x + P.rx, P.y + P.ry, Math.min(1, P.lift + P.rl), P.w];
// where a point of the paw is now: turned about the wrist, then carried with the paw
const pawPoint = (P: Paw, u: number, v: number) => {
const qx = (u - P.piv[0]) * aspect, qy = v - P.piv[1];
const c = Math.cos(P.w), sn = Math.sin(P.w);
return [P.piv[0] + (qx * c - qy * sn) / aspect + P.x + P.rx, P.piv[1] + qx * sn + qy * c + P.y + P.ry];
};
// photo uv -> s (0..1 across the element, y down): the inverse of the shader's framing
const uvToS = (u: number, v: number) => {
const z = s.zoom + s.push;
const x = 0.5 + (u - 0.5) * z, y = 0.46 + (v - 0.46) * z;
const ha = w / h;
if (ha > aspect) {
const kk = aspect / ha;
const c = Math.min(Math.max(0.45, kk / 2), 1 - kk / 2);
return { x, y: (y - c) / kk + 0.5 };
}
return { x: (x - 0.5) * (aspect / ha) + 0.5, y };
};
const draw = () => {
const p = props.current;
gl.uniform2f(U.uRes, canvas.width, canvas.height);
gl.uniform2f(U.uGrid, 1 / GW, 1 / GH);
gl.uniform2f(U.uPar, Math.max(-1, Math.min(1, s.px)), Math.max(-1, Math.min(1, s.py + s.sy)));
gl.uniform1f(U.uAspect, aspect);
gl.uniform1f(U.uTime, time);
gl.uniform1f(U.uZoom, s.zoom + s.push);
gl.uniform1f(U.uVeil, s.veil);
gl.uniform1f(U.uFogAmt, p.fog);
gl.uniform1f(U.uBeads, p.beads ? 1 : 0);
gl.uniform3fv(U.uShadow, rgb(p.shadow));
gl.uniform3fv(U.uLight, rgb(p.light));
dropBuf.fill(0);
drops.slice(0, 8).forEach((d, i) => dropBuf.set([d.x, d.y, d.r], i * 3));
gl.uniform3fv(U.uDrops, dropBuf);
gl.uniform4fv(U.uPawL, pawU(L));
gl.uniform4fv(U.uPawR, pawU(R));
gl.uniform4f(U.uHead, k.hx + k.lx, k.hy + k.ly, k.ht + k.bt + k.lt, k.lean);
gl.uniform1f(U.uBreath, k.br);
gl.uniform2f(U.uEars, k.el + k.elk, k.er + k.erk);
upload();
gl.drawArrays(gl.TRIANGLES, 0, 3);
};
// ---- the photos ----
let loaded = 0;
let started = false;
let alive = true;
const onReady: (() => void)[] = [];
const srcs = [image, clearImage, depthImage, rigImage, pawsImage];
// texture units: 0 fog, 1 clear, 2 depth, 3 the wipe field (written every frame), 4 the rig, 5 the paws
const units = [0, 1, 2, 4, 5];
srcs.forEach((src, i) => {
const unit = units[i];
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () => {
if (!alive) return;
gl.activeTexture(gl.TEXTURE0 + unit);
gl.bindTexture(gl.TEXTURE_2D, texs[unit]);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
if (unit === 0) aspect = img.naturalWidth / Math.max(1, img.naturalHeight);
if (++loaded === srcs.length) {
canvas.style.visibility = "visible";
draw();
onReady.forEach((f) => f());
}
};
img.src = src;
});
const ro = new ResizeObserver(() => {
resize();
if (still && loaded === srcs.length) draw();
});
ro.observe(host);
if (still) {
redraw.current = () => loaded === srcs.length && draw();
return () => {
alive = false;
redraw.current = null;
ro.disconnect();
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}
// ---- on load: the milky glass thins and the kitten emerges; the view settles in ----
const intro = gsap.timeline({ paused: true });
intro.to(s, { veil: 0, duration: 2.8, ease: "power2.inOut" }, 0).to(s, { zoom: ZOOM, duration: 3.4, ease: "expo.out" }, 0);
// ---- idle: now and then a drop runs down the glass, stalling and racing ----
const spawnDrop = (y?: number) => {
if (drops.length >= 8) return;
const ha = w / h;
drops.push({ x: rand(-ha / 2 + 0.04, ha / 2 - 0.04), y: y ?? rand(0.05, 0.45), r: rand(0.0085, 0.014), v: 0, tv: 0, t: rand(0.2, 0.8) });
};
let nextDrop: gsap.core.Tween | null = null;
let firstDrops: gsap.core.Tween | null = null;
const schedule = () => {
const rate = props.current.droplets;
nextDrop = gsap.delayedCall(rate > 0 ? rand(2.2, 5) / rate : 1, () => {
if (props.current.droplets > 0) spawnDrop();
schedule();
});
};
onReady.push(() => {
started = true;
intro.play();
firstDrops = gsap.delayedCall(1.8, () => {
if (props.current.droplets > 0) {
spawnDrop(0.32);
spawnDrop(0.12);
}
});
schedule();
});
const stepDrops = (dt: number) => {
for (let k = drops.length - 1; k >= 0; k--) {
const d = drops[k];
d.t -= dt;
if (d.t <= 0) {
// water runs in fits: a stall while it gathers, then a run
if (d.tv === 0) {
d.tv = rand(0.12, 0.42);
d.t = rand(0.3, 1.3);
} else {
d.tv = Math.random() < 0.45 ? 0 : rand(0.08, 0.3);
d.t = rand(0.25, 1);
}
}
d.v += (d.tv - d.v) * Math.min(1, dt * 7);
d.y -= d.v * dt;
d.x += (Math.random() - 0.5) * 0.06 * d.v * dt;
d.r = Math.min(0.017, d.r + d.v * dt * 0.004);
// it clears a trail of fog behind it
stamp(d.x * (h / w) + 0.5, 0.5 - (d.y + d.r * 0.2), d.r * 0.95, 1.5);
if (d.y < -0.62) drops.splice(k, 1);
}
};
// ---- the kitten: scratch bouts. Reach up off the glass, plant the paw, drag it down with the
// claws out (they cut thin clear streaks through the fog), then settle back pressed flat ----
const scratchBout = (P: Paw, strokes: number, fast = false) => {
if (P.tl || !props.current.moving) return;
const sp = fast ? 0.62 : 1;
const side = P === L ? 1 : -1;
const tl = gsap.timeline({ onComplete: () => void (P.tl = null) });
// the head leans toward the busy paw while it works
tl.to(k, { bt: 0.035 * side, duration: 0.5, ease: "sine.inOut" }, 0);
const [ax, ay] = P.axis;
for (let i = 0; i < strokes; i++) {
// a stroke runs down the line of the forearm, the way the arm actually pulls
const len = rand(0.04, 0.068) * (fast ? 1.15 : 1);
const up = rand(0.012, 0.022);
const turn = rand(0.03, 0.07) * side;
// reach: the paw comes up off the glass along the arm, wrist straightening
tl.to(P, { lift: rand(0.45, 0.65), x: -ax * up, y: -ay * up, w: -turn * 0.4, duration: rand(0.2, 0.26) * sp, ease: "power2.out" }, i === 0 ? 0 : ">")
// plant: it meets the glass, sharp and dark again
.to(P, { lift: 0, duration: 0.08 * sp, ease: "power2.in" })
// pull: the claws catch, then the paw drags down, the wrist curling as it goes
.to(P, {
x: ax * len,
y: ay * len,
w: turn,
duration: rand(0.32, 0.46) * sp,
ease: "power2.inOut",
onStart: () => {
P.scratch = true;
P.prev.fill(null);
},
onComplete: () => void (P.scratch = false),
});
}
// done: it lifts a little, comes back up and settles flat where it started
tl.to(P, { lift: 0.35, x: -ax * 0.006, y: -ay * 0.006, w: 0, duration: 0.3 * sp, ease: "power2.out" })
.to(P, { lift: 0, x: 0, y: 0, duration: 0.45, ease: "power3.out" })
.to(k, { bt: 0, duration: 0.8, ease: "sine.inOut" }, "<");
P.tl = tl;
};
const kill = (P: Paw) => {
P.tl?.kill();
P.tl = null;
P.scratch = false;
};
// between bouts: a pause, then one paw, the other, or both in turn
let nextBout: gsap.core.Tween | null = null;
const boutLoop = () => {
nextBout = gsap.delayedCall(rand(1.4, 4.2), () => {
const r = Math.random(), n = 1 + Math.floor(Math.random() * 3);
if (r < 0.4) scratchBout(L, n);
else if (r < 0.8) scratchBout(R, n);
else {
scratchBout(L, 2);
gsap.delayedCall(0.3, () => scratchBout(R, 2));
}
// now and then it presses its face to the glass
if (props.current.moving && Math.random() < 0.25) gsap.to(k, { lean: 1, duration: 0.9, ease: "sine.inOut", yoyo: true, repeat: 1, repeatDelay: rand(0.4, 1.4) });
boutLoop();
});
};
// the head looks around between bouts
let look: gsap.core.Tween | null = null;
const lookAround = () => {
const on = props.current.moving;
look = gsap.to(k, {
hx: on ? rand(-0.008, 0.008) : 0,
hy: on ? rand(-0.005, 0.004) : 0,
ht: on ? rand(-0.05, 0.05) : 0,
duration: rand(0.8, 1.6),
ease: "sine.inOut",
delay: rand(0.5, 2.6),
onComplete: lookAround,
});
};
// the ears flick now and then: a quick snap and a smooth return, sometimes twice
const flick = (key: "el" | "er", amt = 1) => {
const dir = key === "el" ? 1 : -1;
const twice = Math.random() < 0.35;
const tl = gsap.timeline();
tl.to(k, { [key]: 0.3 * dir * amt, duration: 0.08, ease: "power2.out" }).to(k, { [key]: 0, duration: 0.32, ease: "power2.inOut" }, "+=0.05");
if (twice) tl.to(k, { [key]: 0.22 * dir * amt, duration: 0.07, ease: "power2.out" }, "+=0.08").to(k, { [key]: 0, duration: 0.3, ease: "power2.inOut" }, "+=0.04");
return tl;
};
let nextFlick: gsap.core.Tween | null = null;
const flickLoop = () => {
nextFlick = gsap.delayedCall(rand(2.2, 6.5), () => {
if (props.current.moving) {
const r = Math.random();
if (r < 0.42) flick("el");
else if (r < 0.84) flick("er");
else {
flick("el");
flick("er");
}
}
flickLoop();
});
};
// and it breathes, quick and shallow like a kitten
const breathing = gsap.to(k, { br: 0.007, duration: 0.95, ease: "sine.inOut", yoyo: true, repeat: -1, paused: true });
let wake: gsap.core.Tween | null = null;
onReady.push(() => {
breathing.play();
wake = gsap.delayedCall(1.6, () => {
boutLoop();
lookAround();
flickLoop();
scratchBout(R, 2);
});
});
// claws on the glass: while a paw drags, each claw tip cuts a thin clear line through the fog
const stepClaws = () => {
for (const P of [L, R]) {
if (!P.scratch || P.lift + P.rl > 0.35) {
P.prev.fill(null);
continue;
}
P.claws.forEach(([u, v], i) => {
const [pu, pv] = pawPoint(P, u, v + 0.004);
const q = uvToS(pu, pv);
const a = P.prev[i];
if (a) {
const n = Math.max(1, Math.ceil((Math.hypot((q.x - a.x) * (w / h), q.y - a.y) * GH) / 0.6));
for (let j = 1; j <= n; j++) stamp(a.x + ((q.x - a.x) * j) / n, a.y + ((q.y - a.y) * j) / n, 0.0052, 2.8);
}
P.prev[i] = q;
});
}
};
// ---- cursor: your finger wipes the fog; a click breathes on the glass ----
const pxTo = gsap.quickTo(s, "px", { duration: 1.6, ease: "power3" });
const pyTo = gsap.quickTo(s, "py", { duration: 1.6, ease: "power3" });
let last: { x: number; y: number } | null = null;
// the kitten watches your finger: its head turns toward it, the nearest paw reaches for it,
// and a quick swipe past a paw sets it scratching
const lxTo = gsap.quickTo(k, "lx", { duration: 0.9, ease: "power3" });
const lyTo = gsap.quickTo(k, "ly", { duration: 0.9, ease: "power3" });
const ltTo = gsap.quickTo(k, "lt", { duration: 1.1, ease: "power3" });
const elTo = gsap.quickTo(k, "elk", { duration: 0.7, ease: "power3" });
const erTo = gsap.quickTo(k, "erk", { duration: 0.7, ease: "power3" });
const reachOf = (P: Paw) => ({
x: gsap.quickTo(P, "rx", { duration: 0.55, ease: "power3" }),
y: gsap.quickTo(P, "ry", { duration: 0.55, ease: "power3" }),
l: gsap.quickTo(P, "rl", { duration: 0.4, ease: "power2" }),
});
const reaches = new Map([
[L, reachOf(L)],
[R, reachOf(R)],
]);
const clamp = (x: number, m: number) => Math.max(-m, Math.min(m, x));
const settle = () => {
lxTo(0);
lyTo(0);
ltTo(0);
elTo(0);
erTo(0);
reaches.forEach((r) => {
r.x(0);
r.y(0);
r.l(0);
});
};
const kittenSees = (p: { x: number; y: number }, prevP: { x: number; y: number } | null) => {
if (!props.current.moving) return settle();
const hs = uvToS(0.55, 0.42);
const ha = w / h;
lxTo(clamp((p.x - hs.x) * ha * 0.018, 0.012));
lyTo(clamp((p.y - hs.y) * 0.014, 0.009));
ltTo(clamp((hs.x - p.x) * ha * 0.06, 0.06));
// the ear on your side swivels toward you
const side = clamp((p.x - hs.x) * ha * 2, 1);
elTo(side < 0 ? -side * 0.09 : 0);
erTo(side > 0 ? -side * 0.09 : 0);
const speed = prevP ? Math.hypot((p.x - prevP.x) * ha, p.y - prevP.y) : 0;
reaches.forEach((r, P) => {
const c = uvToS(P.home[0], P.home[1]);
const dx = (p.x - c.x) * ha, dy = p.y - c.y;
const near = Math.max(0, 1 - Math.hypot(dx, dy) / 0.3);
r.x(clamp(dx * 0.22 * near, 0.028));
r.y(clamp(dy * 0.22 * near, 0.028));
r.l(near * 0.3);
if (near > 0.35 && speed > 0.03) scratchBout(P, 1 + Math.floor(Math.random() * 2), true);
});
};
const toS = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { x: (e.clientX - r.left) / r.width, y: (e.clientY - r.top) / r.height };
};
const move = (e: PointerEvent) => {
if (!props.current.interactive || !started) return;
const p = toS(e);
pxTo((p.x - 0.5) * -0.8);
pyTo((p.y - 0.5) * 0.8);
kittenSees(p, last);
const rad = 0.062 * props.current.brush;
if (last) {
// stamp along the stroke so a fast swipe leaves an unbroken path
const dx = (p.x - last.x) * (w / h), dy = p.y - last.y;
const n = Math.max(1, Math.ceil(Math.hypot(dx, dy) / (rad * 0.3)));
for (let i = 1; i <= n; i++) stamp(last.x + ((p.x - last.x) * i) / n, last.y + (dy * i) / n, rad, 1.5);
} else stamp(p.x, p.y, rad, 1.5);
last = p;
};
const leave = () => {
last = null;
settle();
pxTo(0);
pyTo(0);
};
const down = (e: PointerEvent) => {
if (!props.current.interactive || !started) return;
const p = toS(e);
const b = { r: 0.02, a: 1 };
gsap.to(b, {
r: 0.24 * Math.max(0.6, props.current.brush),
duration: 1.1,
ease: "expo.out",
onUpdate: () => breathe(p.x, p.y, b.r, b.a),
});
// it startles: both paws scrabble at the glass and it presses in close
if (props.current.moving) {
kill(L);
kill(R);
flick("el", 1.2);
flick("er", 1.2);
scratchBout(L, 3, true);
gsap.delayedCall(0.12, () => scratchBout(R, 3, true));
gsap.to(k, { lean: 1, duration: 0.5, ease: "power2.out", yoyo: true, repeat: 1, repeatDelay: 1.2 });
}
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- scroll: a slow push in, with the paws shifting in front of the room ----
const pushTo = gsap.quickTo(s, "push", { duration: 1.2, ease: "power3" });
const syTo = gsap.quickTo(s, "sy", { duration: 1.2, ease: "power3" });
const st = ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
pushTo(Math.max(0, self.progress - 0.5) * 0.24);
syTo((self.progress - 0.5) * 1.2);
},
});
// ---- loop, only while on screen ----
let visible = true;
let prev = performance.now();
const tick = () => {
const now = performance.now();
const dt = Math.min(0.05, (now - prev) / 1000);
prev = now;
if (!visible || loaded < srcs.length) return;
time += dt;
// the glass fogs back over: wipes and trails fade, breath disperses
const fade = (dt * 1.5) / Math.max(0.5, props.current.fogBack);
for (let i = 0; i < wipe.length; i++) {
if (wipe[i] > 0) wipe[i] = Math.max(0, wipe[i] - fade);
if (breath[i] > 0) breath[i] = Math.max(0, breath[i] * (1 - dt * 0.7) - dt * 0.03);
}
stepDrops(dt);
stepClaws();
draw();
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
io.observe(host);
gsap.ticker.add(tick);
return () => {
alive = false;
gsap.ticker.remove(tick);
intro.kill();
nextDrop?.kill();
firstDrops?.kill();
st.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
kill(L);
kill(R);
nextBout?.kill();
nextFlick?.kill();
look?.kill();
wake?.kill();
breathing.kill();
gsap.killTweensOf([s, k, L, R]);
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [scroller, image, clearImage, depthImage, rigImage, pawsImage]);
return (
<div ref={hostRef} className={["isolate overflow-hidden", className].filter(Boolean).join(" ")} style={{ background: light }}>
{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
17 propsimagestringThe fogged photo.default "/backgrounds/frosted-cat/cat.jpg"default "/backgrounds/frosted-cat/cat.jpg"clearImagestringThe same photo with the fog wiped away, shown where the glass is wiped.default "/backgrounds/frosted-cat/clear.jpg"default "/backgrounds/frosted-cat/clear.jpg"depthImagestringDepth map: R = depth, white = near (parallax); G = the body (breathing).default "/backgrounds/frosted-cat/depth.png"default "/backgrounds/frosted-cat/depth.png"rigImagestringRig mask for the kitten's head (B channel).default "/backgrounds/frosted-cat/rig.png"default "/backgrounds/frosted-cat/rig.png"pawsImagestringThe paws as solid layers: the photo minus the glass behind them.default "/backgrounds/frosted-cat/diff.png"default "/backgrounds/frosted-cat/diff.png"shadowstringColour of the darkest shadows.default "#030305"default "#030305"lightstringColour of the brightest light.default "#f6f6fc"default "#f6f6fc"fognumberFog density: 0 is nearly clear glass, 1 the photo as shot, more is thicker.default 1default 1brushnumberSize of the cursor's wipe.default 1default 1fogBacknumberSeconds a wiped patch takes to fog over again.default 5default 5dropletsnumberHow often drops run down the glass (0 = never).default 1default 1beadsbooleanWater beads along the edges of wipes and trails.default truedefault truemovingbooleanThe kitten scratches at the glass, breathes, looks around and reacts to you. Turn off for your own photo.default truedefault trueinteractivebooleanCursor wipes and click breaths.default truedefault truescrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the glass.classNamestringSize it, e.g. h-screen w-full.