There seems to be an issue with Three.js: geometry.addEventListener isn't a recognized

I've been experimenting with Threejs to learn more about it, and I encountered an issue sooner than expected. I'm not sure if the problem lies in my code or within the framework itself (though I suspect it's on my end).

The goal was to swap one object for another when a button is clicked. Currently, my test code loads a cube initially and attempts to replace it with a sphere upon clicking the button. However, instead of the desired outcome, I'm faced with the following error:

TypeError: geometry.addEventListener is not a function
geometry.addEventListener( 'dispose', onGeometryDispose );

This is the HTML content:

<!doctype html>
<html lang="en">
<head>
  <title>My Test Page</title>
  <meta charset="utf-8>
</head>
<body style="margin: 0;">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.js"></script>
  <script src="OrbitControls.js"></script>
  <script>
      var WIDTH = 500,
         HEIGHT = 500;
      var scene = new THREE.Scene();
      var aspect = WIDTH / HEIGHT;
      var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
      camera.position.set(0,0,5);
      scene.add(camera);
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize( WIDTH , HEIGHT );
      document.body.appendChild( renderer.domElement );

      var geometry = new THREE.BoxGeometry( 1, 1, 1 ); 
      var material = new THREE.MeshNormalMaterial(); 
      var cube = new THREE.Mesh( geometry, material ); 
      scene.add( cube ); 

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

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


    </script>
    <div>
        <button onclick="updateObject();">Update Object</button>
    </div>
</body>
<script src="scripts.js" type="text/javascript" charset="utf-8"></script>
</html>

Here's the function that should change the object:

function updateObject() {
    scene.remove(cube)
    var position = new THREE.Vector3(0, 0, 0); 
    var geometry = new THREE.SphereGeometry(1.4);
    var material = new THREE.MeshNormalMaterial();
    var sphere = new THREE.Mesh(geometry, material);
    scene.add(sphere);
}

Could you help identify what I might be doing wrong?

For Orbit controls, visit: https://github.com/mrdoob/three.js/blob/master/examples/js/controls/OrbitControls.js

Answer №1

You may want to consider using THREE.SphereGeometry instead of THREE.Sphere:

var geometry = new THREE.SphereGeometry(parseFloat(1.4));

For more information, refer to the documentation here and here.

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

The sorting feature for isotopes is malfunctioning following the addition of a new div element

Isotope was utilized for sorting divs based on attribute values, however, a problem arises when a new div is added or an existing div is edited. The sorting functionality does not work properly in such cases, as the newly created or edited div is placed at ...

Invalid Syntax: The token '21' is found unexpectedly at column 12 in the expression [2013-08-28 21:10:14] beginning at [21:10:14]

I'm in the process of creating a straightforward directive for a date countdown. However, I've hit a roadblock with this particular error: Syntax Error: Token '21' is an unexpected token at column 12 of the expression [2013-08-28 21:10 ...

Vue data set is displaying as undefined

Having an issue passing the value and index of an object to a vue method. The method successfully displays the value, but the index remains undefined. Looking for guidance on where the mistake might be. JAVASCRIPT: <div class="select is-info"> ...

Utilize a variable within a regular expression

Can the variable label be used inside a regex like this? const label = 'test' If I have the regex: { name: /test/i } Is it possible to use the variable label inside the regex, in the following way? { name: `/${label}/i` } What do you think? ...

Setting the width of a div element dynamically according to its height

Here is a snippet of my HTML code: <div class="persoonal"> <div class="left"></div> <div class="rigth"></div> </div> This is the CSS code I have written: .profile { width: 100%;} .left { width: 50%; float: le ...

Speed up the opening and closing of a div element on mouse hover

Looking to create a smooth delay for opening and closing a div when mouse hover. Here is the code I have: function show_panel1() { document.getElementById('hoverpanel1').style.display="block"; } function hide_panel1() { document.getElementByI ...

Issue with the datepicker not toggling properly in Angular-UI 0.11.0, only opens

I am struggling with implementing 2 datepickers using Angular UI version 0.11.0. Here is my HTML code: <span ng-if="periods.period == 10"> <input type="text" datepicker-popup="dd-MMMM-yyyy" ng-model="cdate.customStartDate" is-open="opened1" ...

What could be the reason for reverse geocoding failure following the marker's movement when autocomplete event is triggered in Google Maps?

When I initiate the map and move the pin, the reverse geocoding correctly updates the input with the new address. However, if I manually type in an address using autocomplete and then drag the marker, the reverse geocoding stops functioning: var map; ...

How can I replay an HTML audio element?

I created an HTML5 page with an audio element for playing music in mp3 format. However, when the music plays to the end, it stops and I'm using JavaScript to control the audio element. Even so, I can't seem to replay it, only stop it. Is there a ...

What is the best way to transform an Object into an Array?

[ { id: '5b3a223296fb381a29cf6fd9', number: 1, name: 'Tablet White EliteBook Revolve 810 G2', dprice: '0', image: '' } ] This message is generated by the angular application. Upon inspecting its type, it was identi ...

Implementing a unique shader on a sprite using THREE.js

My goal is to apply procedural structures to faces, starting with the task of creating a billboard featuring a nuclear blast in open space. I attempted to create an animated radial gradient for this, and achieved some success. The key element for each fra ...

Implementing validation for a Textbox based on changes in another component's value in React.js

Is it possible to trigger validation of a Textbox based on the value change of another custom component that updates the state? Handlers: handleValueChange = (val, elementName) => { this.setState({ ...this.state, [elementName]: val ...

Error message in JS and HTML is totally bizarre

My expertise in JavaScript is very limited, so the terms I use might not be entirely accurate. However, I'll do my best to explain the issue at hand. I'm facing a peculiar problem... I've been attempting to modify someone else's JS scr ...

Leveraging the power of AJAX with either jquery or plain javascript to parse nested JSON data and display the

Similar Question: jquery reading nested json I am seeking a reliable method to iterate through multiple sets of data stored in JSON, some of which may have deep levels of nesting. My goal is to display this data in a table format. I am uncertain abou ...

error 404 when sending a xhr request in node and react js

I am currently developing a basic login page in React that needs to connect to a database through an AJAX call to a Node.js file. Here is the Node.js code I have implemented: var express=require('express'); var app=express(); var db=require(&ap ...

The npm start command is throwing a TypeError because it cannot determine the node

I am encountering an issue while trying to start a react app using npm start. My project is using node js 10.24.1, and due to it being a legacy project, I am unable to upgrade to a newer version. I have attempted clearing the cache with force, deleting n ...

The proper way to link various asynchronous operations enclosed in promises

My current understanding of promises is that they serve as a wrapper for async functions within the outer environment (such as the browser or node.js). However, I am struggling with how to properly connect async operations using promises in software develo ...

Executing a Select Change in a React Application using CasperJS

Has anyone else encountered difficulties with this issue? I have a basic React page set up, with a simple component that renders a select element and triggers a callback function when the value changes. Here is the basic structure of the component: const ...

The body tag mysteriously disappears after the function is called within the $().html() command

As I delve into scraping the source code of a website, I encounter an interesting phenomenon. Initially, when I print out the complete source code, everything appears as expected. However, upon attempting to print an actual DOM, I notice a slight change i ...

"Vue.js: The Ultimate Guide to Event Management and Data Handling

I recently started learning Vue.js and I'm having some difficulty with my coding exercises: The task is to have a menu button that opens a dropdown box when clicked, and when any selection is made, it should go back to the menu button. index.js cons ...