โœฆ StealThis .dev

Mouse Trail Particles

Canvas particles that spawn at the cursor position and fade out, creating a glowing trail effect. Pure vanilla JS.

Open in Lab
canvas vanilla-js
Targets: JS HTML

Code

Mouse Trail Particles

A Canvas 2D particle system that spawns glowing particles at the cursor position. Each particle drifts, shrinks, and fades out โ€” leaving a luminous comet-like trail.

How it works

  1. mousemove pushes new Particle objects into an array with position, velocity, size, opacity, and a hue
  2. requestAnimationFrame loop updates each particle: drift by velocity, reduce size and opacity
  3. Dead particles (opacity โ‰ค 0) are removed from the array
  4. The canvas is cleared with a semi-transparent fill each frame โ€” creates the fade trail
ctx.fillStyle = "rgba(0,0,0,0.15)"; // semi-clear โ†’ trail persists briefly
ctx.fillRect(0, 0, W, H);

Performance

  • Particles are plain objects โ€” no DOM, no CSS, no reflows
  • Array .splice() removes dead particles in-place
  • devicePixelRatio scaling for sharp rendering on HiDPI screens