Pre-Release — API subject to change

Selectors

Part of the ProControls for p5.js documentation.

New here? See the Getting Started page for HTML setup, script tags, a minimal sketch, and the common options table.

Selectors Switch Selector TagSelector SliderSelector

Selectors Input

Switch Input

An illuminated discrete selector. Two states renders as a rocker toggle — click to flip. Three or more states renders as a vertical button stack — click any slot to select it. The active slot glows with the style's accent color. Double-click any slot or the label to reset to the initial state.

new Switch(opts)

Options

OptionTypeDefaultDescription
statesstring[]['OFF','ON']Array of state labels. 2 entries = rocker. 3+ entries = selector stack.
statenumber0Index of the initially active state.
springDefaultnumberstateState index to restore on double-click reset. Defaults to state. Useful when the control is rebuilt dynamically and you want reset to always target a fixed index.
widthnumber48Panel width in pixels.
heightnumberautoDefaults to 70 for 2-state; states.length × 26 + 20 for N-state.
onChangefunctionnullonChange({index, label}[, control]) — called on every click.
onReleasefunctionnullonRelease({index, label}[, control]) — called on mouse/touch release.

Reading state

sw.state            // active index (readable/writable)
sw.states[sw.state]  // active label string

Two-state rocker

editable — changes update the preview

N-state selector

editable — changes update the preview

Selector Input

A multi-style option selector. Set style to choose the visual style. Scroll wheel and touch work for all styles. Double-click the selection display or the label to reset to the initial option (double-clicking the arrow buttons or gear wheel advances state as normal).

new Selector(opts)

Options

OptionTypeDefaultDescription
optionsstring[]['A','B','C']Array of option labels.
statenumber0Initial selected index into options.
stylestring'rotary'Visual style — 'rotary' or 'arrow'. See style descriptions below.
widthnumber140Total panel width in pixels.
heightnumber40Panel height in pixels.
onChangefunctionnullonChange({index, label}[, control]) — called whenever the selection changes.
onReleasefunctionnullonRelease({index, label}[, control]) — called on mouse/touch release.
springBackbooleanfalseWhen true, the selection snaps back to its initial state after springDuration seconds.
springDurationnumber1.0Seconds before snapping back when springBack is enabled.
labelstring''Caption drawn below the panel.
disabledbooleanfalseDisables interaction and renders a translucent overlay.
themeobject{}Partial theme override. See Themes.

Properties

PropertyDescription
.stateCurrent selected index (read/write).
.optionsArray of option labels (read/write).

Style — rotary (default)

A mechanical drum-style selector inspired by vintage rotary counters. A display window shows the selected option; the drum's serrated edge is exposed on the right — horizontal ridges scroll as the drum rotates, giving a tactile feel. Adjacent options are visible while scrolling. Drag up/down, scroll wheel, or touch.

Teeth column sizing: width is max(14, round(height × 0.28)) px. The display window occupies the remaining panel width to the left.
editable — changes update the preview

Style — arrow

A compact left/right arrow selector. Pressing either arrow button steps to the adjacent option; the new selection slides into the display window from the direction of travel. Scroll wheel also works.

editable — changes update the preview

TagSelector Input

A multi-select chip panel. Each string in words is displayed as a rounded pill inside a box. Click any pill to toggle its selection; multiple pills can be selected simultaneously. Double-click the label to reset to the initial selection.

new TagSelector(opts)

Options

OptionTypeDefaultDescription
wordsstring[][]Array of strings to display as selectable chips. Each string may contain spaces (multi-word phrases are treated as one chip).
selectedstring[][]Initially selected words. Must be members of words.
widthnumber200Panel width in pixels. Chips wrap to new rows when they exceed this width.
heightnumberautoPanel height. Omit to auto-size to fit all chips. Provide a fixed value to enable vertical scrolling when chips overflow — a thin scrollbar appears automatically and mouse-wheel or drag scrolls the content.
labelstring''Caption shown at the bottom of the panel. Double-click it to reset selection.
onChangefunctionnullonChange({selected, added, removed}[, control]) — called on each toggle. Exactly one of added / removed is non-null per click; both are null on a label double-click reset.
onReleasefunctionnullonRelease({selected, added: null, removed: null}[, control]) — called on mouse/touch release.
disabledbooleanfalseDisables interaction and renders a translucent overlay.
themeobject{}Partial theme override. See Themes.

Properties

PropertyDescription
.selectedGetter — returns current selection as string[]. Setter accepts a string[] to programmatically set selection.
.wordsArray of all chip strings (read/write). Reassigning marks layout dirty so chips reflow on next draw.

Example

editable — changes update the preview

SliderSelector Input

A vertical fader that snaps to discrete string options. It looks and behaves like an AnalogSlider — same panel, track, and brushed-metal cap — but instead of a continuous numeric range the slider clicks into one of the provided option strings. Tick marks at each position are drawn to the right of the track, and the active option's tick and label are highlighted in the accent colour. Scroll-wheel steps through options when hovering. Double-click the cap to reset to the initial state.

new SliderSelector(opts)

Options

OptionTypeDefaultDescription
optionsstring[][]The ordered list of selectable strings. Index 0 appears at the top of the track, the last index at the bottom.
statenumber0Initial selected index.
stylestring'knob''knob' — brushed-metal rectangular fader cap. 'button' — circular cap with a radial gradient, matching the AnalogSlider button style.
x, ynumber20, 20Canvas position.
widthnumberautoPanel width. Defaults to a size that fits the longest option label with padding.
heightnumberautoPanel height. Defaults to enough vertical space for all options at 22 px spacing (min 80 px).
labelstring''Caption rendered at the top of the panel.
onChangefunctionnullonChange({index, label}[, control]) — called whenever the selection changes during drag or scroll.
onReleasefunctionnullonRelease({index, label}[, control]) — called on mouse release.
disabledbooleanfalseDisables interaction and renders a translucent overlay.
themeobject{}Partial theme override. See Themes.

Properties

PropertyDescription
.stateCurrent selected index (read/write).
.valueGetter — returns options[state] as a string. Setter accepts a string and updates state to its index (no-op if not found).
.optionsThe options array (read/write). Reassign to change the available choices; update .state if needed.

Interaction

GestureEffect
Drag capMoves the cap; snaps to the nearest option tick as you drag.
Double-click capReset to the initial state. Fires onChange and onRelease if the state changes.
Scroll wheelSteps one option up or down when hovering over the control.

Example

editable — changes update the preview