setup()
setup() runs once when the sketch loads. It creates the canvas, initializes all variables, styles the UI elements, and sets up event listeners. Everything in setup() is preparation; the real animation happens in draw().
function setup(){
createCanvas(windowWidth,windowHeight);
textAlign(CENTER,CENTER);
textSize(20);
displayedDreamsIndices = new Set(); // Initialize the Set
currentFishX = width / 2; // Initialize main fish's starting x-position
fishY = height/2+60 + 10; // Initialize main fishY to its default position
// Create input field and button
inputField = createInput();
inputField.style('width', '200px');
inputField.style('padding', '5px');
inputField.style('border', '1px solid #ccc');
inputField.style('border-radius', '5px');
inputField.style('font-size', '16px');
inputField.style('font-family', 'Arial, sans-serif');
inputField.attribute('placeholder', 'What do you want to say?');
inputField.hide(); // Start hidden
submitButton = createButton('Talk');
submitButton.style('background-color', '#4CAF50');
submitButton.style('color', 'white');
submitButton.style('padding', '6px 12px');
submitButton.style('border', 'none');
submitButton.style('border-radius', '5px');
submitButton.style('cursor', 'pointer');
submitButton.style('font-size', '16px');
submitButton.style('font-family', 'Arial, sans-serif');
submitButton.hide(); // Start hidden
// NEW: Create the "Visit Website" button
visitWebsiteButton = createButton('Visit Corbun\'s Weird Webs');
visitWebsiteButton.style('background-color', '#008CBA'); // Blue color
visitWebsiteButton.style('color', 'white');
visitWebsiteButton.style('padding', '8px 16px');
visitWebsiteButton.style('border', 'none');
visitWebsiteButton.style('border-radius', '5px');
visitWebsiteButton.style('cursor', 'pointer');
visitWebsiteButton.style('font-size', '16px');
visitWebsiteButton.style('font-family', 'Arial, sans-serif');
// Position elements initially (will be updated in draw or windowResized)
positionInputElements();
// Event listener for button click
submitButton.mousePressed(handleTalk);
// NEW: Event listener for "Visit Website" button click
visitWebsiteButton.mousePressed(handleVisitWebsite);
// Event listener for Enter key in input field
inputField.elt.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
handleTalk();
event.preventDefault(); // Prevent default form submission behavior
}
});
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth,windowHeight);
Creates a canvas that fills the entire browser window
inputField = createInput();
Creates a text input for the player to type messages to the fish
submitButton = createButton('Talk');
Creates a submit button and links it to the handleTalk function
if (event.key === 'Enter') {
Allows the player to submit messages by pressing Enter
createCanvas(windowWidth,windowHeight);- Creates a canvas that matches the full browser window size, so the sketch fills the screen.
textAlign(CENTER,CENTER);- Centers all text horizontally and vertically by their center point, not their top-left corner.
displayedDreamsIndices = new Set();- Initializes an empty Set to track which dreams have been shown (prevents repeating dreams until all are cycled).
currentFishX = width / 2;- Places the fish horizontally at the center of the canvas.
fishY = height/2+60 + 10;- Places the fish vertically near the center of the bowl (bowl is at height/2+60, fish sits slightly below center).
inputField = createInput();- Creates a text input field where the player types messages to the fish.
inputField.hide();- Hides the input field initially; it will only appear when the fish is awake and not sleeping.
submitButton = createButton('Talk');- Creates a green 'Talk' button that the player clicks to submit their message to the fish.
submitButton.mousePressed(handleTalk);- Connects the button click event to the handleTalk() function, which displays the fish's response.
if (event.key === 'Enter') {- Detects when the player presses the Enter key in the input field, allowing quick message submission without clicking the button.