p5.js | remove() Function

The remove() function is an inbuilt function which is used to remove the element and deregister all listeners.

This function requires p5.dom library. So add the following line in the head section of the index.html file.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script language="javascript"
    type="text/javascript" src="path/to/p5.dom.js">
</script>

chevron_right


Syntax:

remove()

Parameters: This function does not accepts any parameters.

Below examples illustrate the remove() function in p5.js:

Example 1: This example creates an element.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {  
     
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
}
  
function draw() {
    
  // Set the background color
  background('green'); 
    
  // Use createDiv() function to
  // create a div element
  var myDiv = createDiv('Welcome to GeeksforGeeks');
    
  // Set the position of div element
  myDiv.position(150, 100);  
    
  // Set the font-size of text
  myDiv.style('font-size', '24px');
    
  // Set the font color
  myDiv.style('color', 'white');
    
  // Comment the remove() function 
  // myDiv.remove();
}

chevron_right


Output:

Example 2: This example uses remove() function to remove the element.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {  
     
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
}
  
function draw() {
    
  // Set the background color
  background('green'); 
    
  // Use createDiv() function to
  // create a div element
  var myDiv = createDiv('Welcome to GeeksforGeeks');
    
  // Set the position of div element
  myDiv.position(150, 100);  
    
  // Set the font-size of text
  myDiv.style('font-size', '24px');
    
  // Set the font color
  myDiv.style('color', 'white');
    
  // Use remove() function to remove div element
  myDiv.remove();
}

chevron_right


Output:



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.