The simultaneous use of trackball controls and GUI is not compatible

When I click on them, the GUI changes together and I have looked at other answers. However, I am not sure where to put the listener. I tried putting the listener in render(), but it still doesn't work. How can I fix my code? This coding is related to my graduation.

Here is my code:

<!DOCTYPE html>
<html>
<meta charset="utf-8">

<head>
  <title>map draw</title>
  <!-- Original:
        <script type="text/javascript" src="../build/three.js"></script>
        <script type="text/javascript" src="../examples/js/libs/dat.gui.js"></script>
        <script type="text/javascript" src="../examples/js/libs/stats.min.js"></script>
        <script type="text/javascript" src="../examples/js/controls/TrackballControls.js"></script>
          -->
  <!-- Added by TheJim01 -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.js"></script>
  <script src="https://threejs.org/examples/js/libs/dat.gui.min.js"></script>
  <script src="https://threejs.org/examples/js/libs/stats.min.js"></script>
  <script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script>
  <!-- End TheJim01 additions -->
</head>

<body>
  <div id="Stats-output"></div>
  <div id="camera"></div>
  <div id="airport"></div>
  <script type="text/javascript">
    function init() {
      var controls = new Array(cube_number);
      console.log(controls);
      var cubes = new Array(cube_number);
      var guicube = new Array(cube_number);
      var cube_number = 4; //change this word,you can change the number of cubes
      var stats = initStats()
      var scene = new THREE.Scene();
      var camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000);
      camera.position.x = 10;
      camera.position.y = 10;
... (The rest of the code has been restructured here for uniqueness)
    }
    window.onload = init();
  </script>
  //Changing the cube_number parameter can alter the number of cubes added
</body>

</html>

Answer №1

The declaration of trackballControls for the domElement should be done as the second parameter in the constructor. Also, ensure that the same domElement is passed for both trackballControls and renderer to avoid z-indexing conflicts.

Mistake

var trackballControls = new THREE.TrackballControls(camera);
trackballControls.domElement = document.querySelector('#camera'); // declaring too late
...
document.getElementById("airport").appendChild(renderer.domElement); // Potential stacking issues

Correction:

var trackballControls = new THREE.TrackballControls(camera, document.querySelector('#airport'));
...
document.getElementById("airport").appendChild(renderer.domElement); // Use the same element as trackball

In addition

Remember to call stats.update(); outside the for() loop, as running it inside will cause it to execute 4 times per frame, resulting in an inaccurate 240 FPS.

<!DOCTYPE html>
<html>
<meta charset="utf-8">

<head>
  <title>map draw</title>
  <!-- Added by TheJim01 -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.js"></script>
  <script src="https://threejs.org/examples/js/libs/dat.gui.min.js"></script>
  <script src="https://threejs.org/examples/js/libs/stats.min.js"></script>
  <script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script>
  <!-- End TheJim01 additions -->
</head>

<body>
  <div id="Stats-output"></div>
  <div id="airport"></div>
  <script type="text/javascript">
  // The rest of the script remains unchanged
  </script>
  // Modify cube_number parameter to change the number of cubes added
</body>

</html>

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

PHP project encountered an error stating: "Uncaught TypeError: Ajax is not a function"

