Buttons Tutorial

ProControls for p5.js — by David Stein
Step-by-step guide for beginners — all examples are live and editable
↗ Full Docs
Welcome! This tutorial covers button controls in ProControls — specifically the IconButton, which uses Material Design icons for a compact, intuitive interface. IconButtons are perfect for music apps, tools, and game interfaces where you need clickable controls that are small but highly recognizable. You do not need any prior experience — each example is fully explained, and every code block is live and editable. Click inside any code box, make a change, and the preview updates automatically.
First time here? Before running these examples in your own project, visit the Getting Started page to get the HTML setup and script tags you need to load ProControls.
IconButton XYPad GridPad Piano

IconButton

Button

A square button with a Material Design icon and optional toggle state. Perfect for compact interfaces — music apps, tools, playback controls, and game UIs. The button responds to clicks with onClick callback and can be toggled between active/inactive states.

Step 1 Your first IconButton

Create an IconButton with just a position and an icon name. The button uses Material Design icons — a huge library of recognizable symbols. Click the button and check the browser console to see the event fire. Browse available icons →

editable — changes update the preview
Step 2 Toggle state with active property

Add the toggle property to make the button switch between two states. When toggled, the button glows with the style's accent color. The active property sets the starting state — true means glowing at start, false means off.

editable — changes update the preview
Icon feedback: When toggle: true, clicking the button automatically flips active and fires the callback with the new state.
Step 3 Multiple buttons in a row

Arrange multiple IconButtons by offsetting their x positions. This pattern is common in music production interfaces (transport controls) and game UIs (action buttons). ProControls handles the click and keyboard events for each button independently.

editable — changes update the preview
Spacing tip: For a 48px icon button, add 64px between left edges to get even spacing with a 16px gap. Adjust size and spacing to suit your layout.
Step 4 Label and theme customization

Add a label below the button to identify its function. Customize the appearance with ControlStyle themes (like black, red, blue) to match your interface design. The label appears below the button.

editable — changes update the preview

XYPad

2-Axis Surface

An XYPad is a two-dimensional drag surface — like the touchpad on a synthesizer. You drag the crosshair anywhere inside the pad to set two values at once: one for the X axis (left–right) and one for the Y axis (up–down). It is great for controlling two parameters simultaneously, like filter cutoff and resonance, or panning and reverb depth. Double-click to reset the crosshair to its starting position.

Step 1 Your first XYPad

One line creates a working XYPad. Both axes default to a 0–1 range and the crosshair starts in the center. Drag it around and notice it tracks both directions at once!

editable — changes update the preview
Step 2 Customizing the pad

Use minX, maxX, minY, maxY to set independent value ranges for each axis. valueX and valueY set where the crosshair starts. crosshairColor changes the dot and line color — try any CSS color string.

editable — try changing the ranges or crosshairColor
Y axis convention: Like a musical graph, Y increases upward — dragging toward the top gives a higher value. This matches how musicians think about pitch and level.
Step 3 Spring-back — the crosshair snaps to center

Set springBack: true and the crosshair will animate back to its starting position every time you let go. This is perfect for controls that should "snap to neutral" like a pitch bend wheel or an effects send. springDuration controls how long the snap takes in seconds. Try dragging and releasing!

editable — drag and release to see the spring
Step 4 Reading both axes with onChange

The onChange callback receives two arguments: vx (the X value) and vy (the Y value), both passed together every time either axis changes. You can also listen to each axis separately using onChangeX and onChangeY. Drag the crosshair below and watch the console!

editable — drag the pad and watch the console
Tip: Read values any time in your draw() loop with pad.valueX and pad.valueY — no callback needed. You can also set them programmatically to move the crosshair: pad.valueX = 0.5; pad.valueY = 0.5;

GridPad

Cell Grid

A GridPad is a grid of interactive cells — think of a step sequencer on a drum machine, or the launch pads on a hardware controller. Each cell can behave in different ways depending on the mode you choose: cells can toggle on and off, hold a continuous value you fill by holding the mouse, or each display a chosen color from a palette.

Step 1 Your first grid

The default mode is 'toggle' — click any cell to turn it on (lit up) or off. The grid defaults to 4 rows and 4 columns. Try clicking around!

editable — click cells in the preview
Step 2 Sizing the grid and adding groups

rows and cols control how many cells there are. cellSize sets how big each cell is in pixels. hGroup and vGroup draw a subtle dividing line every N cells — useful for marking beats in a sequencer or organizing a larger grid into sections.

editable — try changing rows, cols, cellSize, or hGroup
Step 3 Choosing a mode

The mode option changes how cells behave. toggle — click to flip each cell on or off. percent — hold the left mouse button to raise a cell's value, right-click to lower it (good for velocity grids). colorselect — each cell cycles through a list of colours you define; the value returned is a label you assign, not the colour itself. Try editing mode below!

editable — change mode to 'toggle' or 'percent'

All three modes side by side:

'toggle'
'percent'
'colorselect'
Step 4 Reading the grid with onChange

The onChange callback receives the entire grid as a 2-D array — one row per row of cells, one value per column. In toggle mode the values are 0 or 1; in percent mode they are decimals from 0 to 1; in colorselect mode they are the value strings you defined. Click cells below to see the full grid printed to the console.

editable — click cells and watch the console
Tip: Read a single cell at any time with seq.getValue(row, col), or set one programmatically with seq.setValue(row, col, 1). Read the whole grid with seq.values — returns a deep copy of the 2-D array.

Piano

Note Selection

An interactive piano keyboard for selecting notes. Rendered with white and black keys, with support for highlighting chords or scales.

Basic Piano

Create a simple piano keyboard with default settings:

Custom Note Range

Start from a different note and adjust the range:

Highlight Chords

Pre-highlight notes to show chords or scales:

Respond to Changes

Listen to key state changes with the onChange and onRelease callbacks:

Update Highlighted Notes

Change highlighted notes dynamically: