setup()
setup() runs once when the sketch starts. It initializes the canvas size, calculates the initial tree trunk length based on window dimensions, and defines the colors and angles that will be used throughout the animation.
function setup() {
createCanvas(windowWidth, windowHeight);
branchAngle = radians(25); // base split angle for branches
// initial trunk length based on canvas
baseLength = min(width, height) * 0.25;
// colors for branches
trunkColor = color(80, 42, 15); // dark brown
twigColor = color(150, 95, 45); // lighter brown
strokeCap(ROUND);
}
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a full-window canvas that responds to the browser window size
branchAngle = radians(25)- Converts 25 degrees to radians and stores it - this is the angle at which branches split from their parent
baseLength = min(width, height) * 0.25- Sets the trunk length to 25% of the smaller dimension, so the tree scales to fit any window size
trunkColor = color(80, 42, 15)- Defines a dark brown color for the thick trunk branches
twigColor = color(150, 95, 45)- Defines a lighter brown color for the thin twig branches
strokeCap(ROUND)- Makes line endpoints rounded instead of square, creating a smoother, more natural look