setup()
setup() runs once when the sketch starts. It's the perfect place to initialize canvas size, calculate positions, and set drawing defaults that won't change. Notice how this sketch uses responsive sizing (multiplying by width and height) instead of hard-coded numbers—this is best practice for sketches that need to work on any screen.
function setup() {
createCanvas(windowWidth, windowHeight);
// Set cake dimensions and position relative to canvas size
cakeWidth = min(width * 0.6, 400); // Max 400px wide
cakeHeight = cakeWidth * 0.6;
cakeX = width / 2;
cakeY = height / 2 + cakeHeight / 4; // Position slightly below center
// Set the text properties
textSize(min(width * 0.1, 80)); // Max 80px
textAlign(CENTER, CENTER);
textFont('Georgia'); // A classic font for birthdays
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window, enabling full-screen responsiveness
cakeWidth = min(width * 0.6, 400);
Sets cake width to 60% of window width, but caps it at 400px so it doesn't become too large on huge screens
cakeY = height / 2 + cakeHeight / 4;
Positions the cake horizontally centered and slightly below the vertical center, leaving room for the 'Happy Birthday!' text
createCanvas(windowWidth, windowHeight);- Creates a canvas that stretches to fill the entire window size, allowing the sketch to adapt to any screen
cakeWidth = min(width * 0.6, 400);- Sets the cake width to 60% of the window width, but the min() function ensures it never exceeds 400 pixels, preventing the cake from overwhelming huge displays
cakeHeight = cakeWidth * 0.6;- Makes the cake height 60% of its width, creating a proportional rectangular shape
cakeX = width / 2;- Positions the cake horizontally at the center of the canvas
cakeY = height / 2 + cakeHeight / 4;- Positions the cake slightly below the vertical center, making room above for the birthday text
textSize(min(width * 0.1, 80));- Sets the text size to 10% of window width (capped at 80px) so the 'Happy Birthday!' message scales responsively
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically around its coordinate, making positioning easier
textFont('Georgia');- Applies Georgia font, a classic serif font that looks elegant and festive for birthday text