setup()
setup() runs once when the sketch starts. It initializes all variables, creates the canvas, spawns initial particles, positions the button, and prepares the drawing layer. This is where you set up the 'scene' before animation begins.
function setup() {
// Create a canvas that fills the entire browser window
createCanvas(windowWidth, windowHeight);
// Add some initial particles randomly across the canvas
// This ensures there are particles present even before the mouse moves
for (let i = 0; i < maxParticles / 4; i++) { // Add more initial particles
particles.push(new Particle(random(width), random(height)));
}
// Set the initial position of the bouncing ball to the center of the canvas
ballX = width / 2;
ballY = height / 2;
// Set initial positions for all four pinwheels
pinwheel1X = 100; // Top-left
pinwheel1Y = 100;
pinwheel2X = width - 100; // Top-right
pinwheel2Y = 100;
pinwheel3X = 100; // Bottom-left
pinwheel3Y = height - 100;
pinwheel4X = width - 100; // Bottom-right
pinwheel4Y = height - 100;
// Create the button dynamically using p5.js DOM functions
myButton = createButton('Click Me!');
// Style the button first so we can get its dimensions correctly
myButton.style('background-color', '#4CAF50'); // Green background (keeping it green for visibility)
myButton.style('color', 'white'); // White text
myButton.style('font-size', '16px');
myButton.style('border', 'none');
myButton.style('padding', '10px 20px');
myButton.style('cursor', 'pointer');
myButton.style('z-index', '1000'); // Ensure button is on top of canvas
// Position the button in the middle of the canvas
// We use .elt.offsetWidth and .elt.offsetHeight to get the actual HTML element's dimensions
myButton.position(width / 2 - myButton.elt.offsetWidth / 2, height / 2 - myButton.elt.offsetHeight / 2);
myButton.mousePressed(buttonClicked); // Attach the function to be called on click
// Initialize the drawing layer
drawingLayer = createGraphics(width, height);
drawingLayer.stroke(255); // White stroke color for the drawing layer
drawingLayer.strokeWeight(4); // Set the thickness of the lines on the drawing layer
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
for (let i = 0; i < maxParticles / 4; i++) {
Spawns 200 particles at random positions so the canvas is active before the user moves the mouse
pinwheel1X = 100;
Places pinwheels 100 pixels from each corner, anchoring the visual composition
myButton.position(width / 2 - myButton.elt.offsetWidth / 2, height / 2 - myButton.elt.offsetHeight / 2);
Calculates the exact pixel position so the button appears centered by subtracting half its width and height from the center
createCanvas(windowWidth, windowHeight);- Creates a canvas that stretches to fill the entire browser window, making the sketch responsive and immersive
for (let i = 0; i < maxParticles / 4; i++) {- Loops 200 times (maxParticles / 4) to create initial particles so there is activity on screen immediately
particles.push(new Particle(random(width), random(height)));- Creates a new Particle object at a random position and adds it to the particles array
ballX = width / 2;- Places the bouncing ball horizontally at the center of the canvas
ballY = height / 2;- Places the bouncing ball vertically at the center of the canvas
pinwheel1X = 100;- Sets the x-coordinate of the first pinwheel to 100 pixels from the left edge
pinwheel2X = width - 100;- Sets the second pinwheel 100 pixels from the right edge, placing it in the top-right corner
myButton = createButton('Click Me!');- Creates an interactive HTML button element with the text 'Click Me!' and stores a reference to it
myButton.style('background-color', '#4CAF50');- Applies CSS styling to the button—in this case, a green background color
myButton.mousePressed(buttonClicked);- Attaches the buttonClicked function to trigger whenever the button is clicked
drawingLayer = createGraphics(width, height);- Creates a separate graphics context the same size as the canvas to store permanent drawings independently
drawingLayer.stroke(255);- Sets the stroke color to white (255) for all lines drawn on the drawing layer
drawingLayer.strokeWeight(4);- Sets the thickness of lines drawn on the drawing layer to 4 pixels