Decay Card
A card whose image is displaced by an SVG turbulence filter that follows pointer speed and drifts toward the cursor.
pnpm dlx shadcn@latest add @leo/decay-cardProps
| Name | Type | Default | Description |
|---|---|---|---|
| baseFrequency | number | undefined | 0.015 | |
| children | ReactNode | - | |
| className | string | undefined | - | |
| height | number | undefined | 400 | |
| image | string | undefined | `data:image/svg+xml;charset=utf-8,${encodeURIComponent( '<svg xmlns="http://www.w3.org/2000/svg" width="600" height="750" viewBox="0 0 600 750">' + '<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#2b2b2b"/><stop offset="1" stop-color="#bdbdbd"/></linearGradient></defs>' + '<rect width="600" height="750" fill="url(#g)"/>' + '<circle cx="410" cy="230" r="120" fill="#f2f2f2" fill-opacity="0.85"/>' + '<path d="M0 520 Q150 330 320 500 T600 450 V750 H0Z" fill="#111" fill-opacity="0.75"/>' + '<path d="M0 620 Q200 500 400 610 T600 580 V750 H0Z" fill="#000" fill-opacity="0.8"/>' + "</svg>", )}` | |
| maxDisplacement | number | undefined | 400 | |
| movementBound | number | undefined | 50 | |
| numOctaves | number | undefined | 5 | |
| seed | number | undefined | 4 | |
| width | number | undefined | 300 |
Dependencies
- gsap@^3.13.0
Source
"use client";
import { gsap } from "gsap";
import React, { type ReactNode, useEffect, useId, useRef } from "react";
import { cn } from "@/lib/utils";
// Generated grayscale artwork so the default image loads nothing from the network.
const DEFAULT_IMAGE = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="600" height="750" viewBox="0 0 600 750">' +
'<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#2b2b2b"/><stop offset="1" stop-color="#bdbdbd"/></linearGradient></defs>' +
'<rect width="600" height="750" fill="url(#g)"/>' +
'<circle cx="410" cy="230" r="120" fill="#f2f2f2" fill-opacity="0.85"/>' +
'<path d="M0 520 Q150 330 320 500 T600 450 V750 H0Z" fill="#111" fill-opacity="0.75"/>' +
'<path d="M0 620 Q200 500 400 610 T600 580 V750 H0Z" fill="#000" fill-opacity="0.8"/>' +
"</svg>",
)}`;
export interface DecayCardProps {
className?: string;
width?: number;
height?: number;
image?: string;
baseFrequency?: number;
numOctaves?: number;
seed?: number;
maxDisplacement?: number;
movementBound?: number;
children?: ReactNode;
}
const DecayCard: React.FC<DecayCardProps> = ({
className,
width = 300,
height = 400,
image = DEFAULT_IMAGE,
baseFrequency = 0.015,
numOctaves = 5,
seed = 4,
maxDisplacement = 400,
movementBound = 50,
children,
}) => {
const filterId = `leo-decay-${useId().replace(/:/g, "")}`;
const svgRef = useRef<HTMLDivElement | null>(null);
const displacementMapRef = useRef<SVGFEDisplacementMapElement | null>(null);
const cursor = useRef<{ x: number; y: number }>({
x: typeof window !== "undefined" ? window.innerWidth / 2 : 0,
y: typeof window !== "undefined" ? window.innerHeight / 2 : 0,
});
const cachedCursor = useRef<{ x: number; y: number }>({ ...cursor.current });
const winsize = useRef<{ width: number; height: number }>({
width: typeof window !== "undefined" ? window.innerWidth : 0,
height: typeof window !== "undefined" ? window.innerHeight : 0,
});
useEffect(() => {
const lerp = (a: number, b: number, n: number): number => (1 - n) * a + n * b;
const map = (x: number, a: number, b: number, c: number, d: number): number =>
((x - a) * (d - c)) / (b - a) + c;
const distance = (x1: number, x2: number, y1: number, y2: number): number =>
Math.hypot(x1 - x2, y1 - y2);
const handleResize = (): void => {
winsize.current = {
width: window.innerWidth,
height: window.innerHeight,
};
};
const handleMouseMove = (ev: MouseEvent): void => {
cursor.current = { x: ev.clientX, y: ev.clientY };
};
window.addEventListener("resize", handleResize);
window.addEventListener("mousemove", handleMouseMove);
const imgValues = {
imgTransforms: { x: 0, y: 0, rz: 0 },
displacementScale: 0,
};
const render = () => {
let targetX = lerp(
imgValues.imgTransforms.x,
map(cursor.current.x, 0, winsize.current.width, -120, 120),
0.1,
);
let targetY = lerp(
imgValues.imgTransforms.y,
map(cursor.current.y, 0, winsize.current.height, -120, 120),
0.1,
);
const targetRz = lerp(
imgValues.imgTransforms.rz,
map(cursor.current.x, 0, winsize.current.width, -10, 10),
0.1,
);
if (targetX > movementBound) targetX = movementBound + (targetX - movementBound) * 0.2;
if (targetX < -movementBound) targetX = -movementBound + (targetX + movementBound) * 0.2;
if (targetY > movementBound) targetY = movementBound + (targetY - movementBound) * 0.2;
if (targetY < -movementBound) targetY = -movementBound + (targetY + movementBound) * 0.2;
imgValues.imgTransforms.x = targetX;
imgValues.imgTransforms.y = targetY;
imgValues.imgTransforms.rz = targetRz;
if (svgRef.current) {
gsap.set(svgRef.current, {
x: imgValues.imgTransforms.x,
y: imgValues.imgTransforms.y,
rotateZ: imgValues.imgTransforms.rz,
});
}
const cursorTravelledDistance = distance(
cachedCursor.current.x,
cursor.current.x,
cachedCursor.current.y,
cursor.current.y,
);
imgValues.displacementScale = lerp(
imgValues.displacementScale,
map(cursorTravelledDistance, 0, 200, 0, maxDisplacement),
0.06,
);
if (displacementMapRef.current) {
gsap.set(displacementMapRef.current, {
attr: { scale: imgValues.displacementScale },
});
}
cachedCursor.current = { ...cursor.current };
rafId = requestAnimationFrame(render);
};
let rafId = requestAnimationFrame(render);
return () => {
cancelAnimationFrame(rafId);
window.removeEventListener("resize", handleResize);
window.removeEventListener("mousemove", handleMouseMove);
};
}, [maxDisplacement, movementBound]);
return (
<div
ref={svgRef}
className={cn("relative", className)}
style={{ width: `${width}px`, height: `${height}px` }}
>
{/* biome-ignore lint/a11y/noSvgWithoutTitle: decorative; the card content is the accessible text */}
<svg
viewBox="-60 -75 720 900"
preserveAspectRatio="xMidYMid slice"
className="relative w-full h-full block [will-change:transform]"
>
<filter id={filterId}>
<feTurbulence
type="turbulence"
baseFrequency={baseFrequency}
numOctaves={numOctaves}
seed={seed}
stitchTiles="stitch"
x="0%"
y="0%"
width="100%"
height="100%"
result="turbulence1"
/>
<feDisplacementMap
ref={displacementMapRef}
in="SourceGraphic"
in2="turbulence1"
scale="0"
xChannelSelector="R"
yChannelSelector="B"
x="0%"
y="0%"
width="100%"
height="100%"
result="displacementMap3"
/>
</filter>
<g>
<image
href={image}
x="0"
y="0"
width="600"
height="750"
filter={`url(#${filterId})`}
preserveAspectRatio="xMidYMid slice"
/>
</g>
</svg>
<div className="absolute bottom-[1.2em] left-[1em] tracking-[-0.5px] font-black text-[2.5rem] leading-[1.5em] first-line:text-[6rem]">
{children}
</div>
</div>
);
};
export { DecayCard };
export default DecayCard;