What is the best way to mimic the Three.js OrbitControl behavior while moving the mouse?

Is there a way to replicate the movement of Three.js OrbitControl without having to click and drag, instead just having the camera follow mouse movement?

I attempted to recreate it on my own, but found it to be too complex due to the camera moving on three axes, rather than just two. I'm sure someone has already solved this problem before.

My goal is for the camera to orbit around the scene origin while maintaining the same distance from it.

Answer №1

Having encountered the same challenge as the Original Poster (OP), I found a solution with valuable insights from Leeft's input:

  1. To address the issue, make adjustments in the OrbitControls.js file by updating the scope of the function handleMouseMoveRotate from

    function handleMouseMoveRotate( event )
    

    to

    this.handleMouseMoveRotate = function ( event )
    

    This modification is pivotal for manually utilizing this method within your code.

  2. Incorporate the dispose method in the JavaScript code responsible for loading the model. This will remove the default mouse controls and allow you to introduce your own event handler for mousemove, where you can then manually invoke handleMouseMoveRotate:

    initialize();
    animate();
    
    function initialize() {
        // Establish Camera, Scene and OrbitControls
        camera = new THREE.PerspectiveCamera( 45, containerWidth / containerHeight );
        scene = new THREE.Scene();
        controls = new THREE.OrbitControls(camera);
    
        // Eliminate default OrbitControls event listeners
        controls.dispose();
        controls.update();
    
        ... // Loading model and Renderer details are omitted here
    
        document.addEventListener('mousemove', onDocumentMouseMove, false);
    }
    
    function onDocumentMouseMove( event ) {
        // Trigger the event in OrbitControls programmatically
        controls.handleMouseMoveRotate(event);
    }
    
    function animate() {
        requestAnimationFrame( animate );
        render();
    }
    
    function render() {
        controls.update();
        camera.lookAt( scene.position );
        renderer.render( scene, camera );
    }
    

Please note that this approach eliminates ALL library listeners. Should you wish to re-enable them, you can do so by copying the relevant codes from here to the end of the update method.

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

Updating a Mongoose document

I need help with a Node.js and mongoose Module project. I want to find a way to count the number of documents a user has and if they have more than 0 documents, I want to edit their existing document by adding the input text to it. Here is what I have so f ...

The presence of an additional bracket is being triggered by the JSON array following

Currently, I am developing a function in Freemarker to iterate through all selected checkboxes and then display the same response on the form itself. The form is structured in Freemarker format, and I am utilizing JavaScript to create separate arrays for e ...

Angular and D3.js: Enhancing StackedBar Chart ToolTips with Added Functionality

I have modified the code from this example to be compatible with the latest versions of Angular and D3. Although it works well after a small tweak, I am unable to see the tooltips when hovering over the chart... https://i.sstatic.net/Rpebe.png <h3> ...

Utilizing the map function in React to achieve similar functionality as *ngFor in Angular

When working in React, we often utilize the map function inside JSX to iterate through items. I'm curious if it's possible to create a custom Repeat component similar to Angular's *ngFor. <Repeat for={[1, 2, 3]}> ... <Repea ...

"Revolutionary jQuery plugin designed to function independently of HTML elements

Is it possible to create a jQuery plugin that can be executed without the use of an element selector? Typically, we use: $('#myElementID').myPluginFunction(myVariables); But, is there a way to do it like this instead? $.myPluginFunction(my ...

What is the method for configuring Express to serve static files from a parent directory?

If I have a nodejs express application with the following folder structure: -app - frontend - assets - images - scripts - styles - ... - pages - backend - server.js How can I serve the static files in the assets fold ...

JSON conversion error: Unable to convert circular structure to JSON - locate problem within JSON structure

Currently, I am utilizing Contentful in conjunction with a MEAN stack. Upon querying the Contentful API, I receive a json object. contentClient.entries(query, function(err, entries){ if (err) throw err; console.log(entries); }); Lately, I have been e ...

When attempting to use $.ajax to send JSON, it instead sends a query string

I'm having an issue where I'm using ajax to send JSON data with the following script: var sendData = {"test":"opa"}; console.log(sendData); $.ajax({ contentType: "application/json", method: "POST", url: "/test", dataType: "json", ...

Retrieve the text input from the text field and display it as

Is there a way to display what users enter in a textfield when their accounts are not "Activated"? Here's an example: if(active == NULL);{ //I've attempted the following methods. //In this scenario, 'username' is the name of ...

Chat application using Node.js without the need for Socket.IO

Recently delving into the world of Node.js, I stumbled upon the fs.watchFile() method. It got me thinking - could a chat website be effectively constructed using this method (along with fs.writeFile()) in comparison to Socket.IO? While Socket.IO is reliabl ...

Updating input values in AngularJS using a custom directive in AngularJS

When the value of an input does not meet a certain condition, I need to change it. In this example, if the unit price input is changed to a non-numeric value when adding a Detail, I want to set the value to "0.00". scope.$watch(attrs.ngModel, function(new ...

"When using `app.use(express.static`, it appears that the functionality is not working properly when the app is a sub

I attempted to implement something similar to this: var main = express(); main.use(express.static(path.resolve('./asset'))); main.route('someroute', someHandle); var app = express(); app.use(express.static(path.resolve('./asset&ap ...

Show two choices in a select box next to each other

I've been attempting to display a select box with two options side by side in a list format. Struggling to find any libraries that can achieve this functionality. Here is an example: <select> <option x><option y> <optio ...

Using D3.JS, you can determine the minimum and maximum values within an array of arrays, a feature that is not

My data is structured as an array of arrays, with each inner array containing objects like this: series = [ [ { "x":2013-01-01, "y":100 }, { "x":2013-01-02, "y":300 } ...], [ { "x":2013-01-01, "y":1000 }, { "x":2013-01-02, "y":300 } ...],... ...

Updating a PlaneBufferGeometry in Three.js - Tips and Tricks

I'm currently working on implementing an ocean effect in my Three.js project. I found a helpful example on this website: https://codepen.io/RemiRuc/pen/gJMwOe?fbclid=IwAR2caTQL-AOPE2Gv6x4rzSWBrOmAh2j-raqesOO0XbYQAuSG37imbMszSis var params = { res : ...

Can you explain the concept of middleware and the app.use method in Express?

Before we dive in, I just want to clear the air that this might seem like a repeat question. However, I am curious to hear your explanation of what middleware is. I've noticed similar inquiries on Stack Overflow, but I'm hoping you can provide so ...

What is causing the CSS loader to continue showing up even after all the items have finished loading?

I have been developing an innovative newspaper/blogging platform using CodeIgniter 3.1.8 and Twitter Bootstrap 4. Currently, my focus is on implementing the AJAX functionality to load more posts dynamically. The default setup paginates the posts, display ...

Automatically fill in checkboxes based on user input

Having a simple problem here, I am attempting to dynamically populate check-boxes in an HTML form. However, when I try using the checked property of the checkbox in HTML, it doesn't work correctly. The checkbox remains checked whether I use checked=&a ...

Disable menu bar | customize menu bar in electronJS

I am developing an Angular application with Electron.js. Due to the specific design requirements of my app, which is browser-based, I do not require or want the default Electron.js menu or menu bar to be displayed. Is there a way to remove it from my app ...

What is the best way to initiate a loading event once the window has completely finished loading?

I am currently working with a framework (vue.js) that inserts images when there is a page change, without actually refreshing the entire page. When the page is loaded directly, I can implement a loading screen using the following code: loading = true; $(w ...