Error in Three.js + OrbitControls: Undefined property 'addEventListener' is not readable

Currently, I am facing an issue while using OrbitControls in THREE.js. Whenever I remove the line

let cameraControl = new OrbitControls(camera)
, there are no errors. However, when this line is included, I encounter a "Uncaught TypeError: Cannot read property 'addEventListener' of undefined".

I attempted to alter OrbitControls(camera) to THREE.OrbitControls(camera), but then I received a "Uncaught TypeError: THREE.OrbitControls is not a constructor".

In my efforts to resolve this issue, I tried importing OrbitControls.js via

<script src=...></script>
outside of "module", as opposed to using import {OrbitControls} from ...;. Unfortunately, this did not resolve the problem. I also experimented with moving
let cameraControl = new OrbitControls(camera)
to different lines without success.

If anyone has any ideas on how to fix this, please feel free to share!


<body>

<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/controls/OrbitControls.js';


let scene, renderer, camera
let cube

function init() {

  scene = new THREE.Scene()
  renderer = new THREE.WebGLRenderer()
  renderer.setSize(window.innerWidth, window.innerHeight)
  document.body.appendChild(renderer.domElement)

  camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100)

  let cameraControl = new OrbitControls(camera)

  camera.position.set(10, 10, 10)
  camera.lookAt(scene.position)

  // cube
  cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1))
  scene.add(cube)

}

function render() {
  requestAnimationFrame(render)
  renderer.render(scene, camera)
}

init()
render()

</script>
</body>

Answer №1

To set up camera control, initialize a new OrbitControls instance with the camera object.

Follow this format when creating the controls:

let cameraControl = new OrbitControls(camera, renderer.domElement);

It is now required to include the second parameter.

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

Using Regular Expressions in Express routing

Does anyone have experience serving a file with a dynamic hash in its name on a specific route? The file is named like workbox-someHash.js and the hash changes every time the app is deployed. I attempted to serve it using the following code snippets: &qu ...

Unable to dynamically change the value of the submit button using angular.js

I'm facing an issue with setting the submit button value dynamically using Angular.js. The code I've written below explains my problem. <body ng-controller="MainCtrl"> <input type="submit" value="{{ !model ? 'reset' : mod ...

Troubleshooting Problems with Deploying Next Js on Firebase

I am currently working on a new Next Js application and have successfully deployed it on Vercel by linking the GitLab project. Now, I need to deploy the same project on Firebase. Here's what I have tried so far: - Ran firebase init This command gen ...

How can you pass arguments between commands in discord.js?

I need to transfer arguments; I have attempted to make them global variables, but I am unsure about the correct approach. Command using the arguments : let messageArray = message.content.split(" "); const args = messageArray.slice(1); const invi ...

What is the best way to update the color of a v-select component?

https://i.sstatic.net/6VOIo.png Currently utilizing Vuetify for my project <v-select id="makeMeBlue" dense outlined :items="form.values.urlTypes" label="Single or Multi URL" v-model="form.values.urlType" ...

Having trouble generating a readable output bundle due to the following error message: "The entry module cannot be found: Error: Unable to resolve './src/index.js'"

I'm currently working with Webpack version 6.14.8 within an Asp.net Core Razor Pages project in Visual Studio 2019. My objective is to generate a readable output file, and here's the directory structure I've set up: |-->wwwroot -----> ...

How can you iteratively filter an array using JavaScript?

My goal is to create a function that can filter an array based on one or multiple conditions. Let's examine this example data and filter criteria: var data = [ {company: "Acme", fiscal_period: '01-2019', value: 10}, {company: "Acme", f ...

What is the best way to patiently wait for lines to be printed out one by one

I am in the process of creating my own personal website with a terminal-style design, and I'm looking to showcase a welcome banner followed by a welcoming message. The effect I have in mind involves lines appearing individually from top to bottom and ...

Sign up with identical username and email on WordPress

I'm currently facing a challenge with implementing http://jsfiddle.net/67BUG/2/ in wordpress registration, but I have experimented with various approaches without success. Within the wp-login.php file, you can find the code here - http://pastebin.com ...

React failing to refresh in browser following compiler error when saved

Recently starting to work with React, I have observed an interesting behavior. When I create my app using create-react-app, it automatically updates when I save it in VSCode, which is normal. However, I have noticed that as soon as I encounter my first c ...

The integration of VueJS with the Canva Button JS API amplifies the

I have been exploring Canva's Button JS API which can be found at My goal is to implement a modular approach in utilizing their API, only loading the CanvaJS when necessary. My implementation involves triggering the load function when a button is cli ...

Utilizing Node.js, delete the author from the database and then use a GET request to display all

Exploring node and express for the first time. I've been working on an example that utilizes GET and POST methods, but now I want to implement DELETE function to delete a book based on its title. Additionally, I need to introduce another GET method to ...

Heroku is incompatible with deploying MERN applications

I've been working on deploying my React app through NodeJS and Heroku, but I keep encountering an issue in the browser. Uncaught SyntaxError: Unexpected token '<' Resulting in a blank white page being displayed. I've tried sever ...

Tips for successfully passing a ViewBag in ng-click

<th><a href="javascript:;" ng-click="order(@ViewBag.desc)">Name</a></th> I am currently implementing this code and trying to fetch data from the view bag into my Angular Controller. However, I seem to be facing some challenges in a ...

Node.js accepts JSON data sent via XMLHttpRequest

I have successfully implemented a post method using xmlhttprequest: var xhttp = new XMLHttpRequest() xhttp.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { console.log('Request finished. Pro ...

Adding a stylesheet dynamically in Angular 2: A step-by-step guide

Is there a method to dynamically add a stylesheet URL or <style></style> in Angular2? For instance, if the variable isModalOpened is set to true, I want to apply certain CSS styles to elements outside of my root component, such as the body or ...

Engaging Resources for Introducing Children to Javascript Programming

Anyone have recommendations for a physical book designed to help children learn programming using Javascript? I've come across the question "Best ways to teach a beginner to program" on Stack Overflow, but it's more focused on Python and doesn&a ...

Explain what mongoose and Schema are at the beginning, just once

Hello, I am currently working on a Node.js application using Express and MongoDB. In order to utilize MongoDB and schema, I have to define Mongoose and schema in all of my routing JavaScript files. However, I would like to only define them once. As I am ne ...

What steps should I take to make the initiallyHidden attribute in FusionCharts work properly?

I am encountering an issue with my chart that is reloaded periodically. I want to make sure that the series hidden by the user (by clicking on their legend names) remain hidden even after reloading. I attempted to set the series initiallyHidden attribute t ...

Encountered an Unhandled Rejection error in react-planner when utilizing threejs: TypeError - url.lastIndexOf function is not recognized

Incorporating react-planner into my React application, I am utilizing a collection of 3D items that are stored in a catalog within my SRC folder, using MTL, OBJ, and texture files. However, upon switching to the 3D viewer, I encountered an error while att ...