21
loading...
This website collects cookies to deliver better user experience
.container {
width: 100vw;
height: 100vh;
background-color: white;
position: relative;
}
.circle {
position: absolute;
left: 0;
top: 0;
width: 60px;
height: 60px;
background: white;
border-radius: 50%;
pointer-events: none;
mix-blend-mode: difference;
}
Setting the background-color to white in both the .container and the .circle is needed for this to work. In this tutorial I’m just going to use white but it can be done with any color. It just gets the inverted colors and then css just gets the difference.
const cursor = document.querySelector(".circle")
function getDimensions(e) {
cursor.style.top = `${e.clientY - 25}px` // -25px for the size of the circle
cursor.style.left = `${e.clientX - 25}px`
}
window.addEventListener("mousemove", (e) => {
getDimensions(e)
});
const delay = 250
// ...
function throttle(callback, limit) {
let wait = false
return function () {
if (!wait) {
callback.apply(null, arguments)
wait = true
setTimeout(function () {
wait = false
}, limit)
}
}
}
window.addEventListener("mousemove", (e) => {
throttle(getDimensions(e), delay)
});