setup()
setup() runs once when the sketch starts. It's the ideal place to build offscreen buffers with createGraphics() that will be reused every frame instead of being recreated.
function setup(){
createCanvas(windowWidth,windowHeight);
cols=[color(220,60,70),color(70,110,220),color(245,205,70),color(90,180,110),color(170,90,210)];
makePaper();
paintLayer=createGraphics(width,height);paintLayer.clear();
}
Line-by-line explanation (4 lines)
createCanvas(windowWidth,windowHeight);- Creates a canvas that fills the entire browser window, so the painting area always matches the screen size.
cols=[color(220,60,70),color(70,110,220),color(245,205,70),color(90,180,110),color(170,90,210)];- Builds the array of five p5.Color objects used both for the brush and for the palette buttons - a reddish, a blue, a yellow, a green, and a purple.
makePaper();- Calls the helper function that renders the speckled cream paper texture into its own offscreen buffer.
paintLayer=createGraphics(width,height);paintLayer.clear();- Creates a second offscreen buffer the same size as the canvas, dedicated to holding painted strokes, and clears it so it starts fully transparent.