Loading…
← All components

Cursors · Gooey

Elastic Blob

A gooey jelly drop that squashes and stretches with your speed and is pulled toward nearby buttons.

Move over the words, then the buttons.

Read the docsdata-cursor-text
<MagneticElasticBlobCursor />Live

Customize

Color#a78bfa

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add elastic-blob
01

Install

The 3D packages this component is built on.

Nothing to install: React is all it needs.

02

Add the component

One self-contained file: create components/MagneticElasticBlobCursor.tsx and paste it in.

components/MagneticElasticBlobCursor.tsx
1"use client";
2
3import { memo, useEffect, useId, useRef, useState, type CSSProperties, type RefObject } from "react";
4
5/** What counts as interactive: the cursors react when the pointer is over one of these. */
6const INTERACTIVE_SELECTOR =
7 "a, button, [role='button'], input, select, textarea, label, summary, [data-cursor], [data-cursor-text], [data-cursor-hover]";
8
9/** Full-viewport, click-through layer above the page. */
10const OVERLAY_STYLE: CSSProperties = {
11 position: "fixed",
12 top: 0,
13 left: 0,
14 width: "100vw",
15 height: "100vh",
16 pointerEvents: "none",
17 zIndex: 9999,
18};
19
20/**
21 * False on touch screens (`pointer: coarse`) and during server rendering: the custom cursor is not
22 * drawn and the native cursor is left alone. Follows the media query live (tablet + mouse…).
23 */
24function useCursorEnabled() {
25 const [enabled, setEnabled] = useState(false);
26 useEffect(() => {
27 const query = window.matchMedia("(pointer: coarse)");
28 const sync = () => setEnabled(!query.matches);
29 sync();
30 query.addEventListener("change", sync);
31 return () => query.removeEventListener("change", sync);
32 }, []);
33 return enabled;
34}
35
36/** Hides the system cursor on the whole page while `active` (restored on unmount). */
37function useHideNativeCursor(active: boolean) {
38 useEffect(() => {
39 if (!active) return undefined;
40 const style = document.createElement("style");
41 style.setAttribute("data-custom-cursor", "");
42 style.textContent = "*, *::before, *::after { cursor: none !important; }";
43 document.head.appendChild(style);
44 return () => style.remove();
45 }, [active]);
46}
47
48interface CursorPointer {
49 /** Position in CSS pixels, relative to the viewport. */
50 x: number;
51 y: number;
52 /** Smoothed velocity, in CSS pixels per second. */
53 vx: number;
54 vy: number;
55 speed: number;
56 /** False until the first move, and while the pointer is outside the window. */
57 visible: boolean;
58 down: boolean;
59 /** The interactive element under the pointer (closest match of the selector), or null. */
60 target: HTMLElement | null;
61 /** Incremented on every press, to trigger click effects. */
62 clicks: number;
63}
64
65/**
66 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
67 * moving the mouse never re-renders React. All listeners are removed on unmount.
68 */
69function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
70 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
71
72 useEffect(() => {
73 if (!enabled) return undefined;
74 let last = performance.now();
75 const p = pointer.current;
76 const pick = (event: PointerEvent) => {
77 const el = event.target instanceof Element ? event.target.closest(selector) : null;
78 p.target = el instanceof HTMLElement ? el : null;
79 };
80 const move = (event: PointerEvent) => {
81 if (event.pointerType === "touch") return;
82 const now = performance.now();
83 const dt = Math.max(1, now - last) / 1000;
84 last = now;
85 if (p.visible) {
86 // Smoothed: one jittery event must not make the effects jump
87 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
88 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
89 }
90 p.x = event.clientX;
91 p.y = event.clientY;
92 p.visible = true;
93 pick(event);
94 };
95 const down = (event: PointerEvent) => {
96 if (event.pointerType === "touch") return;
97 p.down = true;
98 p.clicks += 1;
99 pick(event);
100 };
101 const up = () => {
102 p.down = false;
103 };
104 const leave = () => {
105 p.visible = false;
106 p.down = false;
107 };
108 window.addEventListener("pointermove", move, { passive: true });
109 window.addEventListener("pointerdown", down, { passive: true });
110 window.addEventListener("pointerup", up, { passive: true });
111 document.documentElement.addEventListener("pointerleave", leave);
112 window.addEventListener("blur", leave);
113 return () => {
114 window.removeEventListener("pointermove", move);
115 window.removeEventListener("pointerdown", down);
116 window.removeEventListener("pointerup", up);
117 document.documentElement.removeEventListener("pointerleave", leave);
118 window.removeEventListener("blur", leave);
119 };
120 }, [enabled, selector]);
121
122 return pointer;
123}
124
125/**
126 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
127 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
128 */
129function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
130 const frameRef = useRef(frame);
131 useEffect(() => {
132 frameRef.current = frame;
133 });
134 useEffect(() => {
135 if (!enabled) return undefined;
136 let id = 0;
137 let last = performance.now();
138 const tick = (now: number) => {
139 const dt = Math.min((now - last) / 1000, 1 / 20);
140 last = now;
141 frameRef.current(dt, now / 1000);
142 id = requestAnimationFrame(tick);
143 };
144 id = requestAnimationFrame(tick);
145 return () => cancelAnimationFrame(id);
146 }, [enabled]);
147}
148
149/**
150 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
151 * stops, and read `speed` afterwards.
152 */
153function settlePointer(p: CursorPointer, dt: number) {
154 const k = Math.exp(-10 * dt);
155 p.vx *= k;
156 p.vy *= k;
157 p.speed = Math.hypot(p.vx, p.vy);
158}
159
160/** Exponential smoothing factor for a frame: frame-rate independent `lerp`. */
161const damp = (lambda: number, dt: number) => 1 - Math.exp(-lambda * dt);
162
163/** 1. Cursor: how the pointer pulls, pushes and drags the object. */
164interface CursorProps {
165 /** Reach of the pointer's influence, in the component's own space: CSS pixels for pixel-laid-out widgets, world units for 3D scenes (each component says which). Default 220 (px). */
166 cursorRadius?: number;
167 /** Positive pulls things towards the pointer, negative pushes them away (repulsion, shockwave). Default 1. */
168 cursorForce?: number;
169 /** How far the object slides towards the pointer, 0 (stays put) to 1 (follows closely). Default 0. */
170 magneticStrength?: number;
171 /** Share of the remaining distance the motion closes each frame (at 60 fps), 0.01 (heavy, slow) to 1 (instant). Default 0.1. */
172 cursorDamping?: number;
173}
174
175/** 4. Rendering, materials and glow. */
176interface MaterialProps {
177 /** Main color of the object. Default "#6366f1". */
178 primaryColor?: string;
179 /** Color of the glow. Default "#00f0ff". */
180 emissiveColor?: string;
181 /** Strength of the glow, 0 (none) to 3. Default 1. */
182 emissiveIntensity?: number;
183 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
184 roughness?: number;
185 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
186 metalness?: number;
187 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
188 transmission?: number;
189 /** Technical wireframe rendering. Default false. */
190 wireframe?: boolean;
191}
192
193export interface ElasticBlobProps extends Pick<MaterialProps, "primaryColor">, Pick<CursorProps, "cursorRadius"> {
194 primaryColor?: string;
195 /** @deprecated Use `primaryColor`. */
196 color?: string;
197 /** Largest stretch along the motion (1 = never stretches). Default 1.8. */
198 maxStretch?: number;
199 /** Distance (CSS pixels) at which links and buttons start pulling the blob. Default 90. */
200 cursorRadius?: number;
201 /** @deprecated Use `cursorRadius`. */
202 magneticRadius?: number;
203 /** Radius of the blob at rest, in CSS pixels. Default 14. */
204 size?: number;
205 /** Hide the system cursor while the blob is shown. Default true. */
206 hideNativeCursor?: boolean;
207}
208
209const TRAIL = 5;
210
211/**
212 * A jelly drop: it squashes and stretches with the pointer's acceleration, leaves droplets that
213 * merge back into it (an SVG "gooey" filter: blur + alpha threshold), and is pulled toward nearby
214 * links and buttons as if they were magnetic — swelling when it reaches one.
215 */
216function MagneticElasticBlobCursor({
217 primaryColor,
218 color = primaryColor ?? "#a78bfa",
219 maxStretch = 1.8,
220 cursorRadius,
221 magneticRadius = cursorRadius ?? 90,
222 size = 14,
223 hideNativeCursor = true,
224}: ElasticBlobProps) {
225 const enabled = useCursorEnabled();
226 const pointer = useCursorPointer(enabled);
227 useHideNativeCursor(enabled && hideNativeCursor);
228 const filterId = `goo-${useId().replace(/[^a-zA-Z0-9-]/g, "")}`;
229 const group = useRef<SVGGElement>(null);
230 const head = useRef<SVGEllipseElement>(null);
231 const drops = useRef<(SVGCircleElement | null)[]>([]);
232 const sim = useRef({
233 x: -100,
234 y: -100,
235 vx: 0,
236 vy: 0,
237 stretch: 1,
238 grow: 1,
239 trail: Array.from({ length: TRAIL }, () => ({ x: -100, y: -100 })),
240 });
241 // Interactive elements, refreshed now and then (not every frame)
242 const magnets = useRef<HTMLElement[]>([]);
243 useEffect(() => {
244 if (!enabled) return undefined;
245 const refresh = () => {
246 magnets.current = Array.from(document.querySelectorAll<HTMLElement>(INTERACTIVE_SELECTOR));
247 };
248 refresh();
249 const id = window.setInterval(refresh, 1000);
250 return () => window.clearInterval(id);
251 }, [enabled]);
252
253 useAnimationFrame(enabled, (dt) => {
254 const p = pointer.current;
255 settlePointer(p, dt);
256 const s = sim.current;
257
258 // Magnetic pull: the closest element within reach draws the target toward its center
259 let tx = p.x;
260 let ty = p.y;
261 let pull = 0;
262 for (const el of magnets.current) {
263 const r = el.getBoundingClientRect();
264 if (r.width === 0 || r.bottom < 0 || r.top > window.innerHeight) continue;
265 const cx = r.left + r.width / 2;
266 const cy = r.top + r.height / 2;
267 // Distance to the element's box, not its center: big buttons pull from their edges
268 const gap = Math.hypot(Math.max(r.left - p.x, 0, p.x - r.right), Math.max(r.top - p.y, 0, p.y - r.bottom));
269 const strength = Math.max(0, 1 - gap / magneticRadius);
270 if (strength > pull) {
271 pull = strength;
272 tx = p.x + (cx - p.x) * strength * 0.45;
273 ty = p.y + (cy - p.y) * strength * 0.45;
274 }
275 }
276
277 // Spring toward the target: the lag is what gives the jelly its stretch
278 const stiffness = 260;
279 const damping = 22;
280 s.vx += ((tx - s.x) * stiffness - s.vx * damping) * dt;
281 s.vy += ((ty - s.y) * stiffness - s.vy * damping) * dt;
282 s.x += s.vx * dt;
283 s.y += s.vy * dt;
284 const speed = Math.hypot(s.vx, s.vy);
285 const stretch = 1 + Math.min(speed / 1600, 1) * (maxStretch - 1);
286 s.stretch += (stretch - s.stretch) * damp(14, dt);
287 s.grow += ((p.target ? 2 : p.down ? 0.8 : 1 + pull * 0.35) - s.grow) * damp(9, dt);
288
289 // Droplets follow each other like a chain
290 let lx = s.x;
291 let ly = s.y;
292 s.trail.forEach((d, i) => {
293 const k = damp(26 - i * 3, dt);
294 d.x += (lx - d.x) * k;
295 d.y += (ly - d.y) * k;
296 lx = d.x;
297 ly = d.y;
298 const el = drops.current[i];
299 if (el) {
300 el.setAttribute("cx", d.x.toFixed(1));
301 el.setAttribute("cy", d.y.toFixed(1));
302 el.setAttribute("r", (size * s.grow * (0.78 - i * 0.12)).toFixed(1));
303 }
304 });
305
306 const angle = (Math.atan2(s.vy, s.vx) * 180) / Math.PI;
307 const rx = size * s.grow * s.stretch;
308 const ry = (size * s.grow) / Math.sqrt(s.stretch);
309 head.current?.setAttribute("transform", `translate(${s.x.toFixed(1)} ${s.y.toFixed(1)}) rotate(${angle.toFixed(1)})`);
310 head.current?.setAttribute("rx", rx.toFixed(1));
311 head.current?.setAttribute("ry", ry.toFixed(1));
312 group.current?.setAttribute("opacity", p.visible ? "1" : "0");
313 });
314
315 if (!enabled) return null;
316 return (
317 <svg aria-hidden="true" style={{ ...OVERLAY_STYLE, overflow: "visible" }}>
318 <defs>
319 <filter id={filterId} x="-50%" y="-50%" width="200%" height="200%">
320 <feGaussianBlur in="SourceGraphic" stdDeviation={size * 0.55} result="blur" />
321 {/* Alpha threshold: overlapping blurred shapes fuse into one smooth drop */}
322 <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 22 -9" result="goo" />
323 <feComposite in="SourceGraphic" in2="goo" operator="atop" />
324 </filter>
325 </defs>
326 <g ref={group} filter={`url(#${filterId})`} fill={color} opacity={0} style={{ transition: "opacity 0.2s" }}>
327 {Array.from({ length: TRAIL }, (_, i) => (
328 <circle
329 key={i}
330 ref={(el) => {
331 drops.current[i] = el;
332 }}
333 cx={-100}
334 cy={-100}
335 r={size * 0.6}
336 />
337 ))}
338 <ellipse ref={head} rx={size} ry={size} />
339 </g>
340 </svg>
341 );
342}
343
344export default memo(MagneticElasticBlobCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import MagneticElasticBlobCursor from "@/components/MagneticElasticBlobCursor";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <MagneticElasticBlobCursor
8 maxStretch={1.8}
9 cursorRadius={90}
10 size={14}
11 primaryColor="#a78bfa"
12 />
13 </div>
14 );
15}
Next componentCyber Glitch Reticle→