setup()
setup() runs once when the sketch starts. It's where you initialize variables and configure the canvas. This sketch uses windowWidth and windowHeight to make the canvas responsive—when the window is resized, the sketch adapts. The HSB color mode is chosen because it's intuitive for creating vibrant colors: you just change the hue (the color itself) while keeping saturation and brightness high.
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);
// Initialize ball color with a random, vibrant HSB color
// High saturation (80) and brightness (90) for vibrancy
ballColor = color(random(360), 80, 90);
}
đź”§ Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire window, making the sketch responsive to different screen sizes
ballX = width / 2; ballY = height / 2;
Places the ball at the center of the canvas when the sketch starts
ballVX = random(3, 8); if (random() < 0.5) ballVX *= -1;
Sets a random horizontal speed (3-8 pixels per frame) and randomly chooses left or right direction
ballVY = random(3, 8); if (random() < 0.5) ballVY *= -1;
Sets a random vertical speed (3-8 pixels per frame) and randomly chooses up or down direction
colorMode(HSB, 360, 100, 100, 100);
Switches from RGB to HSB color mode, which makes it easier to create vibrant colors by varying hue while keeping saturation and brightness high
ballColor = color(random(360), 80, 90);
Creates a random vibrant color with full saturation (80) and high brightness (90) for the ball
Line by Line:
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full window size. Using windowWidth and windowHeight instead of fixed numbers makes the sketch responsive to different screen sizes.
ballX = width / 2;- Sets the ball's starting horizontal position to the center of the canvas. width is the canvas width in pixels.
ballY = height / 2;- Sets the ball's starting vertical position to the center of the canvas. height is the canvas height in pixels.
ballVX = random(3, 8);- Generates a random number between 3 and 8 for horizontal velocity. This determines how many pixels the ball moves left or right each frame.
if (random() < 0.5) ballVX *= -1;- 50% of the time, this multiplies ballVX by -1, reversing its direction. This randomly chooses whether the ball starts moving left or right.
ballVY = random(3, 8);- Generates a random number between 3 and 8 for vertical velocity. This determines how many pixels the ball moves up or down each frame.
if (random() < 0.5) ballVY *= -1;- 50% of the time, this multiplies ballVY by -1, reversing its direction. This randomly chooses whether the ball starts moving up or down.
colorMode(HSB, 360, 100, 100, 100);- Changes how colors are defined: Hue (0-360 degrees), Saturation (0-100%), Brightness (0-100%), Alpha transparency (0-100%). HSB makes it easier to create vibrant colors.
ballColor = color(random(360), 80, 90);- Creates a color with a random hue (0-360), high saturation (80), and high brightness (90). This ensures the ball starts with a vibrant, colorful appearance.