setup()
setup() runs once when the sketch loads. It initializes the canvas, creates all DOM elements (button, bar, goal, tracker), styles them with colors and positioning, and sets up text rendering. Understanding setup() teaches you how p5.js organizes initialization and how to layer DOM elements with the canvas.
function setup() {
createCanvas(windowWidth, windowHeight);
background(220);
// Auto Launch Button
// This button initiates the auto-perfect launch sequence
autoLaunchButton = createButton("Auto Launch (Perfect)");
autoLaunchButton.position(width / 2 - autoLaunchButton.width / 2, height / 2 - 120);
autoLaunchButton.mousePressed(initiateAutoLaunch); // Call initiateAutoLaunch on click
autoLaunchButton.style('padding', '10px 20px');
autoLaunchButton.style('font-size', '18px');
autoLaunchButton.style('cursor', 'pointer');
autoLaunchButton.style('background-color', '#4CAF50'); // Green button
autoLaunchButton.style('color', 'white');
autoLaunchButton.style('border', 'none');
autoLaunchButton.style('border-radius', '5px');
// Launch Bar Container (p5.Element)
// This div acts as the background for the tracker and goal
launchBarDiv = createDiv();
launchBarDiv.style('width', barWidth + 'px');
launchBarDiv.style('height', barHeight + 'px');
launchBarDiv.style('background-color', '#333'); // Dark gray bar
launchBarDiv.style('border-radius', '5px');
launchBarDiv.style('position', 'absolute');
launchBarDiv.position(width / 2 - barWidth / 2, height / 2); // Position using p5.Element.position()
launchBarDiv.style('overflow', 'hidden'); // Keep tracker and goal inside
launchBarDiv.style('display', 'none'); // Hidden initially
// Goal Area (p5.Element, child of launchBarDiv)
// This div represents the target zone
goalDiv = createDiv();
goalDiv.style('width', (goalWidth * barWidth) + 'px');
goalDiv.style('height', barHeight + 'px');
goalDiv.style('background-color', '#ffcb00'); // Yellowish color for the goal
goalDiv.style('border-radius', '5px');
goalDiv.style('position', 'absolute');
goalDiv.style('left', ((goalX - goalWidth / 2) * barWidth) + 'px'); // Position relative to parent (launchBarDiv)
goalDiv.style('top', '0');
launchBarDiv.child(goalDiv); // Make goal a child of the bar
// Tracker (p5.Element, child of launchBarDiv)
// This div represents the moving indicator
trackerDiv = createDiv();
trackerDiv.style('width', '10px');
trackerDiv.style('height', barHeight + 'px');
trackerDiv.style('background-color', 'red'); // Red tracker
trackerDiv.style('border-radius', '2px');
trackerDiv.style('position', 'absolute');
trackerDiv.style('left', (trackerX * barWidth) + 'px'); // Initial position
trackerDiv.style('top', '0');
launchBarDiv.child(trackerDiv); // Make tracker a child of the bar
// Text styling for results
textAlign(CENTER, CENTER);
textSize(24);
noStroke();
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas that covers the entire browser window
autoLaunchButton = createButton("Auto Launch (Perfect)");
Creates a clickable button that triggers the launch sequence
launchBarDiv = createDiv();
Creates the dark gray background bar that holds the tracker and goal
goalDiv = createDiv();
Creates the yellow target zone where the tracker should land
trackerDiv = createDiv();
Creates the red moving indicator that oscillates across the bar
createCanvas(windowWidth, windowHeight);- Creates a canvas as wide and tall as the browser window, giving p5.js a drawing surface
autoLaunchButton = createButton("Auto Launch (Perfect)");- Creates a clickable HTML button with the text 'Auto Launch (Perfect)' and stores it in the autoLaunchButton variable
autoLaunchButton.position(width / 2 - autoLaunchButton.width / 2, height / 2 - 120);- Centers the button horizontally and places it 120 pixels above the vertical center of the screen
autoLaunchButton.mousePressed(initiateAutoLaunch);- Registers a callback so that clicking the button calls the initiateAutoLaunch() function
launchBarDiv.style('display', 'none');- Hides the launch bar initially—it only shows when the player clicks the launch button
launchBarDiv.child(goalDiv);- Makes the goalDiv a child of launchBarDiv, so the goal moves and stays inside the bar
launchBarDiv.child(trackerDiv);- Makes the trackerDiv a child of launchBarDiv, so the red tracker stays inside the bar and moves with it
textAlign(CENTER, CENTER);- Aligns all text horizontally and vertically centered—result messages ('Perfect!', 'Miss!') will appear in the middle of the screen