p5.js | clear() function

The clear() function in p5.js is used to clear the pixels within a buffer. This function only clears the canvas. This function clears everything to make all of the pixels 100% transparent. It can be used to reset the drawing canvas.



Parameters: This function does not accept any parameter.

Below program illustrates the clear() function in p5.js:

Example: This example uses clear() function to clear the canvas.





function setup() {
    // Create canvas 
    createCanvas(700, 700);
function draw() {
    // Draw Ellipse at mouseX and mouseY
    ellipse(mouseX, mouseY, 20, 20);
    // Set color of the ellipse
    // Set the text size
    // Set the text
    text("Press Mouse To Clear The Screen", 20, 30);
// Define the mousePressed function
function mousePressed() {


Before Clicking the mouse button:

After Clicking the mouse button:

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

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.

Improved By : ManasChhabra2

Article Tags :

Be the First to upvote.

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