Ways to eliminate a Collection of CSS2DObject from the environment

I have a dynamic msg_arr array of strings that I want to display visually, and I need to clear the existing errs Group of CSS2DObject from the scene before creating a new errors Group.

The issue here is that although the new CSS2DObjects are added successfully to the Group, the reset operation: this.scene.remove(this.errs); doesn't seem to be working properly!

Do I need to remove and create a new CSS2DRenderer instead?

Could you please guide me on how to ensure the complete resetting of the old Group before initializing the new one? Thank you in advance.

var camera, scene, renderer, errs;

/**
 ErrVisu class
*/
class ErrVisu{
    constructor(scene){
        this.scene = scene;
    }
    
    visuError=(x, y, errs)=>{
        const x_mm = y * 0.8 - 8.8;
        const y_mm = 2.5;
        const z_mm = x * 0.8 - 2.4;

        const circle = document.createElement('div');
        circle.id = "circle";
        circle.innerHTML = "!";
        
        const objectCSS = new THREE.CSS2DObject(circle);
        objectCSS.position.set(x_mm, y_mm, z_mm);
        objectCSS.name = 'exc_' + x + y;
        errs.add(objectCSS);
    }
    
    errsIface = (msg_arr) => {
        this.scene.remove(this.errs);
        this.errs = new THREE.Group();
        this.errs.name = "errors";
        
        for (let k in msg_arr) {
            let p_xx_yy = msg_arr[k].split("_");
            let x = Number(p_xx_yy[1]);
            let y = Number(p_xx_yy[2]);
            this.visuError(x, y, this.errs);
        }
        this.scene.add(this.errs);
    }
} 
/**
 Create the scene, camera, renderer
*/
function init() {
  scene = new THREE.Scene();
  scene.background = new THREE.Color(0x21252d);
  renderer = new THREE.WebGLRenderer({antialias: true});
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.x = 1;
  camera.position.y = 4;
  camera.position.z = 5;
  scene.add(camera);
  
  labelRenderer = new THREE.CSS2DRenderer();
  labelRenderer.setSize( window.innerWidth, window.innerHeight );
  labelRenderer.domElement.style.position = 'absolute';
  labelRenderer.domElement.style.top = '0px';
  document.body.appendChild( labelRenderer.domElement );

  controls = new THREE.OrbitControls(camera, labelRenderer.domElement);
  
  errs = new ErrVisu(scene);
  let msg_arr_ = [ "p_06_08" ];
  errs.errsIface(msg_arr_);
  window.addEventListener('resize', onWindowResize);

}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
  labelRenderer.setSize( window.innerWidth, window.innerHeight );
}

function animate() {
  requestAnimationFrame(animate);
  render();
}

function render() {
  renderer.render(scene, camera);
  labelRenderer.render( scene, camera );
}

