setup()
setup() runs once when the sketch first loads. It initializes your data structures (priceHistory), prepares the canvas, and builds the UI. Any setup work that should happen only once goes here.
function setup() {
createCanvas(windowWidth, windowHeight);
textFont('monospace');
// Initialize history with a flat line
for (let i = 0; i < maxHistory; i++) {
priceHistory.push(price);
}
createUI();
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
for (let i = 0; i < maxHistory; i++) {
priceHistory.push(price);
}
Fills the priceHistory array with 300 copies of the starting price so the chart starts with a flat baseline
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game fullscreen
textFont('monospace');- Sets the font to monospace for a classic stock-ticker look
for (let i = 0; i < maxHistory; i++) {- Loops 300 times to pre-fill the price history
priceHistory.push(price);- Adds the starting price (100) to the history array each loop iteration
createUI();- Calls the function that creates and styles all four trading buttons