Cursor Trail Fade
Move a pointer or finger across the box and a trail fades behind it, as dots or a comet line.
AI prompt
Abstract motion graphic of a glowing comet trail drawn by a moving cursor across a dark navy canvas, a bright white head with a long tapering tail that fades through cyan to transparent, smooth looping figure-eight path, soft glow, a few faint dots left behind along the curve, minimal and elegant, high contrast. Variants: a trail of separate shrinking dots and a short thin ribbon. Negative: clutter, hard edges, text, logos, noise. Reduced motion: single still frame of the trail.About this pattern
Definition
A cursor trail records recent pointer positions and draws them with fading size or opacity, so motion leaves a short-lived path. It gives direction and speed at a glance and adds playfulness, but it must stay light and never block the content.
Purpose & UX principles
- Store points with timestamps and draw only those younger than the tail length.
- Fade by age: older points smaller and more transparent.
- Use pointer events so mouse, pen and touch work the same; set touch-action: none on the area.
- Keep it short, around 300 to 600 milliseconds.
Interaction states
- Two style buttons, a Tail slider and an automatic demo path until you move.
Motion specification
- Points live for the chosen tail time and fade smoothly.
- Reduced motion: the trail is turned off and a message is shown.
Responsive behaviour
- Do not use trails on text-heavy pages; they distract and cost performance.
- Respect reduced motion by turning the trail off.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Style"><button data-p="0" aria-pressed="true">Comet</button><button data-p="1" aria-pressed="false">Dots</button></div><canvas id="cv" width="330" height="130" aria-label="Pointer trail area"></canvas><label class="rw">Tail <b id="tv">450 ms</b><input id="tl" type="range" min="150" max="900" value="450"></label><p class="out" id="out"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
canvas{width:100%;height:130px;border-radius:12px;background:#0f1730;touch-action:none;cursor:crosshair}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const cv=document.getElementById('cv'),x=cv.getContext('2d'),tl=document.getElementById('tl'),bs=[...document.querySelectorAll('#pre button')],calm=matchMedia('(prefers-reduced-motion:reduce)').matches;let p=0,pts=[],user=false,t0=0;
function add(px,py){pts.push([px,py,performance.now()]);}
cv.addEventListener('pointermove',e=>{user=true;const r=cv.getBoundingClientRect();add((e.clientX-r.left)*cv.width/r.width,(e.clientY-r.top)*cv.height/r.height);});
function draw(now){const T=+tl.value;pts=pts.filter(q=>now-q[2]<T);x.clearRect(0,0,cv.width,cv.height);
if(p===0){for(let i=1;i<pts.length;i++){const a=1-(now-pts[i][2])/T;x.strokeStyle='rgba(120,220,255,'+a.toFixed(2)+')';x.lineWidth=1+a*6;x.lineCap='round';x.beginPath();x.moveTo(pts[i-1][0],pts[i-1][1]);x.lineTo(pts[i][0],pts[i][1]);x.stroke();}}
else pts.forEach(q=>{const a=1-(now-q[2])/T;x.fillStyle='rgba(255,255,255,'+(a*.9).toFixed(2)+')';x.beginPath();x.arc(q[0],q[1],1+a*6,0,7);x.fill();});
if(pts.length){const h=pts[pts.length-1];x.fillStyle='#fff';x.beginPath();x.arc(h[0],h[1],4,0,7);x.fill();}}
function loop(now){if(!user){const t=(now-t0)/1000;add(165+Math.sin(t*1.6)*120,65+Math.sin(t*3.2)*42);}draw(now);requestAnimationFrame(loop);}
function up(){document.getElementById('tv').textContent=tl.value+' ms';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));tl.addEventListener('input',up);up();
if(calm){document.getElementById('out').textContent='Reduced motion is on, so the trail is disabled.';for(let i=0;i<40;i++){const t=i/40*3;pts.push([165+Math.sin(t*1.6)*120,65+Math.sin(t*3.2)*42,performance.now()]);}tl.disabled=true;x.strokeStyle='rgba(120,220,255,.7)';x.lineWidth=3;x.beginPath();pts.forEach((q,i)=>i?x.lineTo(q[0],q[1]):x.moveTo(q[0],q[1]));x.stroke();}else{t0=performance.now();document.getElementById('out').textContent='Move over the box to draw your own trail.';requestAnimationFrame(loop);}One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Cursor Trail Fade — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
canvas{width:100%;height:130px;border-radius:12px;background:#0f1730;touch-action:none;cursor:crosshair}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Style"><button data-p="0" aria-pressed="true">Comet</button><button data-p="1" aria-pressed="false">Dots</button></div><canvas id="cv" width="330" height="130" aria-label="Pointer trail area"></canvas><label class="rw">Tail <b id="tv">450 ms</b><input id="tl" type="range" min="150" max="900" value="450"></label><p class="out" id="out"></p></div>
</div>
<script>
const cv=document.getElementById('cv'),x=cv.getContext('2d'),tl=document.getElementById('tl'),bs=[...document.querySelectorAll('#pre button')],calm=matchMedia('(prefers-reduced-motion:reduce)').matches;let p=0,pts=[],user=false,t0=0;
function add(px,py){pts.push([px,py,performance.now()]);}
cv.addEventListener('pointermove',e=>{user=true;const r=cv.getBoundingClientRect();add((e.clientX-r.left)*cv.width/r.width,(e.clientY-r.top)*cv.height/r.height);});
function draw(now){const T=+tl.value;pts=pts.filter(q=>now-q[2]<T);x.clearRect(0,0,cv.width,cv.height);
if(p===0){for(let i=1;i<pts.length;i++){const a=1-(now-pts[i][2])/T;x.strokeStyle='rgba(120,220,255,'+a.toFixed(2)+')';x.lineWidth=1+a*6;x.lineCap='round';x.beginPath();x.moveTo(pts[i-1][0],pts[i-1][1]);x.lineTo(pts[i][0],pts[i][1]);x.stroke();}}
else pts.forEach(q=>{const a=1-(now-q[2])/T;x.fillStyle='rgba(255,255,255,'+(a*.9).toFixed(2)+')';x.beginPath();x.arc(q[0],q[1],1+a*6,0,7);x.fill();});
if(pts.length){const h=pts[pts.length-1];x.fillStyle='#fff';x.beginPath();x.arc(h[0],h[1],4,0,7);x.fill();}}
function loop(now){if(!user){const t=(now-t0)/1000;add(165+Math.sin(t*1.6)*120,65+Math.sin(t*3.2)*42);}draw(now);requestAnimationFrame(loop);}
function up(){document.getElementById('tv').textContent=tl.value+' ms';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));tl.addEventListener('input',up);up();
if(calm){document.getElementById('out').textContent='Reduced motion is on, so the trail is disabled.';for(let i=0;i<40;i++){const t=i/40*3;pts.push([165+Math.sin(t*1.6)*120,65+Math.sin(t*3.2)*42,performance.now()]);}tl.disabled=true;x.strokeStyle='rgba(120,220,255,.7)';x.lineWidth=3;x.beginPath();pts.forEach((q,i)=>i?x.lineTo(q[0],q[1]):x.moveTo(q[0],q[1]));x.stroke();}else{t0=performance.now();document.getElementById('out').textContent='Move over the box to draw your own trail.';requestAnimationFrame(loop);}
</script>
</body></html>