setup()
setup() runs once when the sketch starts. It initializes the canvas, sets color modes, creates UI elements, and prepares the off-screen graphics buffer. This is where all one-time initialization happens.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
angleMode(DEGREES);
noStroke();
uploadButton = createButton('Upload Image');
uploadButton.position(10, 10);
uploadButton.mousePressed(triggerFileInput);
fileInput = createFileInput(handleFileSelect);
fileInput.hide();
cartoonImg = createGraphics(width, height);
cartoonImg.colorMode(HSB, 360, 100, 100, 100);
cartoonImg.noStroke();
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight); colorMode(HSB, 360, 100, 100, 100);
Creates a full-screen canvas and switches to HSB (Hue, Saturation, Brightness) mode so worm colors are vibrant and easy to control
uploadButton = createButton('Upload Image'); uploadButton.position(10, 10); uploadButton.mousePressed(triggerFileInput);
Creates a green button in the top-left corner that triggers the hidden file input when clicked
cartoonImg = createGraphics(width, height);
Creates an off-screen drawing surface where the cartoon-processed image will be stored
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the neon trails fill the whole screen
colorMode(HSB, 360, 100, 100, 100);- Switches to HSB (Hue-Saturation-Brightness) color mode, where hue is 0-360 degrees. This makes it easy to pick random vibrant neon colors by just varying hue
angleMode(DEGREES);- Makes all angle measurements use degrees (0-360) instead of radians, making the code more intuitive
noStroke();- Disables outlines on shapes drawn afterward, so only fills are visible
uploadButton = createButton('Upload Image');- Creates a clickable button labeled 'Upload Image' for the user to select a photo
uploadButton.position(10, 10);- Positions the button 10 pixels from the left and top edges of the screen
uploadButton.mousePressed(triggerFileInput);- Tells the button to call triggerFileInput() when clicked, which opens the file browser
fileInput = createFileInput(handleFileSelect);- Creates a hidden file input element that triggers handleFileSelect() when a file is chosen
fileInput.hide();- Hides the default p5.js file input UI so only our styled Upload button is visible
cartoonImg = createGraphics(width, height);- Creates an off-screen graphics buffer the same size as the canvas where the processed cartoon image will be drawn
cartoonImg.colorMode(HSB, 360, 100, 100, 100);- The graphics buffer also uses HSB mode so colors are consistent