From 3f166776cad47ae8d3a1576d3d5cf73d69cf9788 Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Sat, 27 Feb 2021 23:06:09 +0100 Subject: [PATCH] fix mouse position --- src/core/useThree.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/useThree.js b/src/core/useThree.js index eceb8cc..9cd7f04 100644 --- a/src/core/useThree.js +++ b/src/core/useThree.js @@ -216,12 +216,19 @@ export default function useThree() { this.renderer.dispose(); } + /** + */ + function updateMouse(e) { + const rect = e.target.getBoundingClientRect(); + mouse.x = ((e.clientX - rect.left) / size.width) * 2 - 1; + mouse.y = -((e.clientY - rect.top) / size.height) * 2 + 1; + } + /** * click listener */ function onClick(e) { - mouse.x = (e.clientX / size.width) * 2 - 1; - mouse.y = -(e.clientY / size.height) * 2 + 1; + updateMouse(e); raycaster.setFromCamera(mouse, obj.camera); const objects = raycaster.intersectObjects(intersectObjects); for (let i = 0; i < objects.length; i++) { @@ -234,8 +241,7 @@ export default function useThree() { * mousemove listener */ function onMousemove(e) { - mouse.x = (e.clientX / size.width) * 2 - 1; - mouse.y = -(e.clientY / size.height) * 2 + 1; + updateMouse(e); onMousechange(e); }