setup()
setup() runs once when the sketch starts. It's the perfect place to create DOM elements (buttons, text, images) and configure them with styles and event handlers. Everything positioned here will be responsive when windowResized() runs.
🔬 Notice how the first button uses -150 and the second uses +150 to spread them apart. What happens if you change both -150 values to 0 so the Speaker button sits at center instead of left?
buttonSpeakerBro.position(width / 2 - buttonSpeakerBro.width / 2 - 150, height * 0.4);
displaySpeakerBro.position(width / 2 - displaySpeakerBro.width / 2 - 150, height * 0.5);
buttonBeanstalk.position(width / 2 - buttonBeanstalk.width / 2 + 150, height * 0.4);
displayBeanstalk.position(width / 2 - displayBeanstalk.width / 2 + 150, height * 0.5);
function setup() {
createCanvas(windowWidth, windowHeight);
background(220); // Initial background color
// Create instructions text
instructions = createElement('h2', 'Vote for your favorite game!');
instructions.style('text-align', 'center');
instructions.style('color', '#333');
instructions.position(0, height * 0.1);
instructions.style('width', '100%');
// Create button for "Don't go hard on the speaker bro🫠"
buttonSpeakerBro = createButton("Vote for Don't go hard on the speaker bro🫠"); // Updated button text
buttonSpeakerBro.style('font-size', '24px');
buttonSpeakerBro.style('padding', '15px 30px');
buttonSpeakerBro.style('background-color', '#4CAF50'); // Green
buttonSpeakerBro.style('color', 'white');
buttonSpeakerBro.style('border', 'none');
buttonSpeakerBro.style('border-radius', '8px');
buttonSpeakerBro.style('cursor', 'pointer');
buttonSpeakerBro.style('margin', '10px');
// Adjust position for potentially longer text
buttonSpeakerBro.position(width / 2 - buttonSpeakerBro.width / 2 - 150, height * 0.4);
buttonSpeakerBro.mousePressed(incrementSpeakerBroVote); // Attach event handler
// Create display for "Don't go hard on the speaker bro🫠" votes
displaySpeakerBro = createElement('p', "Don't go hard on the speaker bro🫠 Likes: 0"); // Updated display text
displaySpeakerBro.style('font-size', '20px');
displaySpeakerBro.style('text-align', 'center');
displaySpeakerBro.style('color', '#555');
displaySpeakerBro.position(width / 2 - displaySpeakerBro.width / 2 - 150, height * 0.5);
displaySpeakerBro.style('width', 'auto'); // Let width adjust to content
// Create button for "Beanstalk Growing Game"
buttonBeanstalk = createButton('Vote for Beanstalk Growing Game');
buttonBeanstalk.style('font-size', '24px');
buttonBeanstalk.style('padding', '15px 30px');
buttonBeanstalk.style('background-color', '#008CBA'); // Blue
buttonBeanstalk.style('color', 'white');
buttonBeanstalk.style('border', 'none');
buttonBeanstalk.style('border-radius', '8px');
buttonBeanstalk.style('cursor', 'pointer');
buttonBeanstalk.style('margin', '10px');
buttonBeanstalk.position(width / 2 - buttonBeanstalk.width / 2 + 150, height * 0.4);
buttonBeanstalk.mousePressed(incrementBeanstalkVote); // Attach event handler
// Create display for "Beanstalk Growing Game" votes
displayBeanstalk = createElement('p', 'Beanstalk Growing Game Likes: 0');
displayBeanstalk.style('font-size', '20px');
displayBeanstalk.style('text-align', 'center');
displayBeanstalk.style('color', '#555');
displayBeanstalk.position(width / 2 - displayBeanstalk.width / 2 + 150, height * 0.5);
displayBeanstalk.style('width', 'auto'); // Let width adjust to content
// You can also add some visual elements on the canvas if you like
// For now, the canvas is just a background for the DOM elements.
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas that fills the entire browser window
instructions = createElement('h2', 'Vote for your favorite game!');
Creates an HTML h2 element with voting instructions
buttonSpeakerBro = createButton("Vote for Don't go hard on the speaker bro🫠");
Creates the first voting button with custom text
buttonSpeakerBro.style('font-size', '24px');
Repeatedly applies CSS styles to customize the button appearance
buttonSpeakerBro.mousePressed(incrementSpeakerBroVote);
Connects the button to a function that fires when clicked
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that fills the entire browser window. windowWidth and windowHeight are built-in p5.js variables that track screen dimensions.
background(220);- Fills the canvas with a light gray color (220 on a 0-255 brightness scale). This is mostly visual since the buttons and text sit on top.
instructions = createElement('h2', 'Vote for your favorite game!');- Creates a new HTML heading element displaying voting instructions. This is NOT drawn on the canvas—it's a real web page element.
instructions.style('text-align', 'center');- Applies CSS styling to center-align the heading text horizontally.
instructions.position(0, height * 0.1);- Places the heading at x=0 (left edge), y=10% down from the top. Uses p5.js height variable for responsive positioning.
buttonSpeakerBro = createButton("Vote for Don't go hard on the speaker bro🫠");- Creates a clickable button with the first game option label. This returns a p5.Renderer object stored in the variable.
buttonSpeakerBro.style('background-color', '#4CAF50');- Sets the button's background color to green (#4CAF50). This makes it visually distinctive.
buttonSpeakerBro.mousePressed(incrementSpeakerBroVote);- Registers incrementSpeakerBroVote as a callback function that runs whenever the button is clicked.
buttonSpeakerBro.position(width / 2 - buttonSpeakerBro.width / 2 - 150, height * 0.4);- Positions the button 150 pixels to the LEFT of center, and 40% down from the top. The subtraction by half-width centers it first, then -150 pushes it left.
displaySpeakerBro = createElement('p', "Don't go hard on the speaker bro🫠 Likes: 0");- Creates a paragraph element below the button to show the current vote count, starting at 0.
displaySpeakerBro.position(width / 2 - displaySpeakerBro.width / 2 - 150, height * 0.5);- Positions the vote count display directly below the button (50% down). Uses same left-offset math as the button.