Designed for p5.js 2.x
<script
src="https://cdn.jsdelivr.net/npm/p5-phone@1.13.0/dist/p5-phone.min.js">
</script>
Illustration by Angela Torchio
Use p5.js, load p5-phone, lock gestures, then enable the capability your sketch needs from a user tap.
<script
src="https://cdn.jsdelivr.net/npm/p5-phone@1.13.0/dist/p5-phone.min.js">
</script>
function setup() {
createCanvas(windowWidth, windowHeight);
lockGestures();
enableGyroTap('Tap to enable motion sensors');
}
function draw() {
background(245);
textAlign(CENTER, CENTER);
if (window.sensorsEnabled) {
circle(width / 2 + rotationY * 3, height / 2 + rotationX * 3, 60);
} else {
text('Tap to enable motion sensors', width / 2, height / 2);
}
}
if (window.sensorsEnabled) { ... }, if (window.micEnabled) { ... }, or the matching status flag for the capability you enabled.
if (window.sensorsEnabled) {
circle(width / 2 + rotationY * 3, height / 2 + rotationX * 3, 60);
}
if (window.micEnabled) {
let level = mic.getLevel();
circle(width / 2, height / 2, level * 500);
}
function mousePressed() {
if (window.soundEnabled) {
mySound.play();
}
return false;
}
if (window.nfcEnabled) {
if (isNfcTag('shirt')) {
drawShirtEffect();
}
}
function setup() {
bleSetup({
characteristics: [
{ name: 'temp', type: 'float', notify: true }
]
});
enableBleTap();
}
function bleReceive(name, value) {
debug(name + ' = ' + value);
}
function mousePressed() {
if (window.vibrationEnabled) {
vibrate(50);
}
return false;
}
if (cam.ready) {
cam.draw();
}
enablePermissionsTap(['sensors', 'mic'], 'Tap to enable motion + microphone');
if (window.sensorsEnabled && window.micEnabled) {
circle(
width / 2 + rotationY * 3,
height / 2 + rotationX * 3,
80 + mic.getLevel() * 400
);
}
mousePressed(), mouseDragged(), and mouseReleased() for touch or pointer input. The older touchStarted(), touchMoved(), and touchEnded() callbacks are p5.js 1.x only.
Optional skills that teach AI coding assistants how p5-phone and p5.js 2.x work, so generated sketches use the right APIs and contemporary patterns. See the full guide for installing them for your particular workflow.
This agent skill contains the knowledge of how the library works, implementation patterns, and commands to simplify the creation of applications built with this library.
While the main library has been optimized for p5 2.x, many models are trained on p5 1.x methods. This skill helps bridge the transition by pointing development to contemporary methods.
Every capability (enableSensor*, enableMic*, enablePermissions*, …) offers the same six unlock screens. Only the enable* line changes — the rest of the sketch is identical. Pick the look you want for the same simple tilt-a-ball sketch below.
function setup() {
createCanvas(windowWidth, windowHeight);
lockGestures();
enableSensorCanvas('Tap the canvas to begin'); // <-- the only line that changes
}
function draw() {
background(20);
if (!window.sensorsEnabled) return; // gate all hardware reads
fill(80, 200, 255);
noStroke();
circle(width / 2 + rotationY * 3, height / 2 + rotationX * 3, 60);
fill(255);
textAlign(CENTER, TOP);
textSize(16);
text('Tilt your phone to move the ball', width / 2, 30);
}
Full-screen dark overlay with a centered frosted-glass message box.
enableSensorTap(
'Tap to enable motion sensors'
);
A centered gradient button (plus optional status text) over the idle canvas.
enableSensorButton(
'ENABLE MOTION SENSORS'
);
No overlay — a text hint is drawn on the canvas until the first touch.
enableSensorCanvas(
'Tap the canvas to begin'
);
A slim slide-in bar pinned to the top or bottom of the screen.
enableSensorBanner(
'Tap to enable motion sensors',
'bottom'
);
Bare semi-transparent full-screen tint with a small radiating icon. Color, opacity, and icon are adjustable.
enableSensorMinimal({
color: '#0a0',
opacity: 0.4,
iconColor: '#fff'
});
No built-in UI — bind activation to an HTML element you add yourself.
// add to index.html:
// <button id="start-btn">Start</button>
enableSensorOn('#start-btn');
enableMic*, enableSound*, enablePermissions*, etc. variant. For styles 1–5 nothing else is required; style 6 needs a target element in your index.html. Add showDesktopQr() in setup() to get a floating scan-to-test QR on desktop only (no-op on mobile).
Grouped by the job you are trying to do. The permission table keeps the six UI styles visible, and each category lists the p5 APIs that become practical after p5-phone handles the mobile browser setup.
Reordered around starter sketches, input, output, ML5, and lower-priority comparison material. Each card has room for a full example, minimal version, Web Editor link, source link, and QR code.
Plan mobile sketches by platform first. Browser permissions and hardware support matter more than the p5.js version for most phone behavior.
mousePressed(), mouseDragged(), and mouseReleased() for touch-compatible sketches.vibrate().| Capability | iOS Safari | Android Chrome | Desktop browsers |
|---|---|---|---|
| Motion sensors | Supported after a tap permission flow. | Supported; permission request is usually a no-op. | Limited or unavailable on most laptops and desktops. |
| Touch input | Supported through touch and pointer events. | Supported through touch and pointer events. | Use mouse or pointer callbacks for testing. |
| Microphone and sound | Supported after user gesture and permission. | Supported after user gesture and permission. | Supported after browser permission. |
| Speech recognition | Browser support varies; test on the target device. | Supported in Chrome where Web Speech is available. | Browser support varies. |
| NFC | Not supported. | Supported in Chrome on HTTPS. | Not supported. |
| GPS / geolocation | Supported on HTTPS after a tap permission. | Supported on HTTPS after a tap permission. | Supported after browser permission. |
| Camera and ML5 | Supported after camera permission. | Supported after camera permission. | Supported after camera permission. |
| Vibration | Not supported. | Supported on many phones. | Limited or unavailable. |