Is it possible to remove several items that share the same name?

I am placing a variable number of circles on the screen.

var radius = 1; 
var segments = 32; 
var circleGeometry = new THREE.CircleGeometry( radius, segments); 
function generateCircles(){
  //scene.remove(circle);
  var count=0;
  while (1000> count) {
    circle = new THREE.Mesh (circleGeometry, material);
    scene.add (circle);
    count ++;
  }
}

Is this approach efficient?

Whenever I call this function in my code, it slows down each time. I believe this is due to the increasing number of objects in the scene. How can I address this issue?

Every time the function is invoked, I need to completely remove the generated circles from the memory stage.

http://jsfiddle.net/v8oxsxtc/

Answer №1

If you add circles to the scene without needing to move or change them later on, it's a good idea to combine them into a single Mesh for greater efficiency. This way, you can remove just one Mesh from the scene before adding more circles:

var radius = 1; 
var segments = 32; 
var circleGeometry = new THREE.CircleGeometry(radius, segments); 
var circlesMesh;

function generateCircles() {
  if (circlesMesh) { scene.remove(circlesMesh); }
  circlesMesh = new THREE.Mesh(circleGeometry, material);
  var count = 0;
  while (count++ < 999) {
    THREE.GeometryUtils.merge(circlesMesh, new THREE.Mesh(circleGeometry, material));
  }

  scene.add(circlesMesh);
}

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

React Redux - There is an error during rendering as expected props have not been received yet

After retrieving data from an API and storing it in the Redux state, I utilize a helper function within mapStateToProps to filter and modify a portion of that data before passing it along as props. Although everything appears to be functioning correctly b ...

Tips for achieving proper styling and formatting of elements in jQuery UI

I've encountered an issue when trying to use jQuery UI after downloading it multiple times. In the examples provided with the download, the UI elements appear perfectly formatted, but when I implement them on my own pages, the styles and formatting ge ...

Error: script 'start' is not defined on Heroku

Hello there! I'm currently working on deploying an application to Heroku from my GitHub repository. To ensure that everything runs smoothly, I made some modifications to the start script in the package.json file. Specifically, I adjusted it to point t ...

jQuery powered image wall with dynamic scrolling

Can someone assist me in figuring out the best approach for this task? I am interested in creating a scrolling image wall using jQuery. The goal is to display numerous small images (such as Twitter profile pics) tiled one after another across the entire w ...

I'm facing some uncertainties with a couple of AngularJS code snippets

At my workplace, I am tasked with making modifications to an angularjs project. However, I find the code quite complex and challenging to fully comprehend: app.controller("complementsController", function($scope, $rootScope, $mdSidenav, $timeout, $localSt ...

Encountering issues with rendering in React JS when utilizing state variables

I've been attempting to display content using the render method in React JS, but for some reason, the onClick code isn't executing. I'm currently enrolled in a course on Udemy that covers this topic. import React, { Component } from 'r ...

The optimal method for designing a select menu to ensure it works smoothly on various web browsers

Recently, I encountered an issue with customizing a select menu using CSS and jQuery. After some work, I was able to achieve a result that I am quite pleased with: So far, the styling works perfectly in Mozilla, Opera, Chrome, and IE7+. Below is the curr ...

Converting MySQL DateTime to seconds in JavaScript from PHP

In my JavaScript code, I have implemented a countdown timer that relies on two variables. The first variable, currentDate, is converted to milliseconds and then has 10 minutes worth of milliseconds added to it. The second variable, d, stores the current da ...

the pop-up modal function is malfunctioning

I can't get my pop up to work properly. When I click the button, the screen dims as if a pop up is going to appear, but nothing shows up. Can someone please assist me with this issue? Here is the HTML file: <!DOCTYPE html> <html lang="en"&g ...

Tips for choosing a single checkbox from a set of multiple checkboxes in React.js

I iterated through a list of objects to generate table rows, each containing an input tag with the type set as checkbox. const [ isChecked, setIsChecked ] = useState(false); const handleChange = (e) => { setIsChecked(e.target.checked) ...

Using Leaflet to set the view based on a GeoJSON dataset

I am working on incorporating a leaflet.js map with a square shape imported from an external GeoJSON file. I have determined the exact coordinates for the center of the square, and now I need to convert these coordinates in order to pass them correctly t ...

Step-by-step guide to start an AngularJs application using TypeScript

I have developed an AngularJS App using TypeScript The main app where I initialize the App: module MainApp { export class App { public static Module : ng.IModule = angular.module("mainApp", []) } } And my controller: module MainApp { exp ...

How to delete the last element of an array in Vue.js

Currently, I'm facing an issue with a filter duplication feature in my code. When I click on the "add filter" button, a duplicated filter is added to the array. However, the problem arises when I try to remove a filter using the cross icon - it always ...

Attempting to modify the state within a nested object located in an array by using json data

My objective is to retrieve data from the Google Books API, which returns JSON data in a similar format to what my state displays. I aim to update the title with the title string provided by the JSON data. Currently, I am encountering a "failed to compile" ...

The JavaScript function may fail to work if the AJAX request is not completed

Upon completion of my ajax request, my function does not seem to be functioning as expected. Here is the script I am using: <script> function sendmsg(id,msg){ alert('id is'+id+'and msg is '+msg); } <scr ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...

Creating a standalone executable for Node.js without including the entire Node.js framework

Trying to pack a simple hello world script into an executable has led to some challenges. Both pkg and nexe seem to include the entirety of Node.js in the output file, resulting in quite larger files than necessary (around 30 MB). Although EncloseJS was fo ...

Having trouble with setting the date in Firefox 57 using Selenium 3.4?

Date Field Image Having trouble setting date information beyond the specified date field using the sendKeys function. Attempted to use JavaScript but unable to successfully set the value. Update: HTML driver.findElementBy("//label[text()='Past Spe ...

Verify if the jQuery library has an existing style attribute

I've been working on a script to loop through my form inputs and check for the presence of the style attribute. Despite feeling like I'm close to getting it right, there seems to be an issue with my code. There are actually only 2 inputs with the ...

Creating a Set of Buttons in HTML

Need some assistance with grouped buttons. Let's consider a scenario where there are 5 buttons on an HTML page. When the 3rd button is clicked, buttons from 0 to 3 should change color and the function should return 3. Similarly, when the 5th button is ...