Open In App

p5.js background() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.




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


Output:

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




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


Output:

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


Last Updated : 11 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads