setup()
setup() runs once when the sketch starts. It's where you initialize the canvas, set up variables, and configure drawing settings. Using windowWidth and windowHeight makes the sketch responsive to window size.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 255);
x = width / 2;
y = height / 2;
vx = random(-5, 5);
vy = random(-5, 5);
currentColor = color(random(255), 200, 255);
noStroke();
}
๐ง Subcomponents:
createCanvas(windowWidth, windowHeight)
Creates a canvas that fills the entire browser window dynamically
colorMode(HSB, 255)
Sets color mode to HSB (Hue, Saturation, Brightness) for more intuitive and vibrant color control
x = width / 2; y = height / 2
Places the ball at the center of the canvas
vx = random(-5, 5); vy = random(-5, 5)
Assigns random initial velocities between -5 and 5 pixels per frame for varied movement
currentColor = color(random(255), 200, 255)
Sets initial ball color with random hue, high saturation, and full brightness
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that matches the full width and height of the browser window, making the sketch responsive
colorMode(HSB, 255)- Switches to HSB color mode where values range 0-255. HSB is better for creating vibrant, saturated colors than RGB
x = width / 2- Sets the ball's starting x-position to the horizontal center of the canvas
y = height / 2- Sets the ball's starting y-position to the vertical center of the canvas
vx = random(-5, 5)- Assigns a random horizontal velocity between -5 and 5 pixels per frame, making each run unique
vy = random(-5, 5)- Assigns a random vertical velocity between -5 and 5 pixels per frame
currentColor = color(random(255), 200, 255)- Creates initial color with random hue (0-255), saturation 200 (high), and brightness 255 (full) for vibrant appearance
noStroke()- Disables the outline/border around shapes, giving the ball and shadow a cleaner appearance