setup()
setup() runs once when the sketch starts. Here it's doing double duty: preparing the canvas for drawing AND building an entire user interface out of p5.js DOM elements (sliders, divs, spans) instead of writing raw HTML.
function setup() {
// Create a canvas of 400x400 pixels
canvas = createCanvas(400, 400);
// Attach the canvas to the 'sketch-container' div in index.html
// This allows CSS to center both the canvas and its controls together.
canvas.parent('sketch-container');
// Set the color mode to HSB (Hue, Saturation, Brightness)
// Hue: 0-360 degrees (color wheel)
// Saturation: 0-100% (intensity)
// Brightness: 0-100% (lightness)
colorMode(HSB, 360, 100, 100);
// Disable drawing outlines around shapes (we only want stroke)
noFill();
// Set the stroke weight for drawing the curve
strokeWeight(2); // https://p5js.org/reference/#/p5/strokeWeight
// Set text properties for displaying the A:B ratio
textSize(16); // https://p5js.org/reference/#/p5/textSize
textAlign(CENTER, CENTER); // https://p5js.org/reference/#/p5/textAlign
// Get the div to hold controls from index.html
controlsContainer = select('#controls-container'); // https://p5js.org/reference/#/p5/select
// === Create Sliders ===
// createSlider(min, max, initial_value, step)
// Sliders allow users to interactively change parameters.
// A Frequency Slider (integer values from 1 to 10)
aSlider = createSlider(1, 10, 3, 1); // https://p5js.org/reference/#/p5/createSlider
createControlRow('A frequency: ', aSlider); // Helper function to create labeled row
// B Frequency Slider (integer values from 1 to 10)
bSlider = createSlider(1, 10, 4, 1);
createControlRow('B frequency: ', bSlider);
// Phase Slider (float values from 0 to TWO_PI with 0.01 step)
// TWO_PI is a p5.js constant for 2 * PI radians (a full circle)
phaseSlider = createSlider(0, TWO_PI, 0, 0.01);
createControlRow('Phase: ', phaseSlider);
// Create a paragraph element to display the A:B ratio
ratioText = createP(''); // https://p5js.org/reference/#/p5/createP
ratioText.id('ratio-display'); // Assign an ID for CSS styling
controlsContainer.child(ratioText); // Add the paragraph to the controls container
}
Line-by-line explanation (10 lines)
canvas = createCanvas(400, 400);- Creates a 400x400 pixel drawing surface and stores a reference to it in the canvas variable.
canvas.parent('sketch-container');- Moves the canvas element into the #sketch-container div in the HTML so CSS can center the canvas together with the sliders below it.
colorMode(HSB, 360, 100, 100);- Switches p5's color system from RGB to Hue-Saturation-Brightness, which makes it much easier to sweep through a rainbow of colors just by changing one hue number.
noFill();- Turns off shape fill so only the stroke (outline) of the curve is visible - important since beginShape()/vertex() would otherwise try to fill in the closed curve.
strokeWeight(2); // https://p5js.org/reference/#/p5/strokeWeight- Sets how thick the lines drawn by stroke() will be, in this case 2 pixels.
controlsContainer = select('#controls-container'); // https://p5js.org/reference/#/p5/select- Grabs a reference to the existing #controls-container div from index.html so new elements can be added inside it.
aSlider = createSlider(1, 10, 3, 1); // https://p5js.org/reference/#/p5/createSlider- Creates an HTML range slider for the A frequency, allowing whole number values from 1 to 10, starting at 3.
createControlRow('A frequency: ', aSlider); // Helper function to create labeled row- Calls the helper function to wrap the slider together with a text label and insert that row into the controls container.
phaseSlider = createSlider(0, TWO_PI, 0, 0.01);- Creates a slider for the phase offset that can take decimal values from 0 up to a full circle (TWO_PI radians), in fine 0.01 steps.
ratioText = createP(''); // https://p5js.org/reference/#/p5/createP- Creates an empty paragraph element that will later display the current A:B frequency ratio as text.