The given formula arranges the contents of the container according to the mouse movement.. the problem was in the size of the image, and in the fact that approaching along the x and y axes from different sides, the arrangement was calculated differently.
const zoomEffect = (event: any) => {
setZoomMode(true);
const xa = event.pageX-containerRef.current.offsetLeft;
const xb = containerRef.current.offsetWidth;
const xc = zoomRef.current?.getBoundingClientRect().width;
const ya = event.pageY-containerRef.current.offsetTop;
const yb = containerRef.current.offsetHeight;
const yc = zoomRef.current?.getBoundingClientRect().height;
setMouseCoords({
x: xc ? -(xa)/(xb/((xc)-xb)) : 0,
y: yc ? -(ya)/(yb/((yc)-yb)) : 0,
});
};
<div ref={conteinerRef} onMouseMove={zoomEffect}>
<div style={{
left: mouseCoords.x,
top: mouseCoords.y,
}}>
<Img ref_={zoomRef} />
</div>}
</div>
I don't know about the correctness of the formula, that's up to you, but I find it very confusing and convoluted with all these 2 letter variables.
I'd write it like this:
The transforms, how I got from your form to mine.
((xc)-xb)===(xc-xb)a/(b/c)===a*c/b-(xa)andxa === a-b->-xa === b-aor(containerRef.current.offsetLeft - event.pageX)