Three.js - Object marked as either chosen or unselected

Within my three.js environment, I have the ability to add and modify objects. Recently, I implemented a DAT.GUI folder that allows me to adjust the color of these objects. However, in cases where no object is SELECTED, I use jQuery to hide it:

function onDocumentMouseDown(event){
    event.preventDefault();
    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    raycaster.setFromCamera(mouse, camera);
    var intersects = raycaster.intersectObjects(objects);
    if(intersects.length > 0){
        SELECTED = intersects[ 0 ].object;

        control.attach(SELECTED);
        scene.add(control);
        $(guiObject.domElement).attr("hidden", false);
    } else{
        control.detach(SELECTED);
        scene.remove(control);
        control.update();
        $(guiObject.domElement).attr("hidden", true);
    }
}

The issue arises when clicking on a button causes it to disappear because I intended for an object to be DESELECTED if the user clicked elsewhere outside of the object.

How can this problem be resolved?

Answer №1

Big shoutout to Falk for providing the perfect solution!

After some tweaking, my function now looks like this:

function onDocumentMouseDown(event){
    event.preventDefault();
    if($(event.target).is('canvas')){
        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
        raycaster.setFromCamera(mouse, camera);
        var intersects = raycaster.intersectObjects(objects);
        if(intersects.length > 0){
            SELECTED = intersects[ 0 ].object;

            control.attach(SELECTED);
            scene.add(control);
            $(guiObject.domElement).attr("hidden", false);
            // SELECTED.material.color.setHex( Math.random() * 0xffffff );
        } else{
            control.detach(SELECTED);
            scene.remove(control);
            control.update();
            $(guiObject.domElement).attr("hidden", true);
        }
    } else{
        $(guiObject.domElement).attr("hidden", false);
    }
}

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

Displaying and hiding collapsible items in Bootstrap 4: A guide to showing one item at a time

I'm working on collapsible menus and I need them to completely hide when one is clicked, instead of just collapsing. This behavior is different from an accordion setup. To achieve this, I incorporated the following function into my code: $('#bo ...

Using JSP to send variables from an external Javascript file

After creating a timer function, I am looking to display the results on a different page. The setup involves a JSP file calling functions from a separate JS file in order to output the information to another screen: Instructions in the JSP file: <butt ...

One way to showcase a single piece of data without the need for looping is by utilizing the `

I am encountering an issue. Why does the insertAdjacentHTML("afterend") output keep looping? I only want to display "1" once, not repeatedly. var btn = document.getElementById("btnClick"); btn.addEventListener("click", function (e) { e.preventDefaul ...

`In NodeJS, facing a challenge with implementing a many-to-many relationship using Sequelize's

I'm in the process of setting up many-to-many relationships between roles and accesses. The `roles` table will contain a list of roles (admin, developer, etc...) and the `accesses` table will have a list of permissions (Create Project, Create Site, De ...

Is there a way to extract data within xml tags with attributes using JavaScript/ Ionic without relying on DOM or ActiveXObject?

I am trying to extract specific information located between two tags, specifically <wsse:BinarySecurityToken> Here is an example: <wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is the text I am trying to extract!!!</wsse ...

Error: The function used in Object(...) is not defined properly in the useAutocomplete file at line 241

I am currently working on a ReactJS application that utilizes Material UI components without the use of Redux. Everything is functioning properly in my application, except when I attempt to integrate the Material UI autocomplete feature, it encounters iss ...

Tips for altering a key within a tree-view:

I am working with a potentially infinite tree-view array: type Tree = { id: number; name: string; email: string; children: Tree[]; }; const tree: Tree[] = [ { id: 1, name: 'Truck', email: '@mail', children ...

Tracking triggered JavaScript events (across all browsers)

Although this query may have been addressed previously, the responses mostly pertain to browser-specific techniques. My inquiry is straightforward: Is there a method to observe all triggered events (specifically the fired event and the corresponding elemen ...

The beauty of asynchronous GET requests in VueJS

As a newcomer to VueJS, I am exploring how to make a GET request to the GitHub API. Initially, I made a request to sort users by follower count, resulting in an array ordered in descending order of user logins. Following that, I sent another GET request to ...

When trying to link a Redis microservice with NestJS, the application becomes unresponsive

I am attempting to create a basic hybrid app following the guidance provided by Nest's documentation, but I have run into an issue where the app becomes unresponsive without any errors being thrown. main.ts import { NestFactory } from '@nestjs/c ...

Vue: The async Apollo mixin function successfully logs a value, however it ultimately returns as undefined

I've encountered numerous async/return undefined queries on this platform, but despite trying various solutions, I'm unable to make any progress. My apologies if I overlooked something obvious. In an attempt to improve reusability, I extracted a ...

At what point is it appropriate for me to delete the token?

Seeking Answers: Token Dilemmas Is it best to create the token upon user login and registration, or just on login? Should the token be saved in local storage? Do I need to send the token after every user request? Should the token o ...

Angular JS is encountering an issue where the promise object is failing to render correctly

I'm currently learning Angular and I have a question about fetching custom errors from a promise object in Angular JS. I can't seem to display the custom error message on my HTML page. What am I missing? Below is my HTML file - <!DOCTYPE htm ...

Apply the style when the page loads and remove it when clicked

There is a feature in my code that adds a class when an element with the class .tab is clicked. $(function() { $(".tab").click(function() { $(this).addClass('blueback'); $(".tab").not($(this)).removeClass('bl ...

Is there a way to keep a JS script running even after navigating away in the Google Chrome console?

Assume there is a basic script available var x = 0; run(); function run() { console.log(x++); setTimeout(run, 1000); } If I input it into the Google Chrome console. How can I keep it running even after navigating to another page (continuously d ...

Having trouble uploading Node.js and Mongoose to Heroku due to error codes H12 and H15? Need help troubleshooting and resolving this issue?

Attempting to deploy my Node, mongoose, express app on Heroku for the first time has been a challenge. The application is a simple blog with a login system. Despite extensive research and effort, I am struggling to successfully host it. Below is the error ...

Information sent by the Firefox TCP socket using the socket.send() method cannot be retrieved until the socket is closed

I am experiencing an issue while trying to send data from Firefox to a Java desktop application. My Java class functions as a server, and the Firefox script acts as a client. When I test it using another Java class called client.java, the data is successfu ...

Creating an autocomplete feature with just one input field to display information for two to three additional input fields

I'm working on implementing an autocomplete feature similar to this example: . Feel free to test it out with the code snippets provided: 1000, 1001. I've successfully implemented the autocomplete functionality where typing in Pa suggests Paris. ...

Vertical alignment issue with icons in Bootstrap 5 HTML code

Just starting out with Css and I'm having trouble aligning the google icon vertically in the center with some padding from the bottom. You can see the issue in the attached file. The text is aligned in the center. .g-icon{ background-image: url(" ...

What is the reason for the directive being available in $rootScope?

Currently, there doesn't seem to be a major issue but it has sparked my curiosity. I have a straightforward directive that, for some unknown reason, is accessible within $rootScope. JAVASCRIPT: (function(){ var app = angular.module('myAp ...