Cursor · 04 of 22

Cursor Trail

Dot trail follows movement

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="trail-area" id="trailArea">
  <span>Move Fast</span>
</div>

CSS

.trail-area {
  position: relative;
  height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #12121a;
  color: #6b6b80;
  overflow: hidden;
  font-family: sans-serif;
}

.trail-dot {
  position: absolute;
  width: 6px; height: 6px;
  background: #7c5cff;
  border-radius: 50%;
  pointer-events: none;
}

JavaScript

var area = document.getElementById('trailArea');
var DENSITY = 0.5;  // chance to spawn a dot per mousemove event
var LIFE = 800;     // ms

area.addEventListener('mousemove', function (e) {
  if (Math.random() > DENSITY) return; // limit density
  var rect = area.getBoundingClientRect();
  var dot = document.createElement('div');
  dot.className = 'trail-dot';
  dot.style.left = (e.clientX - rect.left) + 'px';
  dot.style.top = (e.clientY - rect.top) + 'px';
  area.appendChild(dot);

  // Animate out. The translate(-50%,-50%) must live in the keyframes — a base
  // transform would be replaced by the animation, leaving each dot offset by
  // half its size (down-right of the pointer) for its whole life.
  dot.animate([
    { transform: 'translate(-50%, -50%) scale(1)', opacity: 1 },
    { transform: 'translate(-50%, -50%) scale(0)', opacity: 0 }
  ], { duration: LIFE, easing: 'ease-out' }).onfinish = function () {
    dot.remove();
  };
});