1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

mousemove element fixes

This commit is contained in:
Sander Moolin 2021-03-25 10:06:22 -04:00
parent 136ec03c07
commit 09d5248f24
2 changed files with 50 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import { bindProp } from '../tools/index.js';
export default { export default {
name: 'Object3D', name: 'Object3D',
inject: ['three', 'scene', 'rendererComponent'], inject: ['three', 'scene', 'rendererComponent'],
emits: ['created', 'ready'], emits: ['created', 'ready', 'pointerEnter', 'pointerOver', 'pointerLeave'],
props: { props: {
position: { type: Object, default: { x: 0, y: 0, z: 0 } }, position: { type: Object, default: { x: 0, y: 0, z: 0 } },
rotation: { type: Object, default: { x: 0, y: 0, z: 0 } }, rotation: { type: Object, default: { x: 0, y: 0, z: 0 } },
@ -12,11 +12,12 @@ export default {
lookAt: { type: Object, default: null }, lookAt: { type: Object, default: null },
onPointerEnter: { type: Function, default: null }, onPointerEnter: { type: Function, default: null },
onPointerOver: { type: Function, default: null }, onPointerOver: { type: Function, default: null },
onPointerLeave: { type: Function, default: null } onPointerLeave: { type: Function, default: null },
usePointerEvents: { type: Boolean, default: false }
}, },
data(){ data() {
return { return {
pointerIsOver: null pointerOver: null
} }
}, },
// can't use setup because it will not be used in sub components // can't use setup because it will not be used in sub components
@ -60,7 +61,44 @@ export default {
this.three.raycaster.setFromCamera(this.three.mouse, this.three.camera) this.three.raycaster.setFromCamera(this.three.mouse, this.three.camera)
const intersects = this.three.raycaster.intersectObjects([this.o3d]) const intersects = this.three.raycaster.intersectObjects([this.o3d])
if (intersects.length) { if (intersects.length) {
this.onPointerEnter(intersects[0]) // pass single intersection if we only have one, for convenience
const toPass = intersects.length === 1 ? intersects[0] : intersects;
// pointer is newly over o3d
if (!this.pointerOver) {
this.pointerOver = true;
if (this.onPointerEnter) {
this.onPointerEnter({ object: this.o3d, intersects: toPass });
if (this.usePointerEvents) {
this.$emit('pointerEnter', toPass);
}
}
}
// pointer is still over o3d
else if (this.onPointerOver) {
this.onPointerOver({ object: this.o3d, intersects: toPass });
if (this.usePointerEvents) {
this.$emit('pointerOver', toPass);
}
}
} else {
// pointer is not over o3d
// pointer has just left o3d
if (this.pointerOver) {
this.pointerOver = false;
if (this.onPointerLeave) {
this.onPointerLeave({ object: this.o3d });
}
if (this.usePointerEvents) {
this.$emit('pointerLeave');
}
}
} }
} }
}, },

View File

@ -114,9 +114,11 @@ export default function useThree() {
// conf.mouse_move = conf.mouse_move || conf.mouse_over; // conf.mouse_move = conf.mouse_move || conf.mouse_over;
if (conf.use_pointer) { if (conf.use_pointer) {
if (conf.use_pointer === true) { if (conf.use_pointer === true) {
obj.mouse_move_element = document.body; // use renderer as mousemove by default
} else {
obj.mouse_move_element = obj.renderer.domElement; obj.mouse_move_element = obj.renderer.domElement;
} else {
// use custom element as mousemove element
obj.mouse_move_element = conf.use_pointer;
} }
obj.mouse_move_element.addEventListener('mousemove', onMousemove); obj.mouse_move_element.addEventListener('mousemove', onMousemove);
obj.mouse_move_element.addEventListener('mouseleave', onMouseleave); obj.mouse_move_element.addEventListener('mouseleave', onMouseleave);
@ -223,9 +225,9 @@ export default function useThree() {
/** /**
*/ */
function updateMouse(e) { function updateMouse(e) {
const rect = e.target.getBoundingClientRect(); const rect = obj.mouse_move_element.getBoundingClientRect();
mouse.x = ((e.clientX - rect.left) / size.width) * 2 - 1; mouse.x = ((e.x - rect.left) / size.width) * 2 - 1;
mouse.y = -((e.clientY - rect.top) / size.height) * 2 + 1; mouse.y = -((e.y - rect.top) / size.height) * 2 + 1;
} }
/** /**