setup()
setup() runs once when the sketch starts. It's the ideal place to initialize the canvas, create UI elements, and set starting values. All buttons are hidden and off-screen initially because they only make sense when a person is waiting.
function setup() {
createCanvas(windowWidth, windowHeight);
// Create the emoji input field
emojiInput = createInput('📄'); // Default emoji
emojiInput.id('emojiInput');
emojiInput.position(10, 10);
// Create the "Print" button
printButton = createButton('Print');
printButton.id('printButton');
printButton.position(emojiInput.x + emojiInput.width + 10, emojiInput.y);
printButton.mousePressed(triggerPrint);
printButton.style('z-index', '100'); // Ensure button is above canvas
// Create the transport selection buttons (initially hidden and off-screen)
spawnRedUFOButton = createButton('Red UFO');
spawnRedUFOButton.id('spawnRedUFOButton');
spawnRedUFOButton.position(-999, -999); // Off-screen initially
spawnRedUFOButton.mousePressed(() => dispatchTransport('redUFO'));
spawnRedUFOButton.hide();
spawnBlueUFOButton = createButton('Blue UFO');
spawnBlueUFOButton.id('spawnBlueUFOButton');
spawnBlueUFOButton.position(-999, -999); // Off-screen initially
spawnBlueUFOButton.mousePressed(() => dispatchTransport('blueUFO'));
spawnBlueUFOButton.hide();
spawnAirplaneButton = createButton('Airplane');
spawnAirplaneButton.id('spawnAirplaneButton');
spawnAirplaneButton.position(-999, -999); // Off-screen initially
spawnAirplaneButton.mousePressed(() => dispatchTransport('airplane'));
spawnAirplaneButton.hide();
// NEW: Create the Sharks button (initially hidden and off-screen)
spawnSharksButton = createButton('Sharks');
spawnSharksButton.id('spawnSharksButton');
spawnSharksButton.position(-999, -999); // Off-screen initially
spawnSharksButton.mousePressed(() => dispatchTransport('sharks'));
spawnSharksButton.hide();
// Set printer position
printerX = width / 2;
printerY = height - WATER_LEVEL - 50; // Position printer above the water level
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen, responsive canvas that fills the entire browser window
emojiInput = createInput('📄');
Creates a text input where users type or paste emojis to print
printButton.mousePressed(triggerPrint);
Attaches the triggerPrint callback so clicking the button prints an emoji
spawnRedUFOButton.mousePressed(() => dispatchTransport('redUFO'));
Creates four hidden buttons that will appear when a person is waiting; each dispatches a different transport type
printerX = width / 2;
printerY = height - WATER_LEVEL - 50;
Places the printer in the center horizontally and above the water level
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full browser window size. Using window dimensions makes the sketch responsive.
emojiInput = createInput('📄');- Creates an input field with a default emoji (📄) that users can change by typing or pasting
emojiInput.position(10, 10);- Positions the input field at 10 pixels from the top-left, placing it in the corner
printButton.mousePressed(triggerPrint);- Attaches a callback function so that whenever the Print button is clicked, triggerPrint() runs
spawnRedUFOButton.position(-999, -999);- Places the button way off-screen so it's hidden but ready to reposition when needed
spawnRedUFOButton.hide();- Hides the button element entirely until a person is waiting for transport selection
printerX = width / 2;- Sets the printer's horizontal position to the canvas center
printerY = height - WATER_LEVEL - 50;- Positions the printer 50 pixels above the water level so papers have room to print without landing in water