init();
animate();
#circle {
  color: #ffffff;
  background: #ff5500;

  width: 30px;
  height: 30px;
  border-radius: 50%;
  text-align: center;
  font-size: 25px;
  font-weight: bold;
  display: inline-block;
  animation: blinkingBackground 0.5s infinite;
}
@keyframes blinkingBackground {
  0% {
opacity: 1;
  }
  25% {
opacity: 0.5;
  }
  50% {
opacity: 0.1;
  }
  75% {
opacity: 0.5;
  }
  100% {
opacity: 1;
  }
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03776b71666643332d3231312d33">[email protected]</a>/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e7fbe1f6f6d3a3bda2a1a1bda3">[email protected]</a>/examples/js/controls/OrbitControls.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="61150913040421514f5053534f51">[email protected]</a>/examples/js/renderers/CSS2DRenderer.js"></script>

Answer №1

In order to solve the issue ( reference ), it was suggested to create an array containing instances of CSS2DObjects and then proceed to remove those items from the scene individually instead of grouping them together:

errors.forEach((item) => {
    this.scene.remove(item);
});         
errors = [];

visualizeError=(x, y, errors, scene)=>{
    const x_mm = y * 0.6 - 11.1;
    const y_mm = 3.8;
    const z_mm = x * 0.6 - 3.6;

    const square = document.createElement('div');
    square.id = "square";
    square.innerHTML = "X";
    
    const cssItem = new CSS2DObject(square);
    cssItem.position.set(x_mm, y_mm, z_mm);
    cssItem.name = 'error_' + x + y;

    scene.add(cssItem);
    errors.push(cssItem);
}

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

Struggling to incorporate a basic drop-down into my design despite implementing transitions

Looking to optimize space on a webpage, I am interested in creating a hover effect for topics that reveals sub-topics with a smooth ease-out animation. Here is an example of the effect I am aiming for: https://jsfiddle.net/170z6pj1/ I have been strugglin ...

Change hyperlink text color on Android CheckBox

I am having a CheckBox with two links embedded in the text. The text is set using the following code: String htmlText = "Accept <a href='someUrl'>Terms and Conditions</a> and <a href='anotherUrl'>Privacy Policy&l ...

Discovering distinct colors for this loading dots script

Looking for assistance with a 10 loading dots animation script. I'm trying to customize the color of each dot individually, but when I create separate divs for each dot and control their colors in CSS, it causes issues with the animation. If anyone ...

Text disappears from navbar after selecting anchor link

Recently, I've been experimenting with Twitter Bootstrap and have found it to be quite delightful. I managed to create a fixed navbar that sits at the top of my website. Inside this navbar, you'll find my logo, a header, a few links, and a dropd ...

What could be the reason for CSS3 PIE failing to work on sub pages when an absolute path is used in the CSS file?

I am struggling with CSS styling and have created the following class: a.greenbutton, input.greenbutton { /*background: url("../image/button.png") repeat-x scroll left top transparent;*/ behavior: url("/css3pie/PIE.php"); -webkit-border-radiu ...

Ways to implement varying hover color effects on distinct list items using CSS

When I created a ul inside a nav tag, I wanted to apply different background-color hover effects to different li elements. However, the hover effect is only being applied to the anchor tags and not the complete li elements. The CSS properties that I have ...

Toggle Submenu Visibility with a Click

One section of my code is located in sidebar.component.html : <ul class="nav"> <li routerLinkActive="active" *ngFor="let menuItem of menuItems" class="{{menuItem.class}} nav-item"> &l ...

How to eliminate the gap below a CSS element by adjusting the top value

https://i.stack.imgur.com/Sn6Wx.png The three 'Replacement Options' sections are contained within a div with the class relative. The relevant CSS properties have been applied as shown below: .relative { top: -10rem; position: relative; ...

change the css back to its original state when a key is pressed

Is there a way to retrieve the original CSS of an element that changes on hover without rewriting it all? Below is my code: $(document).keydown(function(e) { if (e.keyCode == 27) { $(NBSmegamenu).css('display', 'none');} ...

Emphasize the checkbox

In my table, I have radio buttons that should turn the neighboring cell green when clicked. Using JQuery, I added a "highlight" class on $.change event to achieve this effect. However, I encountered an issue where some radio buttons load with the "checked ...

Dynamic object transformation in three.js with level of detail animation

I recently completed a website using three.js for 3D design work. The concept behind the website is a universe filled with various texts. There are two key elements that needed to be implemented: First, an object appears as a star when the distance betw ...

Transform website code into email-friendly HTML code

Is there any software available that can convert web HTML and CSS files to email HTML with inline CSS? This would allow the email to be viewable on various email clients such as Outlook, Thunderbird, Gmail, Yahoo, Hotmail, etc. I want to write my code lik ...

What methods can be used to incorporate animation when the display attribute transitions to none?

Is there a way to add animation in a Vue app when replacing elements? I would like the transition from, for example, clicking on a div with 'Num 1' to the divs with class 'showing' not disappear abruptly but smoothly, such as moving to ...

Ensure that the second table header remains in a fixed position

One of my tables is scrollable horizontally while the other is not. I'm looking to keep the header of the scrollable table fixed in place. You can take a look at this jsfiddle link for reference. The headers of the second table, which is enclosed i ...

What is the best way to enable my search function to filter out specific items from a table?

Currently, I have created a table populated with data fetched from a JSON file. Now, my focus is on implementing a search functionality that filters out items based on user input and displays only those table rows matching the search criteria. The code sni ...

What method does nosotroshq.com use to achieve the navigation effect when hovering over a menu item?

Seeking advice from skilled individuals familiar with JQUERY: Can anyone explain the technique used by nosotroshq.com in their top navigation bar? Specifically, how do they create the slow animation when hovering over "ABOUT US"? Any insights would be ap ...

Unable to resize the div to fit the content

My container div is not adjusting its height based on the content inside. It expands according to the header and navigation divs, but not the sidebar which is nested inside another div. Here is the HTML structure: <div id="container"> <div ...

Creating a smooth entrance effect in VueJS using CSS transitions is a breeze

Even though it seems simple, I am struggling to get it to work properly. My goal is to have the existing elements in my list shift to make room for a new element added, and then have the new element appear with a smooth fade transition. I have tried to im ...

Challenge with CSS Box Shadow

Hey there, I've been attempting to add a shadow effect to a box inside another box but I'm struggling to achieve the desired outcome. <div class="box effect2"> <div class="box effect2"> Box inside a box <div> &l ...

Tips for inverting the z-index order of stacked divs

On my webpage, I have set up multiple columns enclosed in divs like this: <div class="first column" style="width:33%; float: left;"></div> <div class="column" style="width:33%; float: left;"></div> <div class="last column" style ...