background()

Category:Drawing

Sets the color used for the background of your game.

This function is typically used within draw() to reset the display window at the beginning of each frame.

The color parameter can take one of two forms. It can be:

  • The lowercase name of a color inside " ". A full list of color names can be found at W3 Schools - Colors
  • A call to the rgb() command.

The default background color is white.

Examples

// Draw a blue background
background("blue");

Changing the Background Color

Use the mouse pointer position to change the color of the background.

                            // Use the mouse pointer position to change the color of the background.
function draw() {
  background(rgb(World.mouseX, World.mouseY, 0));
}
                        

RGB Colors

Using the rgb block, create a background using RGB colors.

// Use RGB numbers to set a background color.
background(rgb(63, 127, 255));

Syntax

background(color)

Parameters

NameTypeRequired?Description
color String The color used for the background or a call to the rgb() command.

Returns

No return value. Changes output to the display only.

Tips

  • A full list of color names can be found at W3 Schools - Colors.
  • For more specific color selection, or to randomize color selection, use rgb() as a parameter to background instead of a color name.
  • To fill the entire screen use background instead of rect() because it will fill the screen regardless of how big the screen is.
  • When animating a drawing using the draw() function, you need to redraw the entire image, back to front, not just the part that is moving.

Found a bug in the documentation? Let us know at documentation@code.org