The orbit controls are malfunctioning, preventing the mouse from adjusting the view

I've been struggling to solve this issue for some time now and I'm not sure if it's related to the OrbitControls.js file or my own code. My goal is simple - I just want the orbit controls to work, but unfortunately nothing is moving as expected.

In my attempt, I used the following line of code:

    controls = new THREE.OrbitControls(camera, scene);

However, when I use renderer.domElement instead, I lose sight of my scene completely.

If anyone has any suggestions or ideas on how to fix this, please feel free to share them. Any help would be greatly appreciated.

Here is a snippet of my code for reference:


        <script src="libraries/three.min.js"></script>
        <script src="libraries/OrbitControls.js"></script>
        


        <script>


            let scene,camera,renderer, controls;

            function init(){

                scene = new THREE.Scene();
                scene.background = new THREE.Color( 0xdddddd );
//Camera Things

                        camera = new THREE.PerspectiveCamera( 75, window.innerWidth/  window.innerHeight, 0.1, 5000 );
                        camera.position.set(0,25,25);

//cube
                        var cubeGeometry = new THREE.BoxGeometry (10,10,10);
                        var cubeMaterial = new THREE.MeshBasicMaterial ({color: 0x1ec876});
                        cube = new THREE.Mesh (cubeGeometry, cubeMaterial);

                        cube.position.set (0, 0, 0);
                        scene.add (cube);

// controls
                    controls = new THREE.OrbitControls(camera, scene);

                    scene.add( new THREE.AxesHelper(500));

// Renderer
                    renderer = new THREE.WebGLRenderer();
                    renderer.setSize(window.innerWidth,window.innerHeight);
                    document.body.appendChild( renderer.domElement );
                    animate();
                    }

                function animate() {
                controls.update();
                renderer.render( scene, camera );
                requestAnimationFrame( animate );
                }

                init();


        </script>
    </body>

Thank you in advance!

Answer №1

controls = new THREE.TrackballControls(camera, scene);

When initializing TrackballControls in Three.js, it's crucial to remember that you must provide a DOM element as the second parameter, not a 3D object or scene. Therefore, the correct syntax is:

controls = new THREE.TrackballControls(camera, renderer.domElement);

Remember, this code should be run only after the renderer has been instantiated.

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

Silencing feature on the video control bar

I recently created a video embedded in a slider, with the intention of removing the start and stop buttons for a seamless experience. However, I now seek to include a mute button so that users have the option to silence videos with sound. Can anyone provid ...

The Click Event is failing to trigger for a dynamically created ID within a span element

I am currently working on a Task Project. In this project, I have been adding items dynamically and generating items dynamically as well. However, when it comes to deleting items by clicking on a span element, the click event doesn't seem to work. I ...

I am experiencing an issue where localhost is not displaying the JSON output for the Router and controller that I have

Recently delving into the world of NodeJS, I find myself facing a roadblock. While attempting to manage requests, my localhost appears to be endlessly loading or throwing an error. Error: cannot GET/ I aim to showcase the JSON data on my local site. Wh ...

What is the method for retrieving a child element using its ID in JavaScript?

Below is the HTML code I am working with: <div id="note"> <textarea id="textid" class="textclass">Text</textarea> </div> I am trying to retrieve the textarea element without using document.getElementById("textid"). This is what I ...

Is there a way to extract the year, day, minute, hour, and second from the Date.now() function in Node.js?

Figuring out how to interpret Date.now() has been challenging for me. I understand that it returns the time in milliseconds since the unix epoch (January 1st, 1970), but I'm struggling to use this information to determine the current date and hour. An ...

Modify HTML by replacing two consecutive tags using JavaScript

Currently working on a React project and struggling with a particular issue. My text editor seems to have trouble with certain HTML syntaxes, making the package less than ideal for my needs. The main goal is to convert the following syntax from: <p> ...

API Post request encountering fetch failure

The route "/api/users/register" on my express server allows me to register an account successfully when passing data through Postman. However, when trying to register an account using the front-end React app, I'm encountering a "TYPE ERROR: Failed to ...

AngularJS - Customizing the dropdown selection in Bootstrap

I am encountering an issue with setting the selected dropdown value from the server. <select **class="form-control input-sm"** placeholder="Choose Email" ng-model="groupForm.email" ng-options="agentListl.email for agentListl in agentList track by ag ...

Information failed to load into the datatable

i've implemented this code snippet to utilize ajax for loading data into a datatable. However, I'm encountering an issue where the data is not being loaded into the database. $('#new_table').DataTable({ "processing": true, "ser ...

What is the process for pulling out a specific JSON element based on a condition?

I am working with a JSON object that looks like this: "links" : [ { "rel" : "first", "href" : "http://localhost:8080/first" }, { "rel" : "self", "href" : "http://localhost:8080/self" }, { "rel" : "next", "href" : "http://loca ...

What is the best way to restrict the length and number of lines in a textarea based on pixel dimensions?

I am looking to create a textarea where people can select different fonts, such as "sans-serif" and "serif", to type in text. I plan to have buttons labeled "sans-serif" and "serif" that will change the font of the textarea when clicked. However, there ar ...

I am seeking a way to transform this request call from JavaScript into Ajax code

My JavaScript code includes a request function, but I believe it's outdated and I'd like to convert it into an AJAX call. Can someone assist with this update? Here is the current function in my JavaScript file: function loadRest() { const ...

Incorrect use of the jQuery .height() method

I am working on creating a responsive div with a sloped edge that needs to adjust according to the screen size. I have successfully maintained a consistent angle across all screen sizes, but I am facing an issue where the height calculation for $('#sl ...

Save the newly added items on the server using socket.io

// Creating a function to convert element to string function elemToString(elem){ return elem.outerHTML } // Create a new child div element with text content and class name let child = document.createElement("div") child.textContent = "bar" child.class ...

How can we nest a div within the outermost parent element?

Consider a scenario where there are 3 different divs named grandParent, Parent, and Child. Although the parent's tag is placed inside the grandParent, due to the use of position: absolute; property, the parent ends up being displayed outside the grand ...

Utilizing jQuery and CSS to make an entire div clickable, and activate an 'a' tag hover state upon hovering

Looking to create a clickable link for the .wrapper div that directs users to the location of a.icon. Want the .wrapper div to activate the a.icon:hover state when hovered over, not just when hovering over the icon itself. Any assistance would be highly a ...

What is the recommended approach for utilizing props versus global state within your components when working with JS Frameworks such as Vue?

Currently, I am delving into a larger project using Vue and I find myself contemplating the best practices when it comes to utilizing props versus global Vuex states for accessing data within a component. To elaborate, let's say I have a component re ...

The AngularJs 2 framework encountered an issue with booting up after attempting to combine all TypeScript files into a single JavaScript file

I am currently utilizing Angular 2 with TypeScript (V-1.8) in my project setup. I have configured my tsconfig to output the code into a single .js file. This single.js file includes the necessary code to bootstrap the application, as the boot.ts file is al ...

Node.js 0.12 now offers access to the latest ECMAScript 6 features

The latest version of Node.js (0.12) has been released with an updated Google's v8 JavaScript engine, v3.28.73. Which ECMAScript 6 features are currently available in Node.js without using the --harmony flag? I have searched multiple websites that c ...

The socket.io.js file could not be located while using (nodejs with express [4.13] and socket.io [1.3])

Currently, I am utilizing express 4.13 and socket.io 1.3.2 along with an express generator. Here is the code snippet from my app.js: var app = express(); var server=require('http').createServer(app).listen(app.get('port'),'127.0. ...