Cursor trail
A tail of dots chases the pointer around an area, stretching on fast moves and gathering into one point when you stop.
"use client";
import { useEffect, useRef, type CSSProperties, type ReactNode } from "react";
import "./cursor-trail.css";
type CursorTrailProps = {
children?: ReactNode;
/** How many dots make up the tail. */
length?: number;
/** Any CSS color. */
color?: string;
/** 0 to 1. Lower is lazier and longer. */
follow?: number;
className?: string;
/** Match your content's corner radius so the dots are clipped the same way. */
radius?: number | string;
};
/**
* CursorTrail
* A tail of dots that chases the pointer around an area. Each dot eases
* toward the one in front of it, so fast moves stretch the tail and stops
* gather it back into one point. The loop sleeps when nothing moves.
*/
export function CursorTrail({
children,
length = 16,
color = "#ff5a1f",
follow = 0.42,
className = "",
radius = 0,
}: CursorTrailProps) {
const area = useRef<HTMLDivElement>(null);
const dots = useRef<(HTMLSpanElement | null)[]>([]);
useEffect(() => {
const el = area.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const pts = Array.from({ length }, () => ({ x: 0, y: 0 }));
const target = { x: 0, y: 0 };
let frame = 0;
let started = false;
const draw = () => {
frame = 0;
let moving = false;
pts.forEach((p, i) => {
const lead = i === 0 ? target : pts[i - 1];
// Reduced motion: every dot sits on the pointer, so only one shows.
const k = reduce ? 1 : follow;
const dx = lead.x - p.x;
const dy = lead.y - p.y;
p.x += dx * k;
p.y += dy * k;
if (Math.abs(dx) + Math.abs(dy) > 0.1) moving = true;
const dot = dots.current[i];
if (dot) dot.style.transform = `translate(${p.x}px, ${p.y}px)`;
});
if (moving) frame = requestAnimationFrame(draw);
};
const onMove = (e: PointerEvent) => {
const r = el.getBoundingClientRect();
target.x = e.clientX - r.left;
target.y = e.clientY - r.top;
if (!started) {
// First touch: start the whole tail on the pointer, not in a corner.
pts.forEach((p) => Object.assign(p, target));
started = true;
}
el.dataset.active = "true";
if (!frame) frame = requestAnimationFrame(draw);
};
const onLeave = () => {
el.dataset.active = "false";
started = false;
};
el.addEventListener("pointermove", onMove);
el.addEventListener("pointerdown", onMove);
el.addEventListener("pointerleave", onLeave);
el.addEventListener("pointercancel", onLeave);
return () => {
el.removeEventListener("pointermove", onMove);
el.removeEventListener("pointerdown", onMove);
el.removeEventListener("pointerleave", onLeave);
el.removeEventListener("pointercancel", onLeave);
cancelAnimationFrame(frame);
};
}, [length, follow]);
return (
<div
ref={area}
className={`gu-trail ${className}`}
data-active="false"
style={
{ "--gu-trail-color": color, borderRadius: radius } as CSSProperties
}
>
{children}
<div className="gu-trail__dots" aria-hidden="true">
{Array.from({ length }, (_, i) => (
<span
key={i}
ref={(d) => {
dots.current[i] = d;
}}
className="gu-trail__dot"
// The head is biggest; each dot after it is a little smaller and fainter.
style={{ "--k": 1 - i / length } as CSSProperties}
/>
))}
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Cursor trail
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A tail of dots that chases the pointer around an area. Each dot eases toward the dot in front of it, so quick moves stretch the tail out and stopping gathers it back into one point.
## Behavior
- One requestAnimationFrame loop that only runs while something is moving, and sleeps when all dots have caught up.
- Dot 0 eases toward the pointer; dot i eases toward dot i − 1, each by `follow` (default 0.42) of the remaining distance per frame. Positions are written straight to style.transform; React never re-renders while it runs.
- pointermove and pointerdown set the target (so mouse, pen and touch all work). On the first move, all dots jump to the pointer so the tail never flies in from a corner.
- pointerleave/pointercancel fade the dots out (0.4s).
## Look
- 16 dots by default. Size 6px + 22px × k and opacity 0.15 + 0.85 × k, where k goes from 1 at the head to near 0 at the tail.
- Color via the `color` prop (default orange #ff5a1f). The area clips the dots; pass `radius` to match your corners.
- Demo: an ink panel 400px tall with a crosshair cursor, a small hint and "Leave a mark." in 5rem type.
## Accessibility
- Purely decorative: the dots are aria-hidden and nothing depends on them.
- The area has touch-action: pan-y, so a vertical swipe still scrolls the page while a tap or a sideways drag draws the trail.
- prefers-reduced-motion: no lag and no tail; the dots stack on the pointer, so a single dot follows it.
## Props
children, length (number of dots), color, follow (0–1), radius, className.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### CursorTrail.tsx
```tsx
"use client";
import { useEffect, useRef, type CSSProperties, type ReactNode } from "react";
import "./cursor-trail.css";
type CursorTrailProps = {
children?: ReactNode;
/** How many dots make up the tail. */
length?: number;
/** Any CSS color. */
color?: string;
/** 0 to 1. Lower is lazier and longer. */
follow?: number;
className?: string;
/** Match your content's corner radius so the dots are clipped the same way. */
radius?: number | string;
};
/**
* CursorTrail
* A tail of dots that chases the pointer around an area. Each dot eases
* toward the one in front of it, so fast moves stretch the tail and stops
* gather it back into one point. The loop sleeps when nothing moves.
*/
export function CursorTrail({
children,
length = 16,
color = "#ff5a1f",
follow = 0.42,
className = "",
radius = 0,
}: CursorTrailProps) {
const area = useRef<HTMLDivElement>(null);
const dots = useRef<(HTMLSpanElement | null)[]>([]);
useEffect(() => {
const el = area.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const pts = Array.from({ length }, () => ({ x: 0, y: 0 }));
const target = { x: 0, y: 0 };
let frame = 0;
let started = false;
const draw = () => {
frame = 0;
let moving = false;
pts.forEach((p, i) => {
const lead = i === 0 ? target : pts[i - 1];
// Reduced motion: every dot sits on the pointer, so only one shows.
const k = reduce ? 1 : follow;
const dx = lead.x - p.x;
const dy = lead.y - p.y;
p.x += dx * k;
p.y += dy * k;
if (Math.abs(dx) + Math.abs(dy) > 0.1) moving = true;
const dot = dots.current[i];
if (dot) dot.style.transform = `translate(${p.x}px, ${p.y}px)`;
});
if (moving) frame = requestAnimationFrame(draw);
};
const onMove = (e: PointerEvent) => {
const r = el.getBoundingClientRect();
target.x = e.clientX - r.left;
target.y = e.clientY - r.top;
if (!started) {
// First touch: start the whole tail on the pointer, not in a corner.
pts.forEach((p) => Object.assign(p, target));
started = true;
}
el.dataset.active = "true";
if (!frame) frame = requestAnimationFrame(draw);
};
const onLeave = () => {
el.dataset.active = "false";
started = false;
};
el.addEventListener("pointermove", onMove);
el.addEventListener("pointerdown", onMove);
el.addEventListener("pointerleave", onLeave);
el.addEventListener("pointercancel", onLeave);
return () => {
el.removeEventListener("pointermove", onMove);
el.removeEventListener("pointerdown", onMove);
el.removeEventListener("pointerleave", onLeave);
el.removeEventListener("pointercancel", onLeave);
cancelAnimationFrame(frame);
};
}, [length, follow]);
return (
<div
ref={area}
className={`gu-trail ${className}`}
data-active="false"
style={
{ "--gu-trail-color": color, borderRadius: radius } as CSSProperties
}
>
{children}
<div className="gu-trail__dots" aria-hidden="true">
{Array.from({ length }, (_, i) => (
<span
key={i}
ref={(d) => {
dots.current[i] = d;
}}
className="gu-trail__dot"
// The head is biggest; each dot after it is a little smaller and fainter.
style={{ "--k": 1 - i / length } as CSSProperties}
/>
))}
</div>
</div>
);
}
```
### cursor-trail.css
```css
.gu-trail {
position: relative;
overflow: hidden;
/* Vertical swipes still scroll the page; taps and sideways drags draw. */
touch-action: pan-y;
}
.gu-trail__dots {
position: absolute;
inset: 0;
pointer-events: none;
opacity: 0;
transition: opacity 0.4s ease;
}
.gu-trail[data-active="true"] .gu-trail__dots {
opacity: 1;
}
.gu-trail__dot {
--size: calc(6px + 22px * var(--k));
position: absolute;
left: calc(var(--size) / -2);
top: calc(var(--size) / -2);
width: var(--size);
height: var(--size);
border-radius: 50%;
background: var(--gu-trail-color);
opacity: calc(0.15 + 0.85 * var(--k));
will-change: transform;
}
/* Reduced motion: the dots stack on the pointer, so a single dot follows it. */
@media (prefers-reduced-motion: reduce) {
.gu-trail__dots {
transition: none;
}
}
```
You might also want
Next piece: Curtain page loaderDot ripple field
Animations, intermediate
Letter scatter headline
Animations, beginner
Spotlight reveal
Animations, intermediate