setup()
setup() runs once when the sketch starts. It's where you initialize your canvas, set up colors, and establish starting values for variables. Using windowWidth and windowHeight makes your sketch responsive to window resizing.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100);
noStroke();
planetX = width / 2;
planetY = height / 2;
}
๐ง Subcomponents:
createCanvas(windowWidth, windowHeight)
Creates a canvas that fills the entire browser window, using dynamic window dimensions
colorMode(HSB, 360, 100, 100)
Switches from RGB to HSB color mode for more intuitive color animation, with hue 0-360, saturation 0-100, brightness 0-100
planetX = width / 2; planetY = height / 2
Places the planet at the center of the canvas by calculating the midpoint
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a drawing canvas that matches the full size of the browser window. windowWidth and windowHeight are built-in p5.js variables that automatically get the current window dimensions.
colorMode(HSB, 360, 100, 100)- Switches the color system from RGB to HSB (Hue, Saturation, Brightness). This makes it easier to create color animations because hue values cycle smoothly from 0-360 like a color wheel.
noStroke()- Removes the outline/border from all shapes. All polygons will be filled with solid colors only, no visible edges.
planetX = width / 2- Sets the planet's horizontal position to the center of the canvas by dividing the canvas width by 2.
planetY = height / 2- Sets the planet's vertical position to the center of the canvas by dividing the canvas height by 2.