setup()
setup() runs once at sketch start. This function initializes all your colors as global variables so that draw() can reference them 60 times per second without recreating them—a performance best practice.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1); // Crucial for direct pixel array manipulation to work consistently
noStroke(); // Not strictly necessary for pixel manipulation, but good practice
// Define colors using p5.Color objects for easy RGB level access
cRed = color(200, 0, 0); // Red for shirt
cBlue = color(0, 0, 150); // Blue for pants
cBrown = color(100, 50, 0); // Brown for hair
cBlack = color(0, 0, 0); // Black for shoes
cSkin = color(255, 200, 150); // Light peach/orange for skin
cBackground = color(220); // Light grey background
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
cRed = color(200, 0, 0);
Creates p5.Color objects with .levels arrays that store RGB values for easy pixel-by-pixel access later
pixelDensity(1);
Ensures pixel array indices match canvas coordinates exactly without device pixel scaling complications
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches your entire browser window size, making the figure fill whatever space you give it
pixelDensity(1);- Tells p5.js to use a 1:1 ratio between canvas coordinates and actual pixels; without this, high-DPI screens would create misaligned pixel indices
noStroke();- Disables outline strokes on shapes (not needed for pixel manipulation but good habit to prevent confusion)
cRed = color(200, 0, 0);- Creates a p5.Color object with 100% red, 0% green, 0% blue; stores it in a global variable so draw() can access its .levels array
cBlue = color(0, 0, 150);- Creates a p5.Color object with strong blue tone for the pants
cBrown = color(100, 50, 0);- Creates a brownish color for hair by mixing red and green but no blue
cSkin = color(255, 200, 150);- Creates a peachy skin tone by mixing high red and green with medium blue