setup()
setup() runs exactly once when the sketch starts. Use it to initialize your canvas, set drawing modes, load data, and seed starting values that will change how the sketch looks and behaves.
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(RADIANS); // Ensure angles are in radians (default, but good to be explicit)
strokeCap(ROUND); // Rounded ends for branches, for a softer look
// Load the initial color palette
loadPalette(currentPaletteIndex);
// Initialize noise offsets with random values for varied starting animations
angleNoiseOffset = random(1000);
reductionFactorNoiseOffset = random(1000);
skyNoiseOffset1 = random(1000);
skyNoiseOffset2 = random(1000);
groundNoiseOffset = random(1000); // NEW: Initialize ground noise offset
// Initialize the initial branch length based on the canvas height
branchLength = height * 0.25;
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that fills the entire browser window
loadPalette(currentPaletteIndex);
Loads the first color palette into global color variables
angleNoiseOffset = random(1000);
reductionFactorNoiseOffset = random(1000);
skyNoiseOffset1 = random(1000);
skyNoiseOffset2 = random(1000);
groundNoiseOffset = random(1000);
Seeds six independent random starting points for Perlin noise, ensuring different animation patterns each time the sketch runs
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window—the tree and sky will scale responsively to any screen size
angleMode(RADIANS);- Tells p5.js to interpret all angles in radians (0 to 2π) rather than degrees—important for rotate() and angle calculations in branch()
strokeCap(ROUND);- Makes the ends of drawn lines rounded instead of square, giving branches a softer, more natural appearance
loadPalette(currentPaletteIndex);- Calls a helper function to load the initial palette's colors into the global color variables like skyColor1, branchBaseColor, and leafColor
angleNoiseOffset = random(1000);- Seeds angleNoiseOffset with a random value between 0 and 1000—this ensures the noise pattern starts at a different point each time, creating variety
branchLength = height * 0.25;- Sets the trunk length to 25% of the canvas height, scaling the tree proportionally to any window size