Variables
Store and change values with variables.
Creating Variables
Use let to create a variable:
fn setup() {
size(300, 200)
}
fn draw() {
background(rgb(30, 30, 40))
let x = 150
let y = 100
let size = 60
fill(coral)
circle(x, y, size)
}Changing Variables
Variables declared outside functions persist between frames:
let x = 0
fn setup() {
size(300, 200)
}
fn draw() {
background(rgb(30, 30, 40))
// Move right each frame
x = x + 2
// Wrap around
if x > 320 {
x = -20
}
fill(coral)
circle(x, 100, 30)
}Math Operations
Use standard math operators:
+add-subtract*multiply/divide%remainder (modulo)
fn setup() {
size(300, 200)
}
fn draw() {
background(rgb(30, 30, 40))
let a = 10
let b = 3
fill(white)
text("10 + 3 = " + (a + b), 20, 40)
text("10 - 3 = " + (a - b), 20, 70)
text("10 * 3 = " + (a * b), 20, 100)
text("10 / 3 = " + (a / b), 20, 130)
text("10 % 3 = " + (a % b), 20, 160)
}Built-in Variables
Bloom provides useful built-in variables:
width- Canvas widthheight- Canvas heightframe- Current frame numbermouseX- Mouse X positionmouseY- Mouse Y position
fn setup() {
size(300, 200)
}
fn draw() {
background(rgb(30, 30, 40))
// Center using width/height
let cx = width / 2
let cy = height / 2
// Pulse size with frame
let size = 30 + sin(frame * 0.1) * 15
fill(coral)
circle(cx, cy, size)
}