Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
How do I make sure my threejs mesh follows my mouse on the x only?

I have a threejs mesh that I want to move from side to side when I move my mouse after clicking it.

On mousemove function here is my code.

The problem I'm running into is if I first move to the right and then I switch directions it doesn't start moving to the left until I get closer to my original x position where I clicked my mouse for the first time.

let originx;
onDocumentClick(evt) {
  originx = evt.pageX;
  document.addEventListener('mousemove', onMouseMove);
}

onMouseMove(evt) {
let _newX = myMesh.position.x + (originx - evt.pageX);
myMesh.position.set(_newX, myMesh.position.y, myMesh.position.z);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's not quite clear the effect you're trying to achieve, so I'm going to take a stab in the dark... If you update your answer with a more precise description, let me know and I will update this answer.

I think the biggest problem with your logic is that you are basing everything on MouseEvent.pageX. This property's coordinate system starts at 0 on the left edge, and has a positive value on the right edge. As such, when you move your mouse anywhere to the right of your original click point, you will always have a MouseEvent.pageX that is larger than the MouseEvent.pageX of the original point, regardless of the direction in which you are moving your mouse. Likewise, pageX will only be smaller than the original click point's pageX value while your cursor is to the left of that click point.

I think the property you're actually looking for is MouseEvent.movementX. This property describes only the change in position since the last MouseEvent. It also takes direction into account, so a movement to the right will produce a positive value, and a movement to the left will produce a negative value.

Try replacing pageX with movementX and see if that gets you closer to what you want.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda