Background
Description
The background()
function sets the background color of the canvas. It can accept either a SIMD4 color value or individual RGB(A) components.
Syntax
// Using a SIMD4 color value
background(color: SIMD4<Float>)
// Using RGB components
background(r: Float, g: Float, b: Float, a: Float = 255)
// Using positional RGB components
background(r, g, b, a)
// Using grayscale value
background(gray: Float, a: Float = 255)
Parameters
color
A SIMD4 color value (optional)
r
Red component (0-255)
g
Green component (0-255)
b
Blue component (0-255)
a
Alpha component (0-255, defaults to 255)
gray
Grayscale value (0-255)
Example
// Set background color using a SIMD4 color
background(color(0, 0, 0))
// Set background color using RGB components
background(0, 0, 0) // Black
background(r: 255, g: 255, b: 255) // White
// Set semi-transparent background
background(0, 0, 0, 128) // Semi-transparent black
// Set grayscale background
background(0) // Black
background(255) // White
background(128, 128) // Semi-transparent gray
Notes
- The background color is applied to the entire canvas
- Input values are in the range 0-255 but are automatically converted to 0-1
- The alpha component defaults to 255 (fully opaque) if not specified
- Using a single value creates a grayscale color
- Typically called at the beginning of each frame to clear the canvas