Using the background() Function in p5.js

a drawing of a house with a green roof, and a bench on a sidewalk with trees and plants on each corner

Note: this page has been created with the use of AI. Please take caution, and note that the content of this page does not necessarily reflect the opinion of Cratecode.

When you're creating a visual sketch with p5.js, one of the first things you might want to do is to set a background color. You know, to set the mood. A dark, brooding grey for a sketch about the existential dread of modern life, or a cheerful yellow for a sketch about bouncy puppies. The p5.js library gives us a handy function to do just that - background().

What is the background function?

The background() function in p5.js is used to set the color used for the background of your p5.js canvas. This function is usually called within the setup() or draw() functions. It basically gives your canvas a fresh coat of paint.

// sets the background color to white background(255);

In the example above, the background color is set to white. If you're thinking "Wait a minute. Why is it 255 for white?", check out our article on RGB color values.

Using Colors

The background() function accepts either RGB values, grayscale values or color strings. Let's look at some examples.

// sets the background color to red using RGB values background(255, 0, 0); // sets the background color to a moderate gray using a grayscale value background(122); // sets the background color to blue using a color string background('blue');

Note that when using color strings, the strings must be written in lowercase and without spaces. Otherwise, p5.js won't recognize them and will leave you with a default black background. Check out our color naming guide for more on this.

Hey there! Want to learn more? Cratecode is an online learning platform that lets you forge your own path. Click here to check out a lesson: Drawing Basic Shapes (psst, it's free!).

FAQ

What does the background() function in p5.js do?

The background() function in p5.js sets the color used for the background of your p5.js canvas. This function is usually called within the setup() or draw() functions.

What values does the background() function accept?

The background() function accepts either RGB values, grayscale values or color strings.

Can I use upper case letters when using color strings in the background() function?

No, when using color strings in the background() function, the strings must be written in lowercase and without spaces.

Can I use the background() function outside of the setup() and draw() functions?

While it is possible to call the background() function outside of the setup() and draw() functions, it is generally not recommended. The reason is that the setup() and draw() functions are special functions in p5.js that control the setup and drawing of the p5.js sketch.

Why does the background() function take a number from 0 to 255 for grayscale values?

The number from 0 to 255 represents the range of possible values in an 8-bit color model. In this model, 0 represents black, 255 represents white, and the values in between represent varying shades of gray.

Similar Articles