setup()
setup() runs once when the sketch starts. In WEBGL sketches, it's where you create your canvas in 3D mode, set the camera position, and pre-generate static data like the city buildings that never change.
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
milesX = -200; // Start Miles on the left
milesZ = random(-100, 100); // Randomize Z slightly
venomX = 200; // Start Venom on the right
venomZ = random(-100, 100); // Randomize Z slightly
// Set initial camera position for a better view of the city
camera(0, -300, 500, 0, 0, 0, 0, 1, 0);
// Generate buildings once in setup()
generateBuildings();
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a full-window 3D rendering canvas using WEBGL for hardware-accelerated 3D graphics
milesX = -200; milesZ = random(-100, 100); venomX = 200; venomZ = random(-100, 100);
Places Miles Morales on the left and Venom on the right with randomized Z depth for variety each restart
camera(0, -300, 500, 0, 0, 0, 0, 1, 0);
Sets the initial 3D camera position above and behind the scene looking toward the center (0, 0, 0)
generateBuildings();
Creates all 30 buildings once at startup with random heights and positions - efficient since buildings never move
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a WEBGL canvas that fills the entire window - WEBGL is p5.js's 3D rendering mode using your GPU
milesX = -200;- Sets Miles Morales' starting X position 200 units to the left of the scene center
milesZ = random(-100, 100);- Randomizes Miles' depth (Z position) between -100 and 100 so he doesn't start in exactly the same spot each cycle
venomX = 200;- Sets Venom's starting X position 200 units to the right of the scene center
venomZ = random(-100, 100);- Randomizes Venom's depth so each fight cycle feels slightly different
camera(0, -300, 500, 0, 0, 0, 0, 1, 0);- Positions the camera at (0, -300, 500) looking toward (0, 0, 0) - the first three numbers are camera position, the next three are the look-at target, the last three define which way is 'up' (0, 1, 0)
generateBuildings();- Calls the building generation function to populate the buildings array once at startup