setup()
setup() runs once when the sketch starts and is the perfect place to initialize your canvas, create UI elements, and prepare audio. Everything you create here (buttons, paragraphs, audio) persists until you 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 (MAX SPEED)');
clickButton.position(width / 2 - 150, height / 2 - 50);
clickButton.size(300, 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 (16 lines)
🔧 Subcomponents:
clickSound = new p5.Oscillator();
clickSound.setType('sine');
clickSound.freq(440);
clickSound.amp(0);
clickSound.start();
Creates and configures the audio oscillator that will generate beep sounds, starting silent so no noise plays until autoClick() triggers it
clickButton = createButton('Start Auto-Clicker (MAX SPEED)');
clickButton.position(width / 2 - 150, height / 2 - 50);
clickButton.size(300, 100);
clickButton.mousePressed(toggleAutoClicker);
Creates an interactive button, positions it centered on the canvas, sizes it large for easy clicking, and links it to the toggleAutoClicker function so pressing it starts or stops the auto-clicker
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the sketch responsive to different screen sizes
background(220);- Fills the canvas with light gray (220 brightness in grayscale), providing a clean background
clickSound = new p5.Oscillator();- Creates a new oscillator object from the p5.sound library, which will generate the beep sounds
clickSound.setType('sine');- Sets the oscillator to produce a smooth sine wave, which sounds like a clean, pure tone
clickSound.freq(440);- Sets the oscillator frequency to 440 Hz, which is the musical note A4 (a standard test tone)
clickSound.amp(0);- Sets the volume to 0 (silent) initially, so the oscillator plays without making sound until we turn it up in autoClick()
clickSound.start();- Starts the oscillator running, even though it's silent; this is necessary before we can adjust its amplitude later
clickButton = createButton('Start Auto-Clicker (MAX SPEED)');- Creates a button element with the given text, storing it in the clickButton variable so we can style and control it
clickButton.position(width / 2 - 150, height / 2 - 50);- Positions the button in the center of the canvas horizontally and slightly above center vertically (subtracting 150 and 50 to account for the button's size)
clickButton.size(300, 100);- Makes the button 300 pixels wide and 100 pixels tall, creating a large, easy-to-click target
clickButton.mousePressed(toggleAutoClicker);- Attaches the toggleAutoClicker function to the button so it runs whenever the button is clicked
clickButton.style('font-size', '24px');- Makes the button text large and readable at 24 pixels
clickButton.style('background-color', '#4CAF50');- Sets the button background to a friendly green color (hex #4CAF50)
counterDisplay = createP('Clicks: 0');- Creates an HTML paragraph element to display the click counter, starting with the text 'Clicks: 0'
counterDisplay.position(width / 2 - 100, height / 2 + 70);- Positions the counter display below the button, centered horizontally
counterDisplay.style('font-size', '36px');- Makes the counter text large and highly visible at 36 pixels