Thursday, February 5, 2026
HomeLanguagesJavascriptp5.js removeElements() Function

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:




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();
}


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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32487 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6861 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12071 POSTS0 COMMENTS
Shaida Kate Naidoo
6994 POSTS0 COMMENTS
Ted Musemwa
7233 POSTS0 COMMENTS
Thapelo Manthata
6945 POSTS0 COMMENTS
Umr Jansen
6926 POSTS0 COMMENTS