setup()
setup() runs once when the sketch loads. It is where you initialize all your variables, create the canvas, and prepare data structures. In this sketch, setup() calculates the target date 128 days in the past and generates all the sunflowers that will be drawn later.
function setup() {
pixelDensity(2);
createCanvas(windowWidth, windowHeight);
petalColor1 = color(255, 200, 0);
petalColor2 = color(255, 170, 0);
centerColor1 = color(120, 60, 10);
centerColor2 = color(180, 100, 30);
stemColor = color(50, 160, 50);
leafColor = color(70, 180, 70);
backgroundColor = color(220, 255, 220);
targetTimestamp = Date.now() - (128 * 24 * 60 * 60 * 1000) - (10 * 60 * 60 * 1000);
generateSunflowers();
textAlign(CENTER, CENTER);
textSize(width * 0.035);
fill(0);
textFont('Arial');
}
Line-by-line explanation (15 lines)
🔧 Subcomponents:
petalColor1 = color(255, 200, 0);
Establishes the realistic color scheme for all flower parts—yellows for petals, browns for centers, greens for stems and leaves
targetTimestamp = Date.now() - (128 * 24 * 60 * 60 * 1000) - (10 * 60 * 60 * 1000);
Calculates the past moment in time (128 days and 10 hours ago) by subtracting milliseconds from the current time
textSize(width * 0.035);
Makes text size responsive to window width so the counter scales properly on different screen sizes
pixelDensity(2);- Sets the rendering resolution to 2x, making graphics crisp and detailed on high-DPI displays like retina screens
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the sketch fullscreen and responsive
petalColor1 = color(255, 200, 0);- Defines a bright yellow (RGB: 255, 200, 0) used for alternating petals to create color variation
petalColor2 = color(255, 170, 0);- Defines a deeper orange-yellow (RGB: 255, 170, 0) for the other set of alternating petals
centerColor1 = color(120, 60, 10);- Defines a dark brown (RGB: 120, 60, 10) for the phyllotaxis florets to create texture in the center
centerColor2 = color(180, 100, 30);- Defines a lighter brown-orange (RGB: 180, 100, 30) that alternates with centerColor1 in the spiral
stemColor = color(50, 160, 50);- Defines a natural green (RGB: 50, 160, 50) for the curved stem and leaf veins
leafColor = color(70, 180, 70);- Defines a slightly brighter green (RGB: 70, 180, 70) for the leaf shapes
backgroundColor = color(220, 255, 220);- Defines a light pastel green (RGB: 220, 255, 220) for the background, simulating a sunny field
targetTimestamp = Date.now() - (128 * 24 * 60 * 60 * 1000) - (10 * 60 * 60 * 1000);- Calculates the target moment: current time minus 128 days (converted to milliseconds) minus 10 hours (converted to milliseconds)
generateSunflowers();- Calls the helper function to populate the sunflowers array with 50 randomly-positioned Sunflower objects
textAlign(CENTER, CENTER);- Centers text both horizontally and vertically around its anchor point, making positioning easier for the counter box
textSize(width * 0.035);- Sets text size proportionally to window width (3.5% of canvas width), so text scales when the window resizes
fill(0);- Sets the text fill color to black (RGB: 0, 0, 0)
textFont('Arial');- Sets the font to Arial, a common web-safe font that renders consistently across browsers