Skip to content

background()

Sets the color or image used for the background of the canvas.

By default, the background is transparent. background() is typically used within draw() to clear the display window at the beginning of each frame. It can also be used inside setup() to set the background on the first frame of animation.

The version of background() with one parameter interprets the value one of four ways. If the parameter is a Number, it's interpreted as a grayscale value. If the parameter is a String, it's interpreted as a CSS color string. RGB, RGBA, HSL, HSLA, hex, and named color strings are supported. If the parameter is a color object, it will be used as the background color. If the parameter is a image object, it will be used as the background image.

The version of background() with two parameters interprets the first one as a grayscale value. The second parameter sets the alpha (transparency) value.

The version of background() with three parameters interprets them as RGB, HSB, or HSL colors, depending on the current colorMode(). By default, colors are specified in RGB values. Calling background(255, 204, 0) sets the background a bright yellow color.

Examples

background example 1

function setup()
  size(100, 100)

  -- A grayscale value.
  background(51)

  describe('A canvas with a dark charcoal gray background.')
end

background example 2

function setup() 
  size(100, 100)

  -- R, G, B values
  background(255, 204, 0, 128)
  describe('A canvas with a yellow background.')
end

background example 3

function setup() 
  size(100,100)
  -- 3 digit hex notation
  background('#fae')
  describe('A canvas with a pink background.')
end

background example 4

function setup() 
  size(100,100)
  -- 6-digit hex notation
  background('#222222')
  describe('A canvas with black background.')
end

background example 5

function setup() 
  size(100,100)

  --a color object
  local c = color(0, 0, 255)
  background(c)

  describe('A canvas with blue background.')
end

Syntax

background(color)
background(colorstring, [a])
background(gray, [a])
background(v1, v2, v3, [a])
background(values)
background(image)

Parameters

Parameter
color color: any value created by the color() function
colorstring string: color string, possible formats include html color name, 3-digit hex, 6-digit hex
a number: opacity of background relative to current color range (default 0 - 255)
gray number: specifies a value between white and black
v1 number: red value if color mode is RGB, or hue value if color mode is HSB
v2 number: green value if color mode is RGB, or saturation value if color mode is HSB
v3 number: blue value if color mode is RGB, or brightness value if color mode is HSB
values table: a table array containing the red, green, blue and alpha components of the color
image image: image created with loadImage() to set as background