A Menu is a menu bar that runs along one edge of the canvas, just like the
application menus you see in desktop software. Items are defined as a simple array of strings.
Any item can become a parent with a submenu by replacing the string with an array — the first
element is the label and the remaining elements are the submenu entries. Clicking a leaf item
fires onChange.
Create a Menu with an items array. By default the menu bar sits along the top
edge of the canvas. Click any item in the preview to see it highlighted — items with no
submenu are leaf items you can act on.
The orientation option controls which edge the bar sits on and which direction
submenus open. Use 'top' or 'bottom' for a horizontal bar, or
'left' or 'right' for a vertical sidebar. The bar automatically
snaps to the corresponding edge.
orientation:'right' automatically sticks the bar to the
right edge of the canvas, and 'bottom' sticks to the bottom — you don't need
to calculate the canvas size yourself.
Replace any string in items with an array to create a submenu. The first
element of the array becomes the parent label — the remaining elements are the submenu
entries. Click a parent item to open its submenu, then click a submenu entry to select it.
Submenus can be nested inside any orientation.
onChange
The onChange callback fires when the user clicks a leaf item (a non-parent
entry). It receives two arguments: label — the text of the clicked item, and
path — an array of labels from the root down to the selection. Use
path when you need to know which parent menu an item came from. Click items
below and watch the console!
menu.items at any time to update the menu
structure dynamically — useful for toggling checkmarks or enabling/disabling entries.
Set menu._layoutDirty = true after reassigning to trigger a redraw.
A PopupMenu is a floating vertical menu positioned at a specific canvas
coordinate — the classic context menu. It accepts exactly the same options as Menu but is
pre-configured with orientation: 'popup', so you never need to set it yourself.
Submenus automatically flip left or right depending on where the menu sits on the canvas,
so they always stay within the visible area.
Create a PopupMenu with x, y, and an items array.
It appears immediately as a floating vertical list. Click a parent item to open its submenu,
and notice how the submenu arrow and direction automatically adapt to the menu's position.
Set x and y to place the menu anywhere on the canvas. When the
menu sits in the right half of the canvas, submenus automatically open to the left so they
don't overflow off-screen — you never need to calculate this yourself.
Try moving x to a larger value to see the submenu flip direction.
onChange
The onChange callback works exactly the same as in Menu — it fires on every
leaf item click and receives (label, path). Use path to distinguish
items with the same label that appear in different submenus. Click items below!
The most common pattern is to create a PopupMenu at the current mouse position when the user
right-clicks. In p5.js you can do this inside mousePressed() by checking
mouseButton === RIGHT. Create a fresh menu each time — any previously open menu
is automatically replaced.
false from mousePressed in p5.js
prevents the browser's own right-click context menu from appearing over your canvas, giving
the ProControls menu the whole stage.