Guide on ensuring a THREE JS model maintains constant orientation towards cursor pointer

I'm working on a 3D model that currently follows the mouse only when clicked. However, I am looking to make it move without requiring the click so that it always faces the cursor pointer. The project is built using THREE.JS. Is there a way to achieve this through orbital control or do I need to include additional JavaScript for that functionality?

Answer №1

I recently implemented a similar concept during a student's lesson. See below for an example:

For the three.js code repository, visit:

https://github.com/soupIsTheCurrencyOfTheFuture/tweeter/blob/main/public/scripts/profile-icon.js

window.addEventListener('mousemove', (event) => {
  modelGroup.rotation.y = (event.clientX / window.innerWidth) - 0.5;
  modelGroup.rotation.x = (event.clientY / window.innerHeight) - 0.5;

  modelGroup.position.x = ((event.clientX / window.innerWidth) - 0.5) * 15;
  modelGroup.position.y = ((event.clientY / window.innerHeight) - 0.5) * -15;
});

Make sure to update your current onclick event listener to a mousemove event listener for this effect.

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

When we find ourselves within a fat arrow function

I'm struggling with this code. I have a ternary operator within a fat arrow function, but for some reason it's not working. There are no errors in browserify or the console, but the headers are not being printed. If I remove the {} and ternary o ...

Utilize jQuery to select the parent checkbox and mark it as checked

Currently, I am working on a JavaScript function that will automatically check the parent checkbox if a child checkbox is checked. I am implementing this using jQuery and here is my HTML code: <ul id="ulParent" style="margin: 0; padding: 0; list- ...

While creating a customer profile sample, I encountered an issue where I received an error message stating "POST http://localhost:3000/cusprofiles/add 400 (Bad Request)" when attempting to add data

I'm having trouble creating a customer profile for a vet clinic in my code. I'm unable to add and edit customer details, and I'm not sure where I've made a mistake. The only method that seems to be working is the GET method. Can someone ...

The authentication method "discord" is not recognized

Currently, I am working on implementing discord authentication using passport. Originally, everything was functioning correctly, but now it seems to have encountered an issue which I cannot identify. auth.js const express = require('express') co ...

What steps can I take to ensure that the upper and left sections of a modal dialog remain accessible even when the size is reduced to the point of overflow?

One issue I'm facing is with a fixed-size modal dialog where part of the content gets cut off and becomes inaccessible when the window shrinks to cause an overflow. More specifically, when the window is narrowed horizontally, the left side is cut off ...

Delete Refresh Support Jquery

I am attempting to have the div recentactivity only refresh when the Remove button is clicked < a href='#' onclick=\"javascript:remove_wall('$id')\">Remove However, every time I click on the link, it keeps trying to re ...

Uploading Multiple Files with Base64 Encoding in PHP

I am looking for a way to store images in a database and retrieve them using base64 encoding/decoding. Can anyone guide me on how to achieve this? Here is the HTML code: <form method="POST" action="add_script.php" enctype="multipart/form-data"> ...

What is the best way to bend a Polyline in react-google-maps?

Just dipping my toes into React and experimenting with the react-google-maps package. I'm on a mission to curve a Polyline connecting two locations. I've reviewed the documentation and I'm attempting to integrate the curve polyline function ...

What is the best way to utilize ng-if in the index.html page depending on the URL?

Is there a way to hide certain elements in the index page based on the URL of nested views? In my index.html file, I am looking to implement something like this: <top-bar ng-if="!home"></top-bar> <ui-view class="reveal-animation"> ...

Use the jqwidgets jxGrid with the checkbox selection mode to enable row selection by checking for any clicks on the row

Currently, I am using a jqxGrid that contains checkboxes. I would like the ability for users to select a row by clicking on any part or column of the row, not just on the checkbox itself. Unfortunately, it seems this feature is not directly supported. Has ...

Displaying an error message in the designated errors div is necessary when the regular expression does not produce a match

Is there a way to display an error message in the errors div when the jQuery match function returns false? <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link href="form.css"> ...

clearing all input fields upon submission in React Native

I need help resolving an error that occurs when I try to clear text input fields on a button press. Instead of clearing the fields, it throws an undefined error because I am trying to set the value as {this.state.inputTextValue} and then clear it using set ...

What are some strategies for preventing unused setState functions in React? Is it possible to create a React useState without a setter function

Currently working on reducing or eliminating npm warnings related to the React site. Many of these warnings are due to the setState function, which is marked as 'unused' in the code snippet below. const [state, setState] = useState('some st ...

Button to scroll down

I have successfully implemented a #scrolldownbutton that scrolls to the first component. However, I am now attempting to modify it so that when the button is clicked, the page smoothly scrolls within the viewport and stops at the partially visible componen ...

Fetching chat messages using an AJAX script depending on the timestamp they were sent

I am currently working on developing a real-time chat application with various rooms as part of my project. Progress has been smooth so far, but I am facing an issue with properly fetching the most recent chat messages from the database. The message table ...

Access control and permissions in Angular's UI router

I am struggling to figure out how to implement permissions and access control in our Angular app. Currently, our setup looks like this: app.config(['$stateProvider', function ($stateProvider) { $stateProvider .state('default', { ...

Determining the exact position of an absolute element within a Bootstrap container

In my design, I am using a full-width cover image with a bootstrap container class containing an absolutely positioned image on top of the cover image. My goal is to have this image positioned exclusively on the right-hand side of the container. Below is ...

gathering information from a group of items and organizing them into a fresh set of items

I have a collection of objects called Lines nestled within the rows array, which is nested within the tabs array. My goal is to extract specific data from the lines array and transfer them into a new array named 'colors'. This is how the initial ...

Sync user information when alterations are made on a different device

As I create a Discord clone using Next.js, I've encountered an issue where when a server is deleted, another client can still see and use the server until the page is reloaded. When testing out the official Discord web app, changes seemed to happen in ...

The Parse.com cloudcode refuses to enter the success or error state

Running this code in my Parse cloud, I noticed that when I call it from my app, it never enters the success or error statement. Could it be because the .save method isn't working? Any assistance would be greatly appreciated :) This is how I invoke t ...