setup()
setup() runs once when the sketch starts and is your chance to initialize the canvas, set global properties like angleMode, and prepare interactive controls. The noLoop() call is a performance win—combined with slider.input(updateSketch), it means draw() only runs when you actually change something.
function setup() {
createCanvas(1000, 1000);
angleMode(DEGREES);
noFill();
rectMode(CENTER);
// Sliders
nPointsLabel = createElement('label', 'Number of Points: ');
nPointsLabel.position(10, 10);
nPointsSlider = createSlider(3, 60, 24, 1); // Min 3, max 60, default 24, step 1
nPointsSlider.position(nPointsLabel.width + 15, 10);
nPointsSlider.style('width', '200px');
nPointsSlider.input(updateSketch); // Call updateSketch when this slider changes
sqSideLengthLabel = createElement('label', 'Rosette Size: ');
sqSideLengthLabel.position(10, 40);
sqSideLengthSlider = createSlider(width / 8, width / 2, width / 4, 1); // Min ~125, max ~500, default ~250, step 1
sqSideLengthSlider.position(sqSideLengthLabel.width + 15, 40);
sqSideLengthSlider.style('width', '200px');
sqSideLengthSlider.input(updateSketch); // Call updateSketch when this slider changes
// Initial calculation and drawing
updateSketch();
noLoop(); // Draw once, then only redraw on slider input
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
angleMode(DEGREES);
Tells p5.js to measure all angles in degrees (0–360) instead of radians, making math more intuitive
nPointsSlider = createSlider(3, 60, 24, 1);
Creates a slider ranging 3–60 points, starting at 24, that triggers updateSketch() when dragged
sqSideLengthSlider = createSlider(width / 8, width / 2, width / 4, 1);
Creates a slider for rosette size from ~125 to ~500 pixels, starting at ~250, that triggers updateSketch() when dragged
noLoop(); // Draw once, then only redraw on slider input
Stops draw() from running every frame, saving CPU—it only runs when updateSketch() calls redraw()
createCanvas(1000, 1000);- Creates a 1000×1000 pixel square canvas, providing a symmetrical space for the rosette pattern
angleMode(DEGREES);- Sets all angle measurements (sin, cos, rotate, etc.) to use degrees 0–360 instead of radians 0–2π, making trigonometry more readable
noFill();- Disables fill for all shapes drawn after this, so only the stroked outlines of petals and stars will be visible
rectMode(CENTER);- Sets rectangle positioning mode to CENTER (not strictly used here, but prepared for potential rect() calls)
nPointsLabel = createElement('label', 'Number of Points: ');- Creates a text label above the first slider to describe what it controls
nPointsSlider = createSlider(3, 60, 24, 1);- Creates a slider with minimum 3, maximum 60, default value 24, and step size 1 for controlling petal count
nPointsSlider.input(updateSketch);- Binds the slider's input event to call updateSketch(), so the pattern redraws whenever the user drags this slider
sqSideLengthSlider = createSlider(width / 8, width / 2, width / 4, 1);- Creates a slider for rosette size: minimum 125 pixels (width/8), maximum 500 pixels (width/2), default 250 pixels (width/4)
updateSketch();- Calls updateSketch() once at startup to create the initial rosette objects and draw the pattern
noLoop();- Stops the draw loop from running continuously—draw() now only executes when updateSketch() explicitly calls redraw(), improving performance