createLabeledSlider()
This helper wraps p5.js's createDiv/createSpan/createSlider DOM functions to avoid repeating the same five lines three times in setup(). It's a great example of how you can build your own reusable UI-building functions on top of p5.js's DOM API.
function createLabeledSlider(labelText, min, max, initial, step) {
const group = createDiv();
group.parent('controls');
group.addClass('control-group');
const label = createSpan(labelText);
label.parent(group);
label.addClass('control-label');
const slider = createSlider(min, max, initial, step);
slider.parent(group);
slider.addClass('slider');
return slider;
}
Line-by-line explanation (6 lines)
const group = createDiv();- Creates a new empty <div> element to hold a label and slider together as one visual group.
group.parent('controls');- Attaches the group div inside the HTML element with id 'controls', which is styled as the floating pill-shaped control bar.
group.addClass('control-group');- Adds a CSS class so the group gets the flexbox styling defined in style.css.
const label = createSpan(labelText);- Creates a text label (like 'X freq') using the string passed into this function.
const slider = createSlider(min, max, initial, step);- Creates an HTML range slider with the given minimum, maximum, starting value, and step increment.
return slider;- Returns the slider element so the caller can store it in a variable and later read its .value().