Loading…
← All components

Cursors · Ink

Ink Streak

A calligraphy brush painting a smooth ink stroke, thin when fast, fading from its tail.

Move over the words, then the buttons.

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

Customize

Ink#e4e4e7

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add ink-streak
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/FluidPaperStreakCursor.tsx and paste it in.

components/FluidPaperStreakCursor.tsx
1"use client";
2
3import { memo, useEffect, 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/**
161 * Full-viewport 2D canvas sized to the window (pixel ratio capped at 2) and kept in sync on resize.
162 * Returns the canvas ref; read `canvas.getContext("2d")` in the loop. Draw in CSS pixels.
163 */
164function useViewportCanvas(enabled: boolean) {
165 const ref = useRef<HTMLCanvasElement>(null);
166 useEffect(() => {
167 if (!enabled) return undefined;
168 const canvas = ref.current;
169 if (!canvas) return undefined;
170 const resize = () => {
171 const dpr = Math.min(window.devicePixelRatio || 1, 2);
172 canvas.width = Math.round(window.innerWidth * dpr);
173 canvas.height = Math.round(window.innerHeight * dpr);
174 canvas.getContext("2d")?.setTransform(dpr, 0, 0, dpr, 0, 0);
175 };
176 resize();
177 window.addEventListener("resize", resize);
178 return () => window.removeEventListener("resize", resize);
179 }, [enabled]);
180 return ref;
181}
182
183/** Clears a canvas drawn in CSS pixels. */
184function clearCanvas(ctx: CanvasRenderingContext2D) {
185 ctx.save();
186 ctx.setTransform(1, 0, 0, 1, 0, 0);
187 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
188 ctx.restore();
189}
190
191/** "#rrggbb" (or "#rgb") to "r, g, b", for rgba() strings. */
192function hexToRgb(hex: string) {
193 let h = hex.replace("#", "");
194 if (h.length === 3) h = h.split("").map((c) => c + c).join("");
195 const n = parseInt(h.slice(0, 6), 16);
196 if (Number.isNaN(n)) return "255, 255, 255";
197 return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
198}
199
200/** 4. Rendering, materials and glow. */
201interface MaterialProps {
202 /** Main color of the object. Default "#6366f1". */
203 primaryColor?: string;
204 /** Color of the glow. Default "#00f0ff". */
205 emissiveColor?: string;
206 /** Strength of the glow, 0 (none) to 3. Default 1. */
207 emissiveIntensity?: number;
208 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
209 roughness?: number;
210 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
211 metalness?: number;
212 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
213 transmission?: number;
214 /** Technical wireframe rendering. Default false. */
215 wireframe?: boolean;
216}
217
218export interface FluidStreakProps extends Pick<MaterialProps, "primaryColor"> {
219 /** Ink color. Default "#e4e4e7" (use a dark ink on light pages). */
220 primaryColor?: string;
221 /** @deprecated Use `primaryColor`. */
222 inkColor?: string;
223 /** How long a stroke takes to fade away, in milliseconds. Default 900. */
224 fadeDuration?: number;
225 /** Width of the stroke when the brush moves slowly, in CSS pixels. Default 14. */
226 strokeWidth?: number;
227 /** Hide the system cursor. Default false. */
228 hideNativeCursor?: boolean;
229}
230
231interface Sample {
232 x: number;
233 y: number;
234 t: number; // ms
235 w: number; // width at this point
236}
237
238const MIN_GAP = 2; // px between recorded samples
239
240/** Catmull-Rom point between p1 and p2 (t in 0..1). */
241function catmull(p0: number, p1: number, p2: number, p3: number, t: number) {
242 const t2 = t * t;
243 const t3 = t2 * t;
244 return 0.5 * (2 * p1 + (-p0 + p2) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 + (-p0 + 3 * p1 - 3 * p2 + p3) * t3);
245}
246
247/**
248 * A calligraphy brush: the pointer paints a stroke of ink, smoothed with Catmull-Rom splines, thick
249 * when slow and thin when fast (like a brush lifting off the paper), with dry-bristle strands along
250 * its edges. The stroke fades from its tail within `fadeDuration`.
251 */
252function FluidPaperStreakCursor({ primaryColor, inkColor = primaryColor ?? "#e4e4e7", fadeDuration = 900, strokeWidth = 14, hideNativeCursor = false }: FluidStreakProps) {
253 const enabled = useCursorEnabled();
254 const pointer = useCursorPointer(enabled);
255 useHideNativeCursor(enabled && hideNativeCursor);
256 const canvas = useViewportCanvas(enabled);
257 const samples = useRef<Sample[]>([]);
258 const wasVisible = useRef(false);
259
260 useAnimationFrame(enabled, (dt) => {
261 const ctx = canvas.current?.getContext("2d");
262 if (!ctx) return;
263 const p = pointer.current;
264 settlePointer(p, dt);
265 const now = performance.now();
266 const list = samples.current;
267
268 if (p.visible) {
269 const last = list[list.length - 1];
270 if (!wasVisible.current) list.length = 0; // a new stroke after leaving the window
271 if (!last || Math.hypot(p.x - last.x, p.y - last.y) >= MIN_GAP) {
272 // Faster → thinner; smoothed with the previous width so the edge stays fluid
273 const target = strokeWidth * Math.max(0.18, 1 - Math.min(p.speed / 2600, 0.82));
274 const w = last ? last.w + (target - last.w) * 0.35 : target * 0.4;
275 list.push({ x: p.x, y: p.y, t: now, w });
276 }
277 }
278 wasVisible.current = p.visible;
279 while (list.length && now - list[0].t > fadeDuration) list.shift();
280
281 clearCanvas(ctx);
282 if (list.length < 2) return;
283 const rgb = hexToRgb(inkColor);
284
285 // Spline points with their width and age
286 const pts: { x: number; y: number; w: number; a: number }[] = [];
287 for (let i = 0; i < list.length - 1; i++) {
288 const p0 = list[Math.max(0, i - 1)];
289 const p1 = list[i];
290 const p2 = list[i + 1];
291 const p3 = list[Math.min(list.length - 1, i + 2)];
292 const steps = Math.max(1, Math.ceil(Math.hypot(p2.x - p1.x, p2.y - p1.y) / 3));
293 for (let s = 0; s < steps; s++) {
294 const t = s / steps;
295 const age = (now - (p1.t + (p2.t - p1.t) * t)) / fadeDuration;
296 pts.push({
297 x: catmull(p0.x, p1.x, p2.x, p3.x, t),
298 y: catmull(p0.y, p1.y, p2.y, p3.y, t),
299 w: p1.w + (p2.w - p1.w) * t,
300 a: 1 - Math.min(Math.max(age, 0), 1),
301 });
302 }
303 }
304 const tip = list[list.length - 1];
305 pts.push({ x: tip.x, y: tip.y, w: tip.w, a: 1 });
306
307 // Main stroke: quads between consecutive spline points, each with its own width and fade.
308 // The tail tapers to a point as it dries.
309 ctx.lineCap = "round";
310 ctx.lineJoin = "round";
311 for (let i = 1; i < pts.length; i++) {
312 const a = pts[i - 1];
313 const b = pts[i];
314 const taper = Math.min(1, (i / pts.length) * 4);
315 ctx.strokeStyle = `rgba(${rgb}, ${(b.a ** 1.5 * 0.92).toFixed(3)})`;
316 ctx.lineWidth = Math.max(0.5, b.w * taper * (0.4 + b.a * 0.6));
317 ctx.beginPath();
318 ctx.moveTo(a.x, a.y);
319 ctx.lineTo(b.x, b.y);
320 ctx.stroke();
321 }
322 // Dry-bristle strands along both edges
323 for (const side of [-1, 1]) {
324 ctx.beginPath();
325 for (let i = 1; i < pts.length; i++) {
326 const a = pts[i - 1];
327 const b = pts[i];
328 const len = Math.hypot(b.x - a.x, b.y - a.y) || 1;
329 const nx = (-(b.y - a.y) / len) * side;
330 const ny = ((b.x - a.x) / len) * side;
331 const off = b.w * 0.62;
332 if (i === 1) ctx.moveTo(a.x + nx * off, a.y + ny * off);
333 ctx.lineTo(b.x + nx * off, b.y + ny * off);
334 }
335 ctx.strokeStyle = `rgba(${rgb}, 0.22)`;
336 ctx.lineWidth = 1;
337 ctx.stroke();
338 }
339 });
340
341 if (!enabled) return null;
342 return <canvas ref={canvas} aria-hidden="true" style={OVERLAY_STYLE} />;
343}
344
345export default memo(FluidPaperStreakCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import FluidPaperStreakCursor from "@/components/FluidPaperStreakCursor";
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 <FluidPaperStreakCursor
8 fadeDuration={900}
9 strokeWidth={14}
10 primaryColor="#e4e4e7"
11 />
12 </div>
13 );
14}
Next componentSaaS Premium Navbar→