What is the best way to ensure that my threejs mesh only tracks my mouse movements along the x-axis?

Trying to create a threejs mesh that responds to mouse movement by moving from side to side, but encountering an issue when changing directions.

Below is the code snippet for the mousemove function:

The problem arises when switching directions after initial movement, causing the mesh to only start moving in the opposite direction once it nears the original x position of the mouse click.

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);
}

Answer №1

It seems like the desired effect you are aiming for is a bit unclear, so I'll make an educated guess... If you provide a more precise description in your answer, feel free to update me and I will adjust accordingly.

The main issue with your current approach lies in the reliance on MouseEvent.pageX. This property's coordinate system starts at 0 on the left side and increases towards the right. Consequently, whenever you move your cursor to the right from the initial click point, the MouseEvent.pageX value will always be greater than the original one, regardless of the actual direction of movement. Similarly, only if your cursor is positioned to the left of the click point will the pageX be less than the initial value.

I believe the property you may be seeking is actually MouseEvent.movementX. This property specifically indicates the change in position since the last recorded MouseEvent, taking into consideration the direction of movement as well – positive for rightward movement and negative for leftward movement.

Consider substituting pageX with movementX to see if it brings you closer to your intended outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some of the potential drawbacks of utilizing the same template ref values repeatedly in Vue 3?

Is it problematic to have duplicate template ref values, such as ref="foo", for the same type but different instances of a component across views in a Vue 3 application? Let's consider the following example pseudocode: // ROUTE A <te ...

Rejuvenating your HTML content with AJAX over time

My HTML page contains links to charts that refresh every time the page is reloaded. A friend mentioned that AJAX can automatically refresh the chart at specified intervals without reloading the entire HTML page. I would appreciate any help with the HTML ...

Node.js client encounters ENOBUFS error due to excessive number of HTTP requests

Currently, I have the following setup: An end-to-end requests system where a node.js client communicates with a node.js server. However, the issue arises when the client fails with an ENOBUFS error in less than a minute. client: (function(){ var lo ...

Clearing the JSON data from the file object

I am encountering an issue that requires me to pass some information along with a file to a controller using an Ajax call. The model structure is as follows: public class PersonModel { [Display(Name ="Full Name")] public string Name { ...

Issue: User is being redirected from "/login" to "/home" due to a navigation guard. This problem arises in the interaction between Vue-router and Firebase authentication

I'm currently working on an app that utilizes Vue.js and Firebase for authentication. I am trying to implement a verification process using the beforeEach method in my router file to ensure that users can only sign in if their displayName matches a sp ...

organize the values based on their priority using reactjs

I'm facing a challenge involving two arrays of objects: array1 and array2. I need to display only three values from both arrays, with priority given to array1. The requirements are as follows: if array1 contains 3 elements, all three should be shown. ...

Whenever my code is used within Google Sites, it triggers the opening of a new and empty tab

When using this code inside an HTML box in Google Sites, a problem arises. It seems to work perfectly fine in Internet Explorer and Chrome when saved to an HTML file. However, within Google Sites, it unexpectedly opens a new tab with no data. The code st ...

JavaScript code for sending information to the server

I am attempting to write data to a file on the server when a user clicks on a button, using JavaScript. I followed the method outlined in this Stack Overflow post: Saving a text file on server using JavaScript. Unfortunately, it did not work as expected. ...

Warning: Potential Infinite Loop when using Vue JS Category Filter

I developed a program that filters events based on their program and type. The program is working correctly, however, an error message stating "You may have an infinite update loop in a component render function" keeps popping up. I suspect that the issue ...

The error message "TypeError: Router.use() expects a middleware function, but received a [type of function]" is a common occurrence in FeathersJS

Struggling to bind a method to my /userAuthenticationInfo route, I've made several adjustments in my code based on other posts related to this issue but still unable to make it work. I am using feathersJS's express implementation, and even with e ...

What is the best way to change CSS style for a button when clicked using JavaScript?

Is there a way to switch the background style of a CSS div when clicking the same button? function changeBg() { var divElem = document.getElementById("change-bg"); if (divElem.style.backgroundColor === "yellow") { divElem.style.backgroundColor ...

Every time I refresh a Next.js page, I find myself back on the index.js

I find it confusing that whenever I refresh a page in my Next.js app, it always redirects me back to the index page. My e-commerce single-page application (SPA) has a catalogue page in index.js and the products are displayed using the dynamic [name].js pag ...

Strategies for resolving the error message "undefined" following the mapping process

Currently, I am utilizing a custom API to retrieve data. In order to accomplish this, I have structured an object that holds all the necessary data to be retrieved in a dynamic manner. My approach involves using the fetch() function to perform these data r ...

A Javascript/JQuery event that prevents Chrome from loading insecure content

When a page is loaded over HTTPS but the resources are served from an HTTP website, Chrome will display an "insecure content" warning. Is there a way to detect when Chrome has blocked insecure content and if the user has allowed it? There is a shield icon ...

Leveraging the power of Javascript/jQuery to manipulate form

On my website, I have implemented a form that requires a customized response for certain zip codes. To achieve this, I am developing a code that validates the first 3 digits of the entered zip code against a predefined array in my system. Although the code ...

A guide on instantly updating displayed flat/section list elements in React Native

I am in the process of creating a screen called ContactListScreen. The direct child of ContactListScreen is ContactItems, which is a sectionList responsible for rendering each individual ContactItem. However, I have encountered a problem where my ContactIt ...

Dependency management in ReactJS components

I am grappling with the optimal structure for a React component that is composed of other components. Let's look at the first example: <ColorSelect id="color" label={this.state.selectLabel} trigger={{ color: "lime", text: "Lime"}} onPropagateCli ...

Setting up Webpack with a Node.js backend

After successfully creating a structured outline for my React App, I have now uploaded the code to Github for easy access: https://github.com/KingOfCramers/React-App-Boilerplate. To launch this React server using Node and Express, I found a helpful guide ...

Locating the position of multiple repeated words

My question involves a complex laTeX string: let result = "\\frac{x}{2}+\\frac{3}{x}"; The task at hand is to locate the instances of "frac" in the string, store their positions in an array, find the first '}&a ...

Strange sequence of results coming from Vue.js

methods: { ShowWindow: function(QueryID) { this.$data.ID = QueryID; if(this.GetData()) { console.log("asdasd") } document.querySelector("#EditWindow").style.visibility = "visi ...