Blob Cursor
Pointer-following gooey blobs with a lagging trail, built from an SVG blur and colour-matrix filter, with a resting position before first move.
pnpm dlx shadcn@latest add @leo/blob-cursorProps
| Name | Type | Default | Description |
|---|---|---|---|
| blobType | undefined | "circle" | "square" | circle | |
| className | string | undefined | - | |
| fastDuration | number | undefined | 0.1 | |
| fastEase | string | undefined | power3.out | |
| fillColor | string | undefined | #5227FF | |
| filterColorMatrixValues | string | undefined | 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 35 -10 | |
| filterId | string | undefined | blob | |
| filterStdDeviation | number | undefined | 30 | |
| initialPosition | { x: number; y: number; } | undefined | { x: 0.5, y: 0.5 } | Where the blobs rest before the pointer first moves, as fractions (0-1) of the container. |
| innerColor | string | undefined | rgba(255,255,255,0.8) | |
| innerSizes | number[] | undefined | [20, 35, 25] | |
| opacities | number[] | undefined | [0.6, 0.6, 0.6] | |
| shadowBlur | number | undefined | 5 | |
| shadowColor | string | undefined | rgba(0,0,0,0.75) | |
| shadowOffsetX | number | undefined | 10 | |
| shadowOffsetY | number | undefined | 10 | |
| sizes | number[] | undefined | [60, 125, 75] | |
| slowDuration | number | undefined | 0.5 | |
| slowEase | string | undefined | power1.out | |
| trailCount | number | undefined | 3 | |
| useFilter | boolean | undefined | true | |
| zIndex | number | undefined | 100 |
Dependencies
- gsap@^3.13.0
Source
"use client";
import gsap from "gsap";
import type React from "react";
import { useCallback, useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
export interface BlobCursorProps {
blobType?: "circle" | "square";
fillColor?: string;
trailCount?: number;
sizes?: number[];
innerSizes?: number[];
innerColor?: string;
opacities?: number[];
shadowColor?: string;
shadowBlur?: number;
shadowOffsetX?: number;
shadowOffsetY?: number;
filterId?: string;
filterStdDeviation?: number;
filterColorMatrixValues?: string;
useFilter?: boolean;
fastDuration?: number;
slowDuration?: number;
fastEase?: string;
slowEase?: string;
zIndex?: number;
/** Where the blobs rest before the pointer first moves, as fractions (0-1) of the container. */
initialPosition?: { x: number; y: number };
className?: string;
}
// Horizontal gap between resting blobs so the whole trail is visible before any pointer input.
const RESTING_SPREAD = 70;
export function BlobCursor({
blobType = "circle",
fillColor = "#5227FF",
trailCount = 3,
sizes = [60, 125, 75],
innerSizes = [20, 35, 25],
innerColor = "rgba(255,255,255,0.8)",
opacities = [0.6, 0.6, 0.6],
shadowColor = "rgba(0,0,0,0.75)",
shadowBlur = 5,
shadowOffsetX = 10,
shadowOffsetY = 10,
filterId = "blob",
filterStdDeviation = 30,
filterColorMatrixValues = "1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 35 -10",
useFilter = true,
fastDuration = 0.1,
slowDuration = 0.5,
fastEase = "power3.out",
slowEase = "power1.out",
zIndex = 100,
initialPosition = { x: 0.5, y: 0.5 },
className,
}: BlobCursorProps) {
const containerRef = useRef<HTMLDivElement>(null);
const blobsRef = useRef<(HTMLDivElement | null)[]>([]);
const updateOffset = useCallback(() => {
if (!containerRef.current) return { left: 0, top: 0 };
const rect = containerRef.current.getBoundingClientRect();
return { left: rect.left, top: rect.top };
}, []);
const handleMove = useCallback(
(e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>) => {
const { left, top } = updateOffset();
const x = "clientX" in e ? e.clientX : e.touches[0].clientX;
const y = "clientY" in e ? e.clientY : e.touches[0].clientY;
blobsRef.current.forEach((el, i) => {
if (!el) return;
const isLead = i === 0;
gsap.to(el, {
x: x - left,
y: y - top,
duration: isLead ? fastDuration : slowDuration,
ease: isLead ? fastEase : slowEase,
});
});
},
[updateOffset, fastDuration, slowDuration, fastEase, slowEase],
);
// Mount-only: place the blobs at their resting spot; pointer moves take over afterwards.
// biome-ignore lint/correctness/useExhaustiveDependencies: initialPosition is read once on mount
useEffect(() => {
const container = containerRef.current;
if (!container) return;
const { width, height } = container.getBoundingClientRect();
blobsRef.current.forEach((el, i) => {
if (!el) return;
gsap.set(el, {
x: initialPosition.x * width + (i - (trailCount - 1) / 2) * RESTING_SPREAD,
y: initialPosition.y * height,
});
});
}, []);
// Blobs are captured here because callback refs are cleared before cleanup runs.
useEffect(() => {
const blobs = blobsRef.current.filter((el): el is HTMLDivElement => el !== null);
return () => {
gsap.killTweensOf(blobs);
};
}, []);
useEffect(() => {
const onResize = () => updateOffset();
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
}, [updateOffset]);
return (
// biome-ignore lint/a11y/noStaticElementInteractions: pointer tracking surface, purely decorative
<div
ref={containerRef}
onMouseMove={handleMove}
onTouchMove={handleMove}
className={cn("relative top-0 left-0 w-full h-full", className)}
style={{ zIndex }}
>
{useFilter && (
<svg aria-hidden="true" className="absolute w-0 h-0">
<filter id={filterId}>
<feGaussianBlur in="SourceGraphic" result="blur" stdDeviation={filterStdDeviation} />
<feColorMatrix in="blur" values={filterColorMatrixValues} />
</filter>
</svg>
)}
<div
className="pointer-events-none absolute inset-0 overflow-hidden select-none cursor-default"
style={{ filter: useFilter ? `url(#${filterId})` : undefined }}
>
{Array.from({ length: trailCount }).map((_, i) => (
<div
// biome-ignore lint/suspicious/noArrayIndexKey: the trail is positional and never reordered
key={i}
ref={(el) => {
blobsRef.current[i] = el;
}}
className="absolute will-change-transform transform -translate-x-1/2 -translate-y-1/2"
style={{
width: sizes[i],
height: sizes[i],
borderRadius: blobType === "circle" ? "50%" : "0",
backgroundColor: fillColor,
opacity: opacities[i],
boxShadow: `${shadowOffsetX}px ${shadowOffsetY}px ${shadowBlur}px 0 ${shadowColor}`,
}}
>
<div
className="absolute"
style={{
width: innerSizes[i],
height: innerSizes[i],
top: (sizes[i] - innerSizes[i]) / 2,
left: (sizes[i] - innerSizes[i]) / 2,
backgroundColor: innerColor,
borderRadius: blobType === "circle" ? "50%" : "0",
}}
/>
</div>
))}
</div>
</div>
);
}
export default BlobCursor;