Cursor · 05 of 22

Scale Interaction

Cursor grows when hovering targets

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="scale-area" id="scaleArea">
  <div class="scale-target" id="scaleTarget">Target</div>
  <div class="scale-dot" id="scaleDot"></div>
  <div class="scale-ring" id="scaleRing"></div>
</div>

CSS

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

.scale-target {
  padding: 1rem 2rem;
  border: 1px solid #7c5cff;
  color: #7c5cff;
  border-radius: 8px;
  cursor: none;
}

.scale-dot {
  position: absolute;
  top: 0; left: 0;
  width: 10px; height: 10px;
  background: #fff;
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  transition: transform 0.2s;
  opacity: 0;
}

.scale-ring {
  position: absolute;
  top: 0; left: 0;
  width: 40px; height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  transition: width 0.3s, height 0.3s, background 0.3s, border-color 0.3s;
  opacity: 0;
}

JavaScript

var area = document.getElementById('scaleArea');
var target = document.getElementById('scaleTarget');
var dot = document.getElementById('scaleDot');
var ring = document.getElementById('scaleRing');
var SMOOTH = 0.15; // ring lerp factor

var mouseX = 0, mouseY = 0;
var ringX = 0, ringY = 0;

area.addEventListener('mousemove', function (e) {
  var rect = area.getBoundingClientRect();
  mouseX = e.clientX - rect.left;
  mouseY = e.clientY - rect.top;
  dot.style.left = mouseX + 'px';
  dot.style.top = mouseY + 'px';
});
area.addEventListener('mouseenter', function () {
  dot.style.opacity = '1';
  ring.style.opacity = '1';
});
area.addEventListener('mouseleave', function () {
  dot.style.opacity = '0';
  ring.style.opacity = '0';
});

// Grow the ring and hide the dot while hovering the target
target.addEventListener('mouseenter', function () {
  dot.style.transform = 'translate(-50%, -50%) scale(0)';
  ring.style.background = 'rgba(124, 92, 255, 0.2)';
  ring.style.width = '80px';
  ring.style.height = '80px';
  ring.style.borderColor = '#7c5cff';
});
target.addEventListener('mouseleave', function () {
  dot.style.transform = 'translate(-50%, -50%) scale(1)';
  ring.style.background = 'transparent';
  ring.style.width = '40px';
  ring.style.height = '40px';
  ring.style.borderColor = 'rgba(255, 255, 255, 0.5)';
});

function tick() {
  ringX += (mouseX - ringX) * SMOOTH;
  ringY += (mouseY - ringY) * SMOOTH;
  ring.style.left = ringX + 'px';
  ring.style.top = ringY + 'px';
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);