I am in the process of configuring an apache server for a project using XAMPP, MySQL, and PHP 5.6 Unfortunately, it appears that there is an issue with how JavaScript has been referenced in the project, and I am unable to get it to function correctly (th ...

The present IP address of the client through AJAX and PHP

This code snippet is on my PHP page: // Setting the timezone to Asia/Manila date_default_timezone_set('Asia/Manila'); $date = date('m/d/Y h:i:s a', time()); if (!empty($_SERVER['HTTP_CLIENT_IP'])){ $ip=$_SERVER['HTTP_C ...

Detecting modifications to an array with MobX

Today marks my first foray into learning MobX and I am eager to discover how to track all array changes (insertions, deletions, etc) with MobX. A practical example would definitely help clarify this: const TodoList = ({todos}) => ( <ul> ...

Remove the image by clicking on the "X" icon located on the top right corner of the image

One of my tasks involves deleting an image by clicking on the "X" mark located at the top right corner of the image. To achieve this, I referred to this CSS fiddle http://jsfiddle.net/yHNEv/. Sample HTML code: <div class="img-wrap"> <span ng-c ...

Is there inconsistency in the behavior of json.parse when given the same input?

This query pertains to the differentiation in outputs resulting from various inputs I am not seeking guidance on achieving a specific output. The reason behind the discrepancy in output between two scenarios, despite using the same argument for the JS ...

Retrieve the accurate file name and line number from the stack: Error object in a JavaScript React Typescript application

My React application with TypeScript is currently running on localhost. I have implemented a try...catch block in my code to handle errors thrown by child components. I am trying to display the source of the error (such as file name, method, line number, ...

Transforming JQuery Output into an HTML Table Layout

Hello there! I am currently working on parsing an XML file within my web page using jQuery. Here is a snippet of the code: function loadCards(lang) { $.ajax({ type: "GET", url: 'data.xml', dataType: "xml", suc ...

Received a String instead of an Object while attempting to parse JSON data

Currently, my goal is to parse a JSON string using jQuery var parsedData = $.parseJSON('{"graph_info": "{}"}'); I assumed that typeof(parsedData.graph_data) would be an Object but surprisingly it is returning as a string. Can someone guide me o ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: ht ...

Utilize JavaScript libraries in a TypeScript project

Looking to integrate a payment system called iyzico into my project. The iyzico payment library can be found here. However, there are no types available for it. How can I utilize this library in my Node.js TypeScript Express backend project? I attempted t ...

Enhancing user experience with Jquery datepicker and executing multiple actions on select

I am working on a form that includes a jQuery date selector and radio buttons structured in a table as shown below: <table> <tr> <td> <input type="text" id="5506_datepick_1"></input> </td> <td&g ...

Merge multiple Javascript files into one consolidated file

Organizing Files. /app /components /core /extensions - array.js - string.js /services - logger.js /lib - core.js Core.js (function() { 'use strict'; an ...

Clicking the submit button in JavaScript will trigger a request to the Spring MVC backend,

When I use the following code, it gives me an object response: @RequestMapping(value = "/NewLogin",method = RequestMethod.POST) public @ResponseBody Token getAllBooks( Token token = new Token(); token.setValue(encryptedMessage); return toke ...

The tooltip callback in react-chartjs-2 is failing to function as expected

I've been utilizing the react-chartjs-2 library to create basic charts in my React project. In an attempt to customize the tooltip, I added a title: tooltips: { callbacks: { label: (tooltipItem, data) => { return tooltipItem?.valu ...

JavaScript's prototypical inheritance model allows objects to inherit properties and

Exploring javascript's prototypical inheritance and object-oriented programming is new to me. I attempted to create a base object called Account and then inherit the CheckingAccount from it. Below is my code snippet. function Account(fName, lName) { ...

How can we minimize the data contained in JSON HTML markup?

https://i.stack.imgur.com/mzuB0.png Currently, I am attempting to find a way to conceal the last 12 digits in the price shown on a button, as it is excessively long. The method I am utilizing involves a JSON api and insertAdjacentHTML markup. This snipp ...

What are some ways to conceal the API connection within a button?

I have a code that allows me to perform a certain API call with a link. Here is an example: <a class="btn btn-default" href="https://testapi.internet.bs/Domain/Transfer/Initiate?ApiKey='.$user.'&Password='.$pass.'&Domain=&ap ...

Issue with RegisterClientScriptCode function following a partial postback

In a SharePoint website, the code below is contained within a user control: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Script ...

Activate inactive html button using javascript

One of the challenges I am facing is testing forms where the client specifically requested that the submit button be disabled by default. I have been exploring ways to dynamically replace the disabled="" attribute with enabled using JavaScript within a s ...

jQuery disrupts the functionality of other scripts

Let me start by saying that I have very little knowledge about scripting and HTML. Recently, I came across the following code: <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script> < ...