Cursor · 02 of 22

Blend Mode

Cursor inverts background colors

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="blend-area" id="blendArea">
  <h2>Difference</h2>
  <div class="blend-cursor" id="blendCursor"></div>
</div>

CSS

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

/* White dot + difference blending inverts whatever is underneath */
.blend-cursor {
  position: absolute;
  top: 0; left: 0;
  width: 30px; height: 30px;
  background: #fff;
  border-radius: 50%;
  pointer-events: none;
  mix-blend-mode: difference;
  transform: translate(-50%, -50%);
  opacity: 0;
}

JavaScript

var area = document.getElementById('blendArea');
var dot = document.getElementById('blendCursor');

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