setup()
setup() runs once when the sketch starts. Here it's used to prepare two offscreen graphics buffers with createGraphics() - a common pattern for separating layers (paper+ink vs. static overlay) so each can be redrawn or cached independently.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1); // Ensure consistent pixel density across devices
// Create graphics buffer for ink and paper texture
pg = createGraphics(width, height);
pg.pixelDensity(1);
// Create graphics buffer for vignette
vignetteGraphics = createGraphics(width, height);
vignetteGraphics.pixelDensity(1);
drawVignette(); // Draw vignette once, it's static
// Initial paper texture
drawPaperTexture(pg);
// Set last mouse moved time
lastMouseMovedTime = millis();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
pixelDensity(1);- Forces one canvas pixel per screen pixel, so high-DPI ('retina') displays don't slow down all the per-pixel noise drawing.
pg = createGraphics(width, height);- Creates a separate offscreen drawing surface the same size as the canvas, used to hold the paper texture and ink so it can be composited separately from the UI overlay.
vignetteGraphics = createGraphics(width, height);- Creates a second offscreen buffer just for the dark vignette effect, so it only needs to be calculated once instead of every frame.
drawVignette();- Draws the vignette rings into vignetteGraphics a single time since the vignette never changes.
drawPaperTexture(pg);- Fills the ink buffer with the initial cream-colored paper and fiber texture before anything is painted.
lastMouseMovedTime = millis();- Records the current time in milliseconds so the idle timer has a valid starting point.