Menus Tutorial

ProControls for p5.js — by David Stein
Menu and PopupMenu — step-by-step for beginners — all examples live and editable
↗ Full Docs
Welcome! This tutorial covers the two menu controls in ProControls — the Menu (a full menu bar that can be placed along any edge of the canvas) and the PopupMenu (a floating context menu that appears at a specific position). Each control is explained in four steps, from a single line of code up to real-world usage with submenus and callbacks. Every code block is live and editable — click inside one, make a change, and the preview updates automatically.
First time here? Visit Getting Started for the HTML setup and script tags needed before running these examples in your own project.
Menu PopupMenu

PopupMenu

Context Menu

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.

Step 1 Your first PopupMenu

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.

editable — click items to open submenus
Step 2 Positioning — auto-flip on the right half

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.

editable — try x:160 to see submenus flip left
How it works: ProControls checks whether the menu's horizontal centre is in the left or right half of the canvas. If it's in the right half, all submenus open leftward. This matches the behaviour of native OS context menus.
Step 3 Reading selections with 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!

editable — click a leaf item and watch the console
Step 4 Context menu pattern — show at mouse position

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.

editable — right-click anywhere in the preview canvas
Tip: Returning 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.