setup()
setup() runs once when the sketch starts. It's your chance to initialize the canvas, create UI elements, and set starting values. Everything you create here (buttons, paragraphs, sounds) persists until you explicitly destroy it or the page reloads.
function setup() {
createCanvas(windowWidth, windowHeight);
background(220);
// Setup a simple sine wave oscillator for the click sound
clickSound = new p5.Oscillator();
clickSound.setType('sine');
clickSound.freq(440); // Standard A4 note
clickSound.amp(0); // Start with zero volume
clickSound.start();
// Create the button to toggle the auto-clicker
clickButton = createButton('Start Auto-Clicker');
clickButton.position(width / 2 - 100, height / 2 - 50);
clickButton.size(200, 100);
clickButton.mousePressed(toggleAutoClicker); // Attach the toggle function
clickButton.style('font-size', '24px');
clickButton.style('background-color', '#4CAF50'); // Green color
clickButton.style('color', 'white');
clickButton.style('border', 'none');
clickButton.style('border-radius', '10px');
clickButton.style('cursor', 'pointer');
// Create a paragraph element to display the click count
counterDisplay = createP('Clicks: 0');
counterDisplay.position(width / 2 - 100, height / 2 + 70);
counterDisplay.style('font-size', '36px');
counterDisplay.style('text-align', 'center');
counterDisplay.style('width', '200px');
counterDisplay.style('color', '#333'); // Dark text color
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
clickSound = new p5.Oscillator();
Creates a reusable sine wave sound object that can be played on demand (though it's commented out in this sketch for performance reasons)
clickButton = createButton('Start Auto-Clicker');
Creates an interactive HTML button element that users click to toggle the auto-clicker
counterDisplay = createP('Clicks: 0');
Creates a paragraph element that displays the current click count and updates in real time
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window—windowWidth and windowHeight are p5.js variables that read the browser's dimensions
background(220);- Fills the entire canvas with a light gray color (220 on a 0-255 scale) once at startup
clickSound = new p5.Oscillator();- Creates a new sound oscillator object—a tool that can generate and play sine wave tones
clickSound.freq(440);- Sets the frequency of the oscillator to 440 Hz, which is the musical note A4 (middle A on a piano)
clickSound.amp(0);- Sets the oscillator's volume to 0 so it doesn't play yet—you control when sound actually plays by changing amplitude
clickButton = createButton('Start Auto-Clicker');- Creates an HTML button element with the text 'Start Auto-Clicker' and stores it in the clickButton variable
clickButton.position(width / 2 - 100, height / 2 - 50);- Positions the button centered horizontally (width/2 - half the button's width) and near the vertical center
clickButton.size(200, 100);- Sets the button's width to 200 pixels and height to 100 pixels—a large, easy-to-click target
clickButton.mousePressed(toggleAutoClicker);- Attaches the toggleAutoClicker function to the button so it runs every time the button is clicked
clickButton.style('background-color', '#4CAF50');- Sets the button's background color to green (#4CAF50)—the 'Start' state color
counterDisplay = createP('Clicks: 0');- Creates an HTML paragraph element displaying 'Clicks: 0' and stores it so we can update it later
counterDisplay.position(width / 2 - 100, height / 2 + 70);- Positions the counter display below the button, also centered horizontally
counterDisplay.style('font-size', '36px');- Makes the counter text large and easy to read at 36 pixels per character