setup()
setup() runs exactly once when the sketch starts, making it the right place to size the canvas and initialize starting values like position, velocity, and color mode before any animation begins.
🔬 This sets a random horizontal speed and then randomly reverses it. What happens if you remove the second line entirely so the ball always starts moving right?
ballVX = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVX *= -1; // Randomly choose initial horizontal direction
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize ball in the center
ballX = width / 2;
ballY = height / 2;
// Initialize ball velocity with random values
// Ensure velocity is not too small or zero to make it move
ballVX = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVX *= -1; // Randomly choose initial horizontal direction
ballVY = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVY *= -1; // Randomly choose initial vertical direction
// Set color mode to HSB: Hue (0-360), Saturation (0-100), Brightness (0-100), Alpha (0-100)
colorMode(HSB, 360, 100, 100, 100);
// Reference: https://p5js.org/reference/#/p5/colorMode
// Initialize ball color with a random, vibrant HSB color
// High saturation (80) and brightness (90) for vibrancy
ballColor = color(random(360), 80, 90);
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
if (random() < 0.5) ballVX *= -1;
Gives the ball a 50/50 chance of starting by moving left instead of right
if (random() < 0.5) ballVY *= -1;
Gives the ball a 50/50 chance of starting by moving up instead of down
createCanvas(windowWidth, windowHeight);- Makes the canvas exactly as big as the browser window, so the sketch fills the whole screen.
ballX = width / 2;- Places the ball's starting horizontal position at the exact center of the canvas.
ballY = height / 2;- Places the ball's starting vertical position at the exact center of the canvas.
ballVX = random(3, 8); // Random speed between 3 and 8- Picks a random horizontal speed between 3 and 8 pixels per frame - never zero, so the ball always moves.
if (random() < 0.5) ballVX *= -1; // Randomly choose initial horizontal direction- random() with no arguments returns 0-1; about half the time this flips the speed negative so the ball can start moving left.
ballVY = random(3, 8); // Random speed between 3 and 8- Same idea as ballVX but for vertical speed.
if (random() < 0.5) ballVY *= -1; // Randomly choose initial vertical direction- Randomly decides whether the ball starts moving up or down.
colorMode(HSB, 360, 100, 100, 100);- Switches p5's color system from default RGB to HSB (Hue, Saturation, Brightness, Alpha), with hue ranging 0-360 and the rest 0-100 - this makes picking vivid random colors much easier than with RGB.
ballColor = color(random(360), 80, 90);- Creates the ball's first color using a random hue (0-360) with fixed high saturation (80) and brightness (90), guaranteeing a vibrant color rather than a dull or grayish one.