Cursor trails are effects that follow the mouse around a page: a dot that lags a little behind the pointer, a fading line, sparks, or a mask that reveals a second picture wherever you move. A good cursor trail makes a site feel like it notices you. A bad one gets in the way of reading and clicking. Below are two small, working versions you can paste into any site, a follower and a canvas trail, plus the checks that keep them off phones and away from people who've asked for less motion.
What a cursor trail is
A cursor trail is anything drawn at or behind the pointer's position as it moves. There are three common kinds:
- A follower: one element, such as a dot or ring, that eases towards the pointer, so it trails slightly behind.
- A drawn trail: a line or particles left along the path, fading out over a fraction of a second.
- A reveal: the pointer paints a mask, and a second image or effect shows through where it has been.
The real system cursor stays where it is in all three. You can also replace the cursor's image with the CSS cursor property, for example cursor: url(dot.svg) 8 8, auto;, but that only swaps the icon. It can't lag, fade or leave anything behind, which is why trails use an extra element or a canvas.
A simple cursor follower in CSS and JS
The follower is one fixed element that ignores clicks, moved with transform on every animation frame. The HTML:
<div class="cursor-dot" aria-hidden="true"></div>The CSS. pointer-events: none lets clicks pass through to the page underneath:
.cursor-dot {
position: fixed;
top: 0;
left: 0;
width: 14px;
height: 14px;
margin: -7px 0 0 -7px; /* centre the dot on the pointer */
border-radius: 50%;
background: #ff630f;
pointer-events: none;
z-index: 9999;
opacity: 0;
transition: opacity 0.2s;
}
.cursor-dot.is-visible {
opacity: 1;
}The JavaScript listens for pointermove and eases the dot towards the pointer inside requestAnimationFrame. The easing uses the time between frames, so the dot moves at the same speed on 60 Hz and 120 Hz screens, and the loop stops once the dot has caught up:
const dot = document.querySelector(".cursor-dot");
const finePointer = window.matchMedia("(hover: hover) and (pointer: fine)");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
if (dot && finePointer.matches && !reduceMotion.matches) {
let targetX = 0, targetY = 0; // where the pointer is
let x = 0, y = 0; // where the dot is
let last = 0;
let running = false;
window.addEventListener("pointermove", (e) => {
targetX = e.clientX;
targetY = e.clientY;
if (!dot.classList.contains("is-visible")) {
x = targetX; // first move: start at the pointer, not the corner
y = targetY;
dot.classList.add("is-visible");
}
if (!running) {
running = true;
last = performance.now();
requestAnimationFrame(follow);
}
});
// hide the dot when the pointer leaves the window
window.addEventListener("pointerout", (e) => {
if (!e.relatedTarget) dot.classList.remove("is-visible");
});
function follow(now) {
const dt = Math.min(now - last, 100);
last = now;
// close about 20% of the gap per 60 Hz frame, at any refresh rate
const k = 1 - Math.pow(0.8, dt / 16.7);
x += (targetX - x) * k;
y += (targetY - y) * k;
dot.style.transform = `translate(${x}px, ${y}px)`;
if (Math.abs(targetX - x) > 0.1 || Math.abs(targetY - y) > 0.1) {
requestAnimationFrame(follow);
} else {
running = false; // stop the loop when the dot has caught up
}
}
}Load the script as a module, <script type="module" src="cursor-dot.js"></script>, so its variables stay private and it can share a page with the trail below.
Change 0.8 to set the lag: closer to 1 trails further behind, closer to 0 sticks to the pointer. For a ring instead of a dot, give it a border and a transparent background.
A canvas cursor trail
For a line that fades out behind the pointer, draw on a full-screen canvas. Each pointer position is stored with a timestamp, and every frame draws the recent ones, thinner and more transparent as they age.
<canvas class="cursor-trail" aria-hidden="true"></canvas>.cursor-trail {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 9998;
}const canvas = document.querySelector(".cursor-trail");
const finePointer = window.matchMedia("(hover: hover) and (pointer: fine)");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
if (canvas && finePointer.matches && !reduceMotion.matches) {
const ctx = canvas.getContext("2d");
const LIFE = 450; // how long a point stays visible, in ms
const points = []; // { x, y, t }
let running = false;
function resize() {
const dpr = window.devicePixelRatio || 1;
canvas.width = Math.round(window.innerWidth * dpr);
canvas.height = Math.round(window.innerHeight * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // draw in CSS pixels
}
resize();
window.addEventListener("resize", resize);
window.addEventListener("pointermove", (e) => {
points.push({ x: e.clientX, y: e.clientY, t: performance.now() });
if (!running) {
running = true;
requestAnimationFrame(draw);
}
});
function draw(now) {
// forget points older than LIFE
while (points.length && now - points[0].t > LIFE) points.shift();
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
ctx.lineCap = "round";
for (let i = 1; i < points.length; i++) {
const a = points[i - 1];
const b = points[i];
const life = Math.max(0, Math.min(1, 1 - (now - b.t) / LIFE)); // 1 new, 0 gone
ctx.strokeStyle = `rgba(255, 99, 15, ${life})`;
ctx.lineWidth = 2 + 10 * life;
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.stroke();
}
if (points.length) {
requestAnimationFrame(draw);
} else {
running = false; // nothing left to draw: stop until the next move
}
}
}Two details matter here. The canvas is sized by devicePixelRatio, so the line stays sharp on high-density screens. And the loop only runs while there's something to draw, so an idle page does no work. Browsers also pause requestAnimationFrame in most background tabs.
From here, the same structure covers most trail styles: draw circles instead of segments for dots, add a random offset per point for sparks, or use globalCompositeOperation to make the trail reveal an image instead of drawing a colour.
Custom cursor trails from real templates
Custom cursor trails work best when the trail is part of the idea of the site, not decoration on top of it. These templates are examples, each checked on its page:
- keycap sphere (free): a site for an invented language lab, with a ring cursor that follows the pointer. In the last two sections the cursor becomes a small sphere of light with a smoky trail that lights and tilts 3D keys, and the keys under it turn to dark glass.
- easylog (free, with a Figma file): moss grows on a floating log wherever the cursor moves, then fades behind it. Its atoll version uses the same idea with coral, painted through a mask that fades over a few seconds. On touch screens, atoll simply shows the log fully covered.
- nora vale (paid): a photographer's portfolio where the cursor becomes a brush. A WebGL fluid simulation wipes the black off a looping clip, and the stroke slowly closes again. It includes touch brushing and an automatic demo stroke for visitors who don't move the mouse.
- molt (paid): a site for an invented serum, where the cursor is a droplet with a sticky trail.
The reveal pattern, where the pointer uncovers a second layer, is the same technique as interactive image relighting. If a trail is the only interesting thing on the page, it won't save it. We explain why in why every AI website looks the same.
Accessibility: touch, reduced motion
Both code samples above start with the same two checks, and they're the part not to skip.
- Touch screens have no hover. A trail only makes sense with a mouse or trackpad. The
pointerandhovermedia features tell you whether the main input is precise and can hover, andmatchMediareads them in JavaScript. On phones, either skip the effect or show its end state, as atoll does. - Respect reduced motion. People who turn on Reduce Motion in their system settings get
prefers-reduced-motion: reduce. Motion that follows the pointer is exactly what WCAG's animation from interactions guidance is about. Turn the trail off, or keep a static dot. - Never hide the real cursor. Hiding it with
cursor: noneand drawing your own means a lag or a script error leaves people unable to see where they're pointing. - Keep it out of the way.
pointer-events: noneon the trail, andaria-hidden="true"so screen readers skip it. Keep it subtle over text, and don't let it cover links.
If you want text to move as well, see CSS text animation for timing that keeps it readable.
FAQ
How do I make a cursor trail in CSS?
CSS alone can change the cursor's image with the cursor property, but it can't make it lag or leave a trail. For that you need a little JavaScript: an element or canvas that follows pointermove events.
Do cursor trails slow down a website?
A small trail like the ones above does little work, especially if the animation loop stops when the pointer is still. Heavy particle or WebGL trails cost more, so test them on a slower laptop.
Should a cursor trail work on mobile?
Usually not. Phones and tablets have no hovering pointer, so check (hover: hover) and (pointer: fine) and skip the effect, or show its finished state, on touch screens.
How do I turn off a cursor trail for reduced motion?
Check window.matchMedia("(prefers-reduced-motion: reduce)") before starting the effect, and don't start it if it matches.