Encountering an issue of THREE.EventDispatcher being undefined while trying to create a THREE.OrbitControls instance using THREE.js, THREE.OrbitControls, and TypeScript

Attempting to delve into typescript alongside three.js, I've encountered a perplexing error that has me stumped. The root of the issue lies within the init function where I initialize a new THREE.OrbitControls controller. Utilizing the setup provided at https://github.com/pinqy520/three-typescript-starter.

Error message received: Uncaught TypeError: Cannot read property 'prototype' of undefined

// Import necessary libraries
import * as THREE from 'three';
import * as ColladaLoader from 'three-collada-loader';
import * as OrbitControls from 'three-orbit-controls';

// Define scene dimensions
const WIDTH: number = window.innerWidth;
const HEIGHT: number = window.innerHeight;

// Set camera attributes
const VIEW_ANGLE: number = 45;
const ASPECT: number = WIDTH / HEIGHT;
const NEAR: number = 0.1;
const FAR: number = 10000;

// Initialize WebGL renderer, camera, and scene
const renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer();
const camera: THREE.PerspectiveCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
const scene: THREE.Scene = new THREE.Scene();

// Load model into scene
let monster: any;
const loader: ColladaLoader = new ColladaLoader();
loader.options.convertUpAxis = true;
loader.load(
    '/src/monster.dae',
    (collada: THREE.ColladaModel): void => {
        monster = collada.scene;
        monster.scale.set(0.001, 0.001, 0.001);
        monster.position.set(0, 0.1, 0);

        scene.add(monster);

        init();
        animate()
    });

// Initialization function 
const init = (): void => {
    // Get DOM element for rendering
    const container = document.querySelector('#canvas');

    // Set camera position and add to scene
    camera.position.set(0, 2, 10)
    scene.add(camera);

    // Create grid helper
    const gridHelper: THREE.GridHelper = new THREE.GridHelper(10, 20);
    scene.add(gridHelper);

    // Add ambient light
    const ambientLight: THREE.AmbientLight = new THREE.AmbientLight(0xcccccc)
    scene.add(ambientLight);

    // Add directional light
    const directionalLight: THREE.DirectionalLight = new THREE.DirectionalLight((0xffffff));
    directionalLight.position.set(0, 1, -1).normalize;
    scene.add(directionalLight);

    // Configure renderer settings
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(WIDTH, HEIGHT);

    // Attach renderer-supplied DOM element
    container.appendChild(renderer.domElement);

    // Instantiate OrbitControls for camera manipulation
    let controls: THREE.OrbitControls = new OrbitControls(camera, renderer.domElement);
}

// Recursive animation loop
let animate = (): void => {

    requestAnimationFrame(animate);

    render();

}

// Rendering function
let render = (): void => {
    renderer.render(scene, camera);

}

Answer №1

After reviewing this post on GITHUB, it appears that the previously used syntax is no longer effective. Therefore, it is recommended to update OrbitControls as follows:

THREE.EventDispatcher.apply( this );

Answer №2

In recent updates to threejs, there has been a modification in the approach towards utilizing default exports from a module.

Instead of employing:

import THREE from 'three';

The recommended practice now is:

import * as THREE from 'three';

Answer №3

After some research, I found a solution that worked for me: https://www.npmjs.com/package/orbit-controls-es6

One key thing to remember:

Be sure to use new OrbitControls instead of new THREE.OrbitControls

import OrbitControls from 'orbit-controls-es6';

const controls = new OrbitControls(camera, renderer.domElement);

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

Refreshing the identification of a button

