Web Animations Medium
Mouse Trail Particles
Canvas particles that spawn at the cursor position and fade out, creating a glowing trail effect. Pure vanilla JS.
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
mousemovepushes newParticleobjects into an array with position, velocity, size, opacity, and a huerequestAnimationFrameloop updates each particle: drift by velocity, reduce size and opacity- Dead particles (opacity โค 0) are removed from the array
- 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 devicePixelRatioscaling for sharp rendering on HiDPI screens