setup()
setup() runs exactly once when the sketch starts. It is where you initialize the canvas, create UI elements like buttons and text displays, and prepare sound objects. Everything you create here is available to use in draw() and other functions.
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 (15 lines)
🔧 Subcomponents:
clickSound = new p5.Oscillator();
Creates a sound-generating oscillator that will produce the click audio
clickButton = createButton('Start Auto-Clicker');
Creates an interactive button that users click to toggle the auto-clicker
counterDisplay = createP('Clicks: 0');
Creates a paragraph element that will display the current click count
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window
background(220);- Fills the canvas with light gray (220 is an RGB value where all three channels equal 220)
clickSound = new p5.Oscillator();- Creates a new sound oscillator object that will generate tones for the click feedback
clickSound.setType('sine');- Sets the oscillator to produce a smooth sine wave (as opposed to square, triangle, or sawtooth)
clickSound.freq(440);- Sets the pitch to 440 Hz, which is the musical note A4 (the standard tuning reference)
clickSound.amp(0);- Sets the initial volume to 0 (silent) so we only hear sound when clicks happen
clickSound.start();- Starts the oscillator running (though silent at amp 0) so it is ready to play on demand
clickButton = createButton('Start Auto-Clicker');- Creates a clickable button with the label 'Start Auto-Clicker'
clickButton.position(width / 2 - 100, height / 2 - 50);- Positions the button 100 pixels to the left and 50 pixels above the center of the canvas
clickButton.size(200, 100);- Sets the button to be 200 pixels wide and 100 pixels tall
clickButton.mousePressed(toggleAutoClicker);- Connects the button so that clicking it calls the toggleAutoClicker function
clickButton.style('background-color', '#4CAF50');- Colors the button green using the hex color code #4CAF50
counterDisplay = createP('Clicks: 0');- Creates a paragraph element (HTML <p> tag) with the initial text 'Clicks: 0'
counterDisplay.position(width / 2 - 100, height / 2 + 70);- Positions the counter 100 pixels to the left and 70 pixels below the center
counterDisplay.style('font-size', '36px');- Makes the counter text large and readable at 36 pixels tall