setup()
setup() is p5.js's initialization function that runs once at the very start. Use it to create your canvas, prepare variables, set up event listeners, and configure your DOM elements. Since this sketch blends canvas graphics with HTML, setup() does double duty: setting up the p5.js canvas AND hooking up the chat interface from index.html to JavaScript functions.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize stick man position to the center of the canvas
stickManX = width / 2;
stickManY = height / 2;
// No noLoop(), so draw() runs continuously for movement
// --- Chat Interface Setup ---
// Select the chat container divs from index.html
chatMessagesDiv = select('#chat-messages');
const chatInputAreaDiv = select('#chat-input-area');
// Create input field and send button using p5.js DOM functions
chatInput = createInput();
chatInput.attribute('placeholder', 'Ask me anything...');
chatInput.parent(chatInputAreaDiv); // Attach to the #chat-input-area div
sendButton = createButton('Send');
sendButton.parent(chatInputAreaDiv); // Attach to the #chat-input-area div
// Add event listeners
sendButton.mousePressed(sendMessage);
chatInput.changed(sendMessage); // Send message on Enter key press in input field
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Sets up a canvas that fills the entire window, allowing the stick man scene to expand responsively
chatMessagesDiv = select('#chat-messages');
Grabs the HTML div from index.html where chat messages will be displayed
chatInput = createInput();
Dynamically creates a text input element using p5.js, which gets appended to the chat interface
sendButton = createButton('Send');
Creates a clickable button labeled 'Send' that users press to submit messages
sendButton.mousePressed(sendMessage);
Links the send button and input field to the sendMessage() function so messages are transmitted when the user interacts
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that stretches the full width and height of the browser window, providing the drawing surface for the stick man and classroom
stickManX = width / 2;- Sets the stick man's initial horizontal position to the center of the canvas by dividing the canvas width in half
stickManY = height / 2;- Sets the stick man's initial vertical position to the center of the canvas by dividing the canvas height in half
chatMessagesDiv = select('#chat-messages');- Uses p5.js's select() function to grab the HTML element with id 'chat-messages' from index.html, storing it in a variable so we can add chat text to it later
const chatInputAreaDiv = select('#chat-input-area');- Selects the HTML div where the input field and button will live, allowing us to attach p5.js DOM elements to a specific container
chatInput = createInput();- Dynamically creates a text input field using p5.js, which becomes a variable we can read from and clear later
chatInput.attribute('placeholder', 'Ask me anything...');- Sets the placeholder text inside the input field—the gray hint text that disappears when you type
chatInput.parent(chatInputAreaDiv);- Tells p5.js to attach the input field as a child of the chatInputAreaDiv HTML element, positioning it correctly in the chat interface
sendButton = createButton('Send');- Creates a clickable button element with the label 'Send' that will trigger the sendMessage() function
sendButton.parent(chatInputAreaDiv);- Attaches the send button to the chat input area div so it sits next to the input field in the interface
sendButton.mousePressed(sendMessage);- Registers sendMessage() as a callback function that runs whenever the send button is clicked
chatInput.changed(sendMessage);- Registers sendMessage() to trigger when the user presses Enter in the input field (the 'changed' event on an input)