setup()
setup() runs once when the sketch first loads. Notice how it calls all the generation functions but NOT the drawing functions—data is created here, rendered in draw(). This separation is a powerful pattern that keeps your code organized.
function setup() {
createCanvas(windowWidth, windowHeight);
// Tip: Use windowWidth/windowHeight for responsive canvas
// Generate the rock's vertices once in setup()
// This prevents the rock from changing shape every frame, avoiding flicker
generateRock();
// Generate cloud data once in setup()
generateClouds();
// Generate people data once in setup()
generatePeople();
// Set the rock's color (a shade of gray/brown)
rockColor = color(random(80, 120), random(80, 120), random(80, 120));
// --- MODIFIED ROCK POSITION CALCULATION ---
let maxY = 0;
for (let v of rockVertices) {
if (v.y > maxY) {
maxY = v.y; // Find the lowest (largest Y) point of the rock's shape relative to its center
}
}
// Position the rock horizontally in the center, and vertically so its lowest point
// sits on the grass line (height * 0.7)
rockPosition = createVector(width / 2, height * 0.7 - maxY);
// ------------------------------------------
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
for (let v of rockVertices) { if (v.y > maxY) { maxY = v.y; } }
Finds the lowest point of the rock shape so it can sit properly on the grass
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches your entire browser window size, making the sketch fully responsive
generateRock();- Generates the random vertices for the rock shape once, preventing it from changing shape every frame
generateClouds();- Creates an array of cloud data with positions and lumps (ellipses) that make up each cloud
generatePeople();- Creates an array of people data, each with position, size, and color properties
rockColor = color(random(80, 120), random(80, 120), random(80, 120));- Picks a random gray-brown color for the rock by choosing random RGB values in the 80-120 range
let maxY = 0;- Starts a variable to track the lowest (highest Y value) point of the rock
for (let v of rockVertices) { if (v.y > maxY) { maxY = v.y; } }- Loops through all rock vertices to find which one has the largest Y value (lowest point on screen)
rockPosition = createVector(width / 2, height * 0.7 - maxY);- Positions the rock's center horizontally in the middle of the canvas and vertically so its lowest point touches the grass at 70% down