setup()
setup() runs once when the sketch starts. It configures the canvas, creates DOM elements (sliders and labels), and calls updateSketch() to build the initial rosette. The noLoop() call is key: it makes the sketch render only on demand, not continuously, improving performance for a static geometric design.
function setup() {
createCanvas(1000, 1000);
angleMode(DEGREES);
noFill();
rectMode(CENTER);
// Sliders
nPointsLabel = createElement('label', 'Number of Points: ');
nPointsLabel.position(10, 10);
nPointsSlider = createSlider(8, 60, 24, 4);
nPointsSlider.position(nPointsLabel.width + 15, 10);
nPointsSlider.style('width', '200px');
nPointsSlider.input(updateSketch);
sqSideLengthLabel = createElement('label', 'Rosette Size: ');
sqSideLengthLabel.position(10, 40);
sqSideLengthSlider = createSlider(width / 16, width / 2, width / 4, 1);
sqSideLengthSlider.position(sqSideLengthLabel.width + 15, 40);
sqSideLengthSlider.style('width', '200px');
sqSideLengthSlider.input(updateSketch);
// Initial calculation and drawing
updateSketch();
noLoop();
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(1000, 1000);
Creates a 1000×1000 pixel canvas for drawing the rosette
angleMode(DEGREES);
Switches p5.js angle functions to use degrees instead of radians, making geometric calculations more intuitive
nPointsSlider = createSlider(8, 60, 24, 4);
Creates the first slider to control the number of rosette points from 8 to 60
noLoop();
Stops the draw loop from running continuously; canvas only redraws when updateSketch() calls redraw()
createCanvas(1000, 1000);- Creates a square 1000×1000 pixel canvas where the rosette will be drawn
angleMode(DEGREES);- Tells p5.js to interpret all angle arguments (sin, cos, rotate) as degrees (0–360) instead of radians (0–2π), matching Islamic geometry conventions
noFill();- Disables fill for all shapes, so only outlines (strokes) will be visible
rectMode(CENTER);- Sets rectangle positioning mode to CENTER (though rectangles aren't used here, this line may be leftover from an earlier version)
nPointsLabel = createElement('label', 'Number of Points: ');- Creates a text label in the HTML DOM for the first slider
nPointsSlider = createSlider(8, 60, 24, 4);- Creates the first slider with minimum 8, maximum 60, default value 24, and step size 4
nPointsSlider.input(updateSketch);- Registers updateSketch() to be called whenever the user moves this slider
sqSideLengthSlider = createSlider(width / 16, width / 2, width / 4, 1);- Creates the second slider controlling rosette size: min ~62 pixels, max ~500, default ~250, step 1
updateSketch();- Calls updateSketch() once to initialize the rosette with default slider values
noLoop();- Disables the automatic 60-fps draw loop; instead, redraw() will only be called manually when updateSketch() triggers it