I’m trying to create a following light animation with jquery and mouse position events – I am not sure on the formula to use or how to stabilize this type of effect looking for help/improved code attempts.
If I evaluate the size of the box and do math from the center points of the square to calculate the x,y percentages?
https://jsfiddle.net/x40ynof2/7/
$( document ).ready(function() {
var myElement = $("#p1");
function setTranslate(xPos, yPos, zPos, el) {
console.log("test");
el.style.transform = `translate3d(${xPos}, ${yPos}, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg)`;
}
myElement.mousemove(function(event) {
console.log("mousemoveY---", event.clientX, event.clientY);
let y = event.clientY;
let x = event.clientX;
var position = {
top: myElement[0].offsetTop,
left: myElement[0].offsetLeft
};
console.log("x", x);
console.log("y", y);
console.log("position", position);
let degx = position.left - x;
let degy = position.top - y;
console.log("degx", degx);
console.log("degy", degy);
setTranslate(-(degx / 20)+"%" , -(degy / 20)+"%", 0, myElement[0]);
});
myElement.mouseleave(function() {
console.log("mouseleave");
setTranslate(0, 0, 0, myElement[0]);
});
});
.container {
padding: 40px;
}
.blockwrapper {
will-change: transform;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg);
margin:10px;
}
.block {
background-color: skyblue;
width: 160px;
height: 160px;
}
.moved {
background-color: pink;
}
.container {
padding: 40px;
}
.blockwrapper {
will-change: transform;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg);
margin:10px;
}
.block {
background-color: #333333;
width: 260px;
height: 260px;
}
.moved {
background-color: pink;
}
.light {
width: 100%;
height: 100%;
opacity: 1;
border-radius: 110px;
justify-content: center;
align-items: center;
display: flex;
position: absolute;
will-change: transform;
transform: translate3d(0.01%, 0.01%, 0px) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg);
transform-style: preserve-3d;
}
.light-wrapper-in-front {
z-index: 6;
width: 100%;
height: 100%;
opacity: 1;
justify-content: center;
align-items: center;
display: none;
position: absolute;
}
.light-wrapper-in-front{
position:relative;
display: flex;
}
.light-outside {
width: 200px;
height: 200px;
opacity: .3;
filter: blur(70px);
background-color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="container">
<div class="blockwrapper">
<div class="block">
Movable
<div id="p1" class="light-wrapper-in-front">
<div class="light">
<div class="light-outside"></div>
</div>
</div>
</div>
</div>
</div>

