What steps should be taken to make an object rotate in the opposite direction of the cursor

I have a group of cubes enclosed within an Object3D with the pivot point set as the origin:
https://i.sstatic.net/JLBr9.png

My objective is to rotate the cube in the opposite direction of the cursor's position. For instance, if the cursor is on the left side of the cube, the cube should spin counter-clockwise around the y-axis (green axis). If the cursor is above the cube, it should rotate counter-clockwise around the x-axis (red axis). I initially tried to achieve this by adding a "mousemove" event listener, but it only rotates when the mouse is in motion:

document.addEventListener("mousemove", onDocumentMouseMove, false);
var mouse = new THREE.Vector2();
function onDocumentMouseMove(event) {
    event.preventDefault();
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
    {...rotations}
}

Is there a way to implement this functionality using three.js?

Answer №1

One simple approach involves detecting the cursor's position and adjusting an object's rotation to create a tilting effect. Alternatively, if you prefer full rotation, you can utilize the mesh.rotate function.

document.addEventListener("mousemove", onDocumentMouseMove, false);

var mouse = new THREE.Vector2();

function onDocumentMouseMove( e ) {
    e.preventDefault();
    mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
}

function animate() {
    mesh.rotation.set(
        (mouse.y * -0.4),
        (mouse.x * -0.8),
        0
    );
}

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

AngularJS - development of a service entity

After deliberating between posting in the Angular mailing list or seeking assistance from the JavaScript community, I have decided that this is more of a JavaScript question. Hopefully, the knowledgeable individuals on Stack Overflow can provide a quicker ...

utilizing ajax for validating logins in Laravel

I am in the process of revamping my login functionality to use ajax. Instead of redirecting users to a new page with an error message when their login fails, I want the error message to display on the login page itself without any page changes. Currently, ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...

The NGINX reverse proxy fails to forward requests to an Express application

I am currently in the process of setting up a dedicated API backend for a website that operates on /mypath, but I am encountering issues with NGINX not properly proxying requests. Below is the nginx configuration located within the sites-enabled directory ...

Ways to take a screenshot using Three.js

I have this interesting project idea that I want to work on but I'm not keen on learning new programming languages. However, after doing some research, I'm not sure if Three.js can help me achieve what I want. So here's my question: How can ...

What is the best way to send a multi-dimensional array to jQuery UI autocomplete feature?

I am working on an autocomplete field that is connected to an array of friends as its data source. GetFollowing(9, function(response) { if (response) { donor. ...

Failure to update the DOM after clicking a button and making an AJAX request

After experimenting with different iterations of this code, the outcomes have been mediocre and inconsistent. The concept is rather simple: A user inputs a search query into a text box, clicks a button, and the query is assigned to a var search. This varia ...

"Enhance Your Website with Dynamic Text Effects using JavaScript

Is there a way to continuously animate text during the loading process of an AJAX request? I've tried implementing various methods, such as using a setTimeout function or an infinite loop, but nothing seems to work for repeating the animation once it& ...

Saving solely the content of an HTML list element in a JavaScript array, excluding the image source

One issue I am facing is with an unordered list in HTML which contains items like <ul id="sortable"> <li class="ui-state-default"><img src="images/john.jpg">John</li> <li class="ui-state-default"><img src="images/lisa.jpg ...

The function `canvas.toDataURL()` does not produce an image as output

When I expect the image to return mirrored, it instead shows up as a black image. <!DOCTYPE html> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> </head> <bo ...

Identifying a loop within a hierarchy of JavaScript elements

I am facing a challenge with a list of elements that have unique IDs and parent IDs. My goal is to identify any loops in this hierarchical structure and pinpoint the ID that initiates the loop. list = [ { id: '1', parent: '2' ...

Angular UI-Router failing to show nested view on page

I've integrated UI-Router into my application but I'm encountering an issue with a nested route. Here's how my index.html page is structured; <body> <!-- Navigation code --> <!-- Content from the template --> ...

Getting Checkbox values selected by the user with ng-model in AngularJS

Having some trouble implementing Checkbox functionality, I'm having difficulty determining which checkboxes the user has selected. How can this be achieved? Here's a look at my HTML snippet: <td> <table border="0">< ...

Make sure to load the HTML content before requesting any input from the user through the prompt function

Could you please help me with a question? I'm looking to load HTML content before prompting the user for input using JavaScript's prompt() function. However, currently the HTML is only loaded after exiting the prompt. Below is the code that I hav ...

Executing multiple Ajax requests on CodeIgniter 3 from two separate pages

I am facing a critical need for a code review and unfortunately, I am unsure of where else to seek assistance besides here. Let me outline my current task: I am working on a user profile page that is designed to showcase users' comments. Users have t ...

The extensive magnetic scrolling functionality in Ionic 2 sets it apart from other frameworks

Hi everyone, I could really use some assistance! I've been working on developing an Ionic 2 App and my navigation setup is not too complex. I have a main menu where clicking on an item opens another menu with a submenu. From there, if I click on an i ...

"Array.Find function encounters issues when unable to locate a specific string within the Array

Currently, I am utilizing an array.find function to search for the BreakdownPalletID when the itemScan value matches a SKU in the array. However, if there is no match found, my application throws a 'Cannot read property breakdownPalletID of undefined& ...

How can we stop the Replace function from replacing spaces as well?

When I trigger a paste event in an input field, I have a method that replaces all special characters. However, it is also removing empty spaces between words. How can I prevent this from happening? checkSpecialCharacters(){ let value = this.form.get(&q ...

disable any other active toggle in a vue component

Exploring JavaScript and how to accomplish the following tasks Snapshot The issue I am facing is that if I click on a product, and then click on settings, both are open. However, I want only the currently clicked toggle to be open. For instance, if I cl ...

Sign up for a Jquery template event

When utilizing a jquery template, the following HTML markup is being used: <div id="results"> <div class="CommentItem" commentid="33064" id="33064" data-guid="/Profile/Profile.aspx?id=Charliedog33"> <div class="CommentPic" ...