setup()
setup() runs once when the sketch starts. It creates the canvas, sets up text styling, and creates and positions the DOM elements. This is where all your initialization happens.
🔬 These four lines style the first link. What happens if you change '#007bff' to '#ff0000' (red) and 'underline' to 'none' (no underline)?
link1.style('color', '#007bff'); // Blue link color
link1.style('text-decoration', 'underline'); // Underlined
link1.style('font-size', '24px'); // Match canvas text size
link1.style('cursor', 'pointer'); // Show pointer cursor on hover
function setup() {
createCanvas(windowWidth, windowHeight);
background(220);
textAlign(CENTER, CENTER);
textSize(24);
noStroke();
// Create the first link as a p5.Element
// https://p5js.org/reference/p5/createA/
link1 = createA('https://ai-technologies.itch.io/', 'AI Technologies on itch.io', '_blank');
link1.style('color', '#007bff'); // Blue link color
link1.style('text-decoration', 'underline'); // Underlined
link1.style('font-size', '24px'); // Match canvas text size
link1.style('cursor', 'pointer'); // Show pointer cursor on hover
link1.style('display', 'block'); // Make it block-level for easier positioning
link1.position(width / 2 - link1.width / 2, height / 3); // Position in the center
// Create the second link as a p5.Element
link2 = createA('https://vimeo.com/user260286416', 'Vimeo User 260286416', '_blank');
link2.style('color', '#007bff');
link2.style('text-decoration', 'underline');
link2.style('font-size', '24px');
link2.style('cursor', 'pointer');
link2.style('display', 'block');
link2.position(width / 2 - link2.width / 2, height * 2 / 3);
// Add some instructional text
fill(0);
text("Click the links below:", width / 2, height / 6);
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window, making the sketch responsive from the start
link1 = createA('https://ai-technologies.itch.io/', 'AI Technologies on itch.io', '_blank');
Creates an HTML anchor element that opens the itch.io URL in a new tab when clicked
link2 = createA('https://vimeo.com/user260286416', 'Vimeo User 260286416', '_blank');
Creates another HTML anchor element linking to a Vimeo profile in a new tab
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full width and height of the browser window, making the sketch fill the entire screen
background(220);- Fills the canvas with a light gray color (220 is a neutral gray on the 0-255 scale)
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically around the coordinates you provide
textSize(24);- Sets the font size for all text drawn on the canvas to 24 pixels
noStroke();- Disables outlines around shapes, though this sketch doesn't draw any shapes so it has minimal effect
link1 = createA('https://ai-technologies.itch.io/', 'AI Technologies on itch.io', '_blank');- Creates an HTML link element: the first argument is the URL, the second is the visible text, and '_blank' opens it in a new tab
link1.style('color', '#007bff');- Colors the link text blue (#007bff is a standard web blue) using inline CSS styling
link1.style('text-decoration', 'underline');- Adds an underline to the link text, making it clearly appear as a clickable link
link1.style('cursor', 'pointer');- Changes the mouse cursor to a pointer hand when hovering over the link, signaling that it's clickable
link1.style('display', 'block');- Makes the link a block-level element so it can be precisely positioned on the page using position()
link1.position(width / 2 - link1.width / 2, height / 3);- Places the link at the horizontal center and one-third of the way down the canvas—subtracting half the link's width ensures it's centered
fill(0);- Sets the text color to black (0) for the instructional text
text("Click the links below:", width / 2, height / 6);- Draws the instruction text centered horizontally and one-sixth of the way down the canvas