I have been working on updating an ID with a value from another button. Here is my current progress: $('.viewemployment').on('click', function(e){ var url = '<?php echo Config::get('URL'); ?>dashboard/employmen ...

When I try to integrate Three.js into my React application, it mysteriously causes my root HTML element

While attempting to utilize Three.js in Typescript react, I successfully rendered a Dodecahedron figure along with random stars. However, my intention was to incorporate some markup into my Three.js scene using React. Unfortunately, when I render the Three ...

When validating storage content, session value appears as null

I have been working on developing an Ionic app that requires creating a session for user login. The goal is to store the user's name upon logging in, so it can be checked later if the user is still logged in. I have created a model class and a user cl ...

Unable to attach to 'leafletOptions' as it is unrecognized as a property of 'div'

It seems like I keep encountering this problem, which is often resolved by adjusting import statements. Right now, my imports look like this: import { LeafletModule } from 'node_modules/@asymmetrik/ngx-leaflet'; import * as L from 'leaflet& ...

Tips for retrieving information from a Vuetify modal window

Is it possible to retrieve data from a dialog in the VueJS Vuetify framework? Specifically, how can I access the username or password entered in the NewUserPopup.vue dialog from app.vue? App.vue = main template NewUserPopup.vue = Dialog template imported ...

How to pass the Node environment to layout.jade in Express without explicitly specifying the route

Passing parameters to Jade files seems like a piece of cake: app.use('/myroute', function (req, res) { res.render('myview', {somevar: 'Testing!'}); }); But, I have my layout.jade file that is automatically read and rendere ...

Fetching Data from Response Headers in Angular 4.3.3 HttpClient

(Text Editor: Visual Studio Code; TypeScript Version: 2.2.1) The main objective here is to fetch the headers of the response from a request Let's consider a scenario where we make a POST request using HttpClient within a service: import { Injec ...

Switching Story Tense on Facebook Open Graph

After creating a unique story for my facebook app, I discovered that when making a basic story post using the facebook js sdk, the resulting post is in past tense and depicts the object in singular form. For example, instead of saying: John is eating an a ...

Socket IO: Error - The call stack has exceeded the maximum size limit

Whenever a client connects to my node.js server, it crashes with a 'RangeError: Maximum call stack size exceeded' error. I suspect there's a recursive problem somewhere in my code that I can't seem to find. This is the code on my serve ...

Full-screen modal fade not functioning properly

I am trying to implement multiple fullscreen modals on a single page, but I am facing an issue where they slide in and out at an angle instead of just fading in and out. I have been unable to achieve the desired effect of a simple fade transition. If you ...

A guide on customizing ng-map markers by assigning colors to different categories

One of the functionalities I have allows users to see nearby locations based on their position and selected radius. These locations fall into different categories, and now I want to customize the markers' colors based on those categories. Here is a s ...

Creating a method that can adopt the return type of the nested function?

I have a function that takes in a callback and returns the value obtained using the useSelector hook from the react-redux library. Is there a way to utilize the return type of useSelector within my wrapper function? import { shallowEqual, useSelector } f ...

AngularJS fails to recognize Iframe element during REST request

I'm having trouble with my webpage only reading the Iframe tag. It's sending the content correctly according to Postman. Postman is giving me this content: "Conteudo": "<p>Test iframe:</p>\n\n<p><iframe framebord ...

Detecting when an object exits the proximity of another object in ThreeJS

In my ThreeJS project, I have planes (Object3D) flying inside a sphere (Mesh). My goal is to detect when a plane collides with the border of the sphere so that I can remove it and respawn it in a different location within the sphere. I am wondering how I ...

Issue with Vue.js - Double curly braces not rendering data on the page

I've recently delved into the world of vue.js Strangely enough, the double curly braces syntax doesn't seem to be rendering for me. However, when I utilize the v-text directive, everything falls into place. Here's a snippet of my code: HTM ...

Updating Select Options with Multiple Values using Javascript

My goal is to update the selected value of multiple select elements simultaneously using JavaScript. However, I am facing an issue where my script only updates one select element instead of all of them on the page. Unfortunately, I cannot modify the id att ...

Using Javascript's Speech Recognition to activate a button

I am new to using JavaScript Speech Recognition and decided to work with the Annyang library. My goal is to automatically trigger the "show date" button when the user says 'hello', without actually clicking the button. However, I've been fac ...

Uh-oh! Angular 6 has encountered an unexpected error with template parsing

I am currently working on an Angular application where I have integrated the FormsModule from '@angular/forms' in my app.module.ts. However, despite this, I keep encountering the error message No provider for ControlContainer. Error log: Uncaug ...

Struggling to access a remote URL through jQuery's getJSON function with jsonp, but encountering difficulties

Currently, I am attempting to utilize the NPPES API. All I need to do is send it a link like this and retrieve the results using jQuery. After my research, it seems that because it is cross-domain, I should use jsonp. However, I am facing difficulties m ...

Using Jquery to run a jquery script stored in a variable within jQuery syntax

This question may seem a bit confusing, so allow me to explain further. I am trying to execute a jquery script that is written in a text file obtained through an ajax request. For example, the code I receive from the ajax request looks like this: ($($(" ...