setup()
setup() runs once when the sketch starts. Here it's responsible for sizing the canvas to match the grid dimensions and initializing the 2D array that stores every pixel's color - the sketch's entire 'memory'.
🔬 This nested loop fills every cell with white using color(255). What happens if you replace that with color(random(255)) so every cell starts as a random gray shade?
for(let i=0;i<rows;i++){ grid[i]=[]; for(let j=0;j<cols;j++) grid[i][j]=color(255); }
function setup(){
createCanvas(cols*cell, rows*cell+40);
for(let i=0;i<rows;i++){ grid[i]=[]; for(let j=0;j<cols;j++) grid[i][j]=color(255); }
noSmooth();
}
Line-by-line explanation (3 lines)
🔧 Subcomponents:
for(let i=0;i<rows;i++){ grid[i]=[]; for(let j=0;j<cols;j++) grid[i][j]=color(255); }
Builds the 2D grid array row by row and fills every cell with white as the starting color
createCanvas(cols*cell, rows*cell+40);- Creates a canvas exactly wide enough for 16 columns of 20px cells and tall enough for 16 rows plus 40 extra pixels reserved for the palette bar at the bottom.
for(let i=0;i<rows;i++){ grid[i]=[]; for(let j=0;j<cols;j++) grid[i][j]=color(255); }- Builds the grid array one row at a time: creates an empty array for each row, then fills every column in that row with white (255) so the canvas starts blank.
noSmooth();- Turns off anti-aliasing so the edges of every pixel square stay crisp and blocky instead of blurred - essential for the retro pixel-art look.