Typography · 17 of 33

Shadow Chase

Drop shadow stretches toward your cursor

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="shadow-chase-wrapper">
  <p class="shadow-chase-headline" id="shadowChaseHeadline">Shadow</p>
</div>

CSS

.shadow-chase-wrapper {
  display: flex;
  justify-content: center;
  padding: 3rem;
}

.shadow-chase-headline {
  font-size: 3.5rem;
  font-weight: 600;
  text-shadow: 0 4px 12px rgba(124, 92, 255, 0.4);
  will-change: text-shadow;
}

JavaScript

var headline = document.getElementById('shadowChaseHeadline');
var wrapper = document.querySelector('.shadow-chase-wrapper');

wrapper.addEventListener('mousemove', function (e) {
  var rect = wrapper.getBoundingClientRect();
  var dx = (e.clientX - (rect.left + rect.width / 2)) / (rect.width / 2);
  var dy = (e.clientY - (rect.top + rect.height / 2)) / (rect.height / 2);
  var len = 24 + Math.sqrt(dx * dx + dy * dy) * 16;
  var angle = Math.atan2(dy, dx);
  headline.style.textShadow =
    (Math.cos(angle) * len) + 'px ' + (Math.sin(angle) * len) +
    'px 20px rgba(124, 92, 255, 0.5)';
});