Moving an object in THREE.js relative to the screen while considering the rotation and scale of its parent(s)

While working in THREE.js, I've been experimenting with moving objects around on the screen based on mouse position. After exploring some sample codes, it appeared to be quite straightforward. Here's a snippet of code that I found useful from one of the examples:

function mouseMove(e){
    mouse2D.x = ( (e.pageX-canvas.offsetParent.offsetLeft) / canvas.clientWidth ) * 2 - 1;
    mouse2D.y = - ( (e.pageY-canvas.offsetParent.offsetTop) / canvas.clientHeight ) * 2 + 1;

    if (selected) {
      var ray = projector.pickingRay(mouse2D, PGL._camera).ray;
      var targetPos = ray.direction.clone().addSelf(ray.origin);
      targetPos.subSelf(_offset);

      obj.position.x = initPos.x + targetPos.x;
      obj.position.y = initPos.y + targetPos.y;
    }
  }

Although this works well for moving children within the scene directly, I encountered an issue when trying to move objects that have parent(s) which are rotated or scaled. How would you approach this challenge? Perhaps consider applying the object's matrixRotationWorld to the movement operation? I am still grappling with these concepts and any guidance would be highly appreciated.

Answer №1

Take a look at these helpful examples:

or

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

Prevent the Stop Function from being executed repeatedly while scrolling

I have implemented infinite scrolling in my react/redux app. As the user nears the bottom of the page, more contents are loaded dynamically. However, a challenge arises when the user scrolls too fast and triggers the function responsible for fetching cont ...

Retrieve the vertices of the animation

After loading an object with GLTFLoader and creating an animation with THREE.AnimationMixer, I discovered that I can extract the vertices of the object using .fromBufferGeometry().vertices on the Mesh geometry. However, I am now trying to figure out how ...

When using React-Leaflet, the pinch zoom feature causes markers to become misaligned with the map

In my React.js application with TypeScript, I am utilizing Leaflet for mapping purposes. Although the map functions smoothly on desktop browsers, I am encountering some issues when zooming and panning on mobile devices by pinching the screen. When I pinch ...

Create a selection menu in an HTML dropdown list using unique values from a JSON object that is separated by commas

I'm working with a JSON object that contains multiple key value pairs, one of which is the Languages key. This Languages key holds comma-separated values such as "English, Hindi, French," and so on. My goal is to extract distinct values from the Lang ...

Heroku - JavaScript and CSS Updates Causing Loading Issues

Ruby version: 2.1.5 | Rails version: 4.1.1 For some reason, the changes I make to my JavaScript and CSS files are not reflecting on Heroku; it seems like the cached assets are still being used. I've tried removing the assets and precompiling them bef ...

The setInterval function will run just one time when triggered by an onclick event

After watching a YouTube tutorial on creating desktop notifications, I had an idea to use it as a reminder tool. For example, having a specific reminder appear as a desktop notification every 30 minutes when triggered by clicking a button. However, the cod ...

Encountering an issue while attempting to install an SSL certificate on my server. The error message displayed is: "ERROR:

Unique HTML Code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="farmapp.css"> </head> <body onload=&qu ...

Guide on downloading a file from Firebase storage within a Vue-based web application

Scenario: You are in a situation where you need to download a PDF file (or any file type) from Firebase storage to your computer using a browser that has a Vue.js web app with Vuetify components. Despite having looked at the Firebase storage documentatio ...

`Inconsistencies in state management across components`

Creating a calendar with the ability to add notes for each day is my main goal. The structure of my components is organized as follows: App component serves as the common component that renders the main Build component. The Build component utilizes CellBui ...

Creating a file of a specific size using Node.JS WriteFile

Are there any techniques to designate the size or length of a file and ensure that the system allocates the necessary space? I am interested in something along the lines of: fs.write('file', Buffer, 1024*1024*54); // To generate a 54MB file. ...

Incorporate Angular frontend into current website or project

I've successfully built a website using bootstrap, but now I'm looking to integrate Angular in order to transform it into a single page application. The goal is that when a user clicks on a link, only the necessary content will be loaded from the ...

"Enhancing User Experience with JavaScript Double Click Feature in Three.js

Currently, I have implemented a double click function that allows the user to double click on a car model, displaying which objects have been intersected such as wipers, grille, and tires. These intersections are listed along with the number of items the d ...

Moving a div to a new spot

Looking to move a div - the expandable panel labeled 'Why Bother', to display at the bottom where it is currently seen. More specifically, I want it to be positioned at the top among the 8 panels in the HTML structure. (These panels are arranged ...

Ways to position bootstrap 4 navigation below header on desktop and above on mobile devices

In my bootstrap 4 grid layout, the first row consists of only a heading. The second row includes a vertical nav that I want to keep aligned with the content by placing it in the same row. However, on desktop only, I would like the heading to appear above t ...

What is the best method for deleting automatically added connection proxies in XCC?

Is there a way to make an ajax request from IBM Connections XCC without it being proxied? let api = 'https://my-server2/api.xml' var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = () => { if (xmlhttp.readyState == XMLHttpRe ...

Detecting characters in jQuery's onKeyPress with case sensitivity

Is there a way to detect characters in a "case-sensitive" manner using the onkeypress event in jQuery? ...

What is the reason why Prettier does not automatically format code in Visual Studio Code?

After installing and enabling ESLint and Prettier in my Nuxt application, I made the switch to Visual Studio Code. However, when I open a .vue file and use CMD+ Shift + P to select Format Document, my file remains unformatted. I even have the Prettier ex ...

Material-UI is having trouble resolving the module '@material-ui/core/styles/createMuiTheme'

Although I have searched for similar issues on StackOverflow, none of the solutions seem to work for me. The errors I am encountering are unique and so are the fixes required, hence I decided to create a new post. The company conducting my test provided m ...

Transferring a curve or path from Blender to Three.js for export

Having trouble exporting a bezier curve / nurbs curve to JSON from my simple scene. No matter what I try, nothing seems to export successfully. Exporting meshes is not an issue for me. I'm positive I have accomplished this before, but now I can' ...

Creating a fetcher that seamlessly functions on both the server and client within Nextjs 13 - the ultimate guide!

My Nextjs 13 frontend (app router) interacts with a Laravel-powered backend through an api. To handle authentication in the api, I am utilizing Laravel Sanctum as suggested by Laravel for SPAs. This involves setting two cookies (a session and a CSRF token) ...