setup()
setup() runs exactly once when the sketch loads. It's the perfect place to initialize variables, create DOM elements, and set up event handlers. p5.js provides createElement(), createInput(), and createButton() to build web interfaces directly in your sketch.
function setup() {
// Create a canvas as a background for the sketch
createCanvas(windowWidth, windowHeight);
background(220); // Default background for the canvas
// Get a reference to the main chat container div from index.html
chatContainer = select('#chat-app');
// Create the chat display area (where messages appear)
chatDisplay = createElement('div');
chatDisplay.id('chat-display');
chatContainer.child(chatDisplay); // Append to the chat container
// Create the input area container
let inputArea = createElement('div');
inputArea.id('input-area');
chatContainer.child(inputArea); // Append to the chat container
// Create the message input field
inputField = createInput('');
inputField.id('message-input');
inputField.attribute('placeholder', 'Type your message...');
inputField.changed(sendMessage); // Call sendMessage when Enter is pressed
inputArea.child(inputField); // Append to the input area
// Create the send button
sendButton = createButton('Send');
sendButton.id('send-button');
sendButton.mousePressed(sendMessage); // Call sendMessage when button is clicked
inputArea.child(sendButton); // Append to the input area
// Add an initial welcome message to the chat
messages.push({ sender: "System", text: "Welcome to the chat! Say hello!", type: "system" });
updateChatDisplay(); // Display the initial messages
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a p5.js canvas that spans the full window, serving as a background for the chat interface
inputField = createInput('');
Creates a text input element where users type their messages
sendButton = createButton('Send');
Creates a clickable button that triggers message sending
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that fills the entire browser window - this will be the background behind the chat UI
background(220);- Fills the canvas with a light gray color (220 is a grayscale value from 0-255)
chatContainer = select('#chat-app');- Finds the div with id 'chat-app' in the HTML and stores a reference to it so we can add elements to it
chatDisplay = createElement('div');- Creates a new div element that will hold all the chat messages
chatDisplay.id('chat-display');- Sets the id of this div to 'chat-display' so CSS can style it
chatContainer.child(chatDisplay);- Adds the chatDisplay div as a child of the chatContainer, making it appear inside the main chat app container
inputField = createInput('');- Creates a text input element with an empty starting value
inputField.changed(sendMessage);- Registers the sendMessage function to run when the user presses Enter in the input field
sendButton = createButton('Send');- Creates a clickable button with the text 'Send'
sendButton.mousePressed(sendMessage);- Registers the sendMessage function to run when the user clicks the button
messages.push({ sender: "System", text: "Welcome to the chat! Say hello!", type: "system" });- Adds an initial welcome message to the messages array as an object with sender, text, and type properties
updateChatDisplay();- Calls the updateChatDisplay function to render all messages in the chatDisplay div