p5.js | removeElements() Function

The removeElements() function is used to remove all the elements currently present that are created by p5, except those that are created using the createCanvas() function or createGraphics() function. The elements are removed from the DOM along with their event handlers.

Syntax:

removeElements()

Parameters: This function doesn’t accept any parameter.



Below example illustrates the removeElements() function in p5.js:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(600, 300);
  textSize(26);
  fill("green")
  text("Click the mouse button to create elements", 10, 20);
  text("Click on the button below to remove all elements", 10, 50);
  
  // button to remove elements
  removeBtn = createButton('Remove All');
  removeBtn.position(20, 80);
  removeBtn.mousePressed(removeAll);
}
  
function mouseClicked() {
  // create element at mouse position
  createDiv('element').position(mouseX, mouseY);
}
  
function removeAll() {
  
  // remove all elements
  removeElements();
}

chevron_right


Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/removeElements



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.