p5.js | background() function

The background() function in p5.js is used to set the color used for the background of the p5.js canvas. The default background is transparent. The color accepted by this function can be of the RGB, HSB, or HSL color depending on the current colorMode.

Syntax:

background(c)

Parameters: This function accepts single parameter c which stores the p5.Color object, color components, or CSS color.



Below programs illustrate the background() function in p5.js:

Example 1: This example uses background() function to set the background color.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of size 300*300
    createCanvas(300, 300);
}
  
function draw() {
    background(220, 141, 155); //RGB Value
}

chevron_right


Output:

Example 2: This example uses background() function to set the background color.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of size 300*300
    createCanvas(300, 300);
}
  
function draw() {
      
    // Set background color to green
    background('green');
}

chevron_right


Output:

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



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.