p5-phone 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +509 -0
  3. package/dist/p5-phone.js +1062 -0
  4. package/dist/p5-phone.min.js +10 -0
  5. package/examples/Phone Sensor Examples/microphone/01_mic_level/index.html +19 -0
  6. package/examples/Phone Sensor Examples/microphone/01_mic_level/sketch.js +117 -0
  7. package/examples/Phone Sensor Examples/movement/01_orientation_basic/index.html +28 -0
  8. package/examples/Phone Sensor Examples/movement/01_orientation_basic/sketch.js +123 -0
  9. package/examples/Phone Sensor Examples/movement/02_rotational_velocity/index.html +28 -0
  10. package/examples/Phone Sensor Examples/movement/02_rotational_velocity/sketch.js +144 -0
  11. package/examples/Phone Sensor Examples/movement/03_acceleration/index.html +28 -0
  12. package/examples/Phone Sensor Examples/movement/03_acceleration/sketch.js +87 -0
  13. package/examples/Phone Sensor Examples/sound/01_dual_audio/index.html +31 -0
  14. package/examples/Phone Sensor Examples/sound/01_dual_audio/sketch.js +225 -0
  15. package/examples/Phone Sensor Examples/sound/01_dual_audio/tracks/audio1.mp3 +0 -0
  16. package/examples/Phone Sensor Examples/sound/01_dual_audio/tracks/audio2.mp3 +0 -0
  17. package/examples/Phone Sensor Examples/sound/02_volume_touches/index.html +31 -0
  18. package/examples/Phone Sensor Examples/sound/02_volume_touches/sketch.js +269 -0
  19. package/examples/Phone Sensor Examples/sound/02_volume_touches/tracks/audio1.mp3 +0 -0
  20. package/examples/Phone Sensor Examples/touch/01_touch_basic/index.html +28 -0
  21. package/examples/Phone Sensor Examples/touch/01_touch_basic/sketch.js +94 -0
  22. package/examples/Phone Sensor Examples/touch/02_touch_zones/index.html +28 -0
  23. package/examples/Phone Sensor Examples/touch/02_touch_zones/sketch.js +220 -0
  24. package/examples/Phone Sensor Examples/touch/03_touch_count/index.html +28 -0
  25. package/examples/Phone Sensor Examples/touch/03_touch_count/sketch.js +93 -0
  26. package/examples/Phone Sensor Examples/touch/04_touch_distance/index.html +28 -0
  27. package/examples/Phone Sensor Examples/touch/04_touch_distance/sketch.js +120 -0
  28. package/examples/Phone Sensor Examples/touch/05_touch_angle/index.html +28 -0
  29. package/examples/Phone Sensor Examples/touch/05_touch_angle/sketch.js +117 -0
  30. package/examples/Phone Sensor Examples - Minimal/microphone/01_mic_level/index.html +19 -0
  31. package/examples/Phone Sensor Examples - Minimal/microphone/01_mic_level/sketch.js +72 -0
  32. package/examples/Phone Sensor Examples - Minimal/movement/01_orientation_basic/index.html +18 -0
  33. package/examples/Phone Sensor Examples - Minimal/movement/01_orientation_basic/sketch.js +51 -0
  34. package/examples/Phone Sensor Examples - Minimal/movement/02_rotational_velocity/index.html +18 -0
  35. package/examples/Phone Sensor Examples - Minimal/movement/02_rotational_velocity/sketch.js +70 -0
  36. package/examples/Phone Sensor Examples - Minimal/movement/03_acceleration/index.html +18 -0
  37. package/examples/Phone Sensor Examples - Minimal/movement/03_acceleration/sketch.js +83 -0
  38. package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/index.html +19 -0
  39. package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/sketch.js +96 -0
  40. package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/tracks/audio1.mp3 +0 -0
  41. package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/index.html +19 -0
  42. package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/sketch.js +118 -0
  43. package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/tracks/audio1.mp3 +0 -0
  44. package/examples/Phone Sensor Examples - Minimal/touch/01_touch_basic/index.html +18 -0
  45. package/examples/Phone Sensor Examples - Minimal/touch/01_touch_basic/sketch.js +58 -0
  46. package/examples/Phone Sensor Examples - Minimal/touch/02_touch_zones/index.html +18 -0
  47. package/examples/Phone Sensor Examples - Minimal/touch/02_touch_zones/sketch.js +78 -0
  48. package/examples/Phone Sensor Examples - Minimal/touch/03_touch_count/index.html +18 -0
  49. package/examples/Phone Sensor Examples - Minimal/touch/03_touch_count/sketch.js +64 -0
  50. package/examples/Phone Sensor Examples - Minimal/touch/04_touch_distance/index.html +18 -0
  51. package/examples/Phone Sensor Examples - Minimal/touch/04_touch_distance/sketch.js +69 -0
  52. package/examples/Phone Sensor Examples - Minimal/touch/05_touch_angle/index.html +18 -0
  53. package/examples/Phone Sensor Examples - Minimal/touch/05_touch_angle/sketch.js +85 -0
  54. package/examples/Phone and Gif/collision/README.md +31 -0
  55. Gif/collision/gifs/spaceSuit2.png +0 -0
  56. package/examples/Phone and Gif/collision/index.html +19 -0
  57. package/examples/Phone and Gif/collision/sketch.js +226 -0
  58. Gif/fetch/gifs/corgiswimflip.gif +0 -0
  59. package/examples/Phone and Gif/fetch/index.html +18 -0
  60. package/examples/Phone and Gif/fetch/sketch.js +139 -0
  61. Gif/fly/gifs/comparison.gif +0 -0
  62. package/examples/Phone and Gif/fly/index.html +18 -0
  63. package/examples/Phone and Gif/fly/sketch.js +103 -0
  64. Gif/roll/gifs/how-penciles-are-made.gif +0 -0
  65. package/examples/Phone and Gif/roll/index.html +18 -0
  66. package/examples/Phone and Gif/roll/sketch.js +121 -0
  67. package/examples/UXcompare/button-vs-movement/index.html +45 -0
  68. package/examples/UXcompare/button-vs-movement/sketch.js +355 -0
  69. package/examples/UXcompare/button-vs-orientation/index.html +25 -0
  70. package/examples/UXcompare/button-vs-orientation/sketch.js +317 -0
  71. package/examples/UXcompare/button-vs-shake/index.html +45 -0
  72. package/examples/UXcompare/button-vs-shake/sketch.js +320 -0
  73. package/examples/UXcompare/gyroscope-demo/index.html +78 -0
  74. package/examples/UXcompare/gyroscope-demo/sketch.js +166 -0
  75. package/examples/UXcompare/index.html +419 -0
  76. package/examples/UXcompare/microphone-demo/index.html +79 -0
  77. package/examples/UXcompare/microphone-demo/sketch.js +210 -0
  78. package/examples/UXcompare/slider-vs-angle/index.html +25 -0
  79. package/examples/UXcompare/slider-vs-angle/sketch.js +429 -0
  80. package/examples/UXcompare/slider-vs-distance/index.html +25 -0
  81. package/examples/UXcompare/slider-vs-distance/sketch.js +401 -0
  82. package/examples/UXcompare/slider-vs-microphone/index.html +26 -0
  83. package/examples/UXcompare/slider-vs-microphone/sketch.js +336 -0
  84. package/examples/UXcompare/slider-vs-touches/index.html +25 -0
  85. package/examples/UXcompare/slider-vs-touches/sketch.js +376 -0
  86. package/examples/UXcompare/sliders-vs-acceleration/index.html +25 -0
  87. package/examples/UXcompare/sliders-vs-acceleration/sketch.js +361 -0
  88. package/examples/UXcompare/sliders-vs-rotation/index.html +25 -0
  89. package/examples/UXcompare/sliders-vs-rotation/sketch.js +375 -0
  90. package/examples/blankTemplate/index.html +31 -0
  91. package/examples/blankTemplate/sketch.js +55 -0
  92. package/examples/homepage/index.html +506 -0
  93. package/package.json +73 -0
  94. package/src/p5-phone.js +1062 -0
  95. package/src/permissionMic.js +240 -0
  96. package/src/permissionsGesture.js +213 -0
  97. package/src/permissionsGyro.js +246 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Digital Futures OCADU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,509 @@
1
+ # p5-phone
2
+ ![P5Phone.png](P5Phone.png)
3
+
4
+ **Simplified mobile hardware access for p5.js** - handle sensors, microphone, touch, and browser gestures with ease.
5
+
6
+ ## [Link for Interactive Examples](https://digitalfuturesocadu.github.io/P5-Phone-Interactions/examples/homepage)
7
+
8
+ # Overview
9
+ P5.js on mobile provides unique opportunities and challenges. The main P5 framework does an excellent job of making it easy to read data from various phone inputs and sensors, however it doesn't deal with the realities of contemporary browser's built in gestures and security protocols.
10
+ That's where this library comes in:
11
+
12
+ - Simplifies accessing phone hardware from the browser (accelerometers, gyroscopes, microphone)
13
+ - Simplifies disabling default phone gestures (Zoom, refresh, back, etc)
14
+ - Simplifies enabling audio output
15
+ - Simplifies using an on-screen console to display errors and debug info
16
+
17
+ ## p5 commands
18
+
19
+ This library simplifies access to the following p5.js mobile sensor and audio commands:
20
+
21
+ **Touch Events:**
22
+ - [`touchStarted()`](https://p5js.org/reference/p5/touchStarted/) - Called when a touch begins
23
+ - [`touchEnded()`](https://p5js.org/reference/p5/touchEnded/) - Called when a touch ends
24
+
25
+ **Device Motion & Orientation:**
26
+ - [`rotationX`](https://p5js.org/reference/p5/rotationX/) - Device tilt forward/backward
27
+ - [`rotationY`](https://p5js.org/reference/p5/rotationY/) - Device tilt left/right
28
+ - [`rotationZ`](https://p5js.org/reference/p5/rotationZ/) - Device rotation around screen
29
+ - [`accelerationX`](https://p5js.org/reference/p5/accelerationX/) - Acceleration left/right
30
+ - [`accelerationY`](https://p5js.org/reference/p5/accelerationY/) - Acceleration up/down
31
+ - [`accelerationZ`](https://p5js.org/reference/p5/accelerationZ/) - Acceleration forward/back
32
+ - [`deviceShaken()`](https://p5js.org/reference/p5/deviceShaken/) - Shake detection event
33
+ - [`deviceMoved()`](https://p5js.org/reference/p5/deviceMoved/) - Movement detection event
34
+ - [`setShakeThreshold()`](https://p5js.org/reference/p5/setShakeThreshold/) - Set shake detection sensitivity
35
+ - [`setMoveThreshold()`](https://p5js.org/reference/p5/setMoveThreshold/) - Set movement detection sensitivity
36
+
37
+ **Audio Input (requires p5.sound):**
38
+ - [`p5.AudioIn()`](https://p5js.org/reference/p5.sound/p5.AudioIn/) - Audio input object
39
+ - [`getLevel()`](https://p5js.org/reference/p5.sound/p5.AudioIn/getLevel/) - Current audio input level
40
+
41
+ ## Browser Compatibility
42
+
43
+ - iOS 13+ (Safari)
44
+ - Android 7+ (Chrome)
45
+ - Chrome 80+
46
+ - Safari 13+
47
+ - Firefox 75+
48
+
49
+ ## Table of Contents
50
+
51
+ - [Link for Interactive Examples](#link-for-interactive-examples)
52
+ - [Browser Compatibility](#browser-compatibility)
53
+ - [CDN (Recommended)](#cdn-recommended)
54
+ - [Basic Setup](#basic-setup)
55
+ - [Index HTML](#index-html)
56
+ - [p5.js](#p5js)
57
+ - [API Reference](#api-reference)
58
+ - [Core Functions](#core-functions)
59
+ - [Status Variables](#status-variables)
60
+ - [lockGestures()](#lockgestures)
61
+ - [Motion Sensor Activation](#motion-sensor-activation)
62
+ - [Microphone Activation](#microphone-activation)
63
+ - [Sound Output Activation](#sound-output-activation)
64
+ - [Debug System](#debug-system)
65
+
66
+ ### CDN (Recommended)
67
+
68
+ ```html
69
+ <!-- Minified version (recommended) -->
70
+ <script src="https://cdn.jsdelivr.net/npm/p5-phone@1.4.4/dist/p5-phone.min.js"></script>
71
+
72
+ <!-- Development version (larger, with comments) -->
73
+ <!-- <script src="https://cdn.jsdelivr.net/npm/p5-phone@1.4.4/dist/p5-phone.js"></script> -->
74
+ ```
75
+
76
+ ### Basic Setup
77
+
78
+ #### Index HTML
79
+
80
+ ```html
81
+ <!DOCTYPE html>
82
+ <html lang="en">
83
+ <head>
84
+ <meta charset="UTF-8">
85
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
86
+ <title>Mobile p5.js App</title>
87
+
88
+ <!-- Basic CSS to remove browser defaults and align canvas -->
89
+ <style>
90
+ body {
91
+ margin: 0;
92
+ padding: 0;
93
+ overflow: hidden;
94
+ }
95
+ </style>
96
+
97
+ <!-- Load p5.js library -->
98
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.10/p5.min.js"></script>
99
+
100
+ <!-- Load p5-phone library -->
101
+ <script src="https://cdn.jsdelivr.net/npm/p5-phone@1.4.4/dist/p5-phone.min.js"></script>
102
+
103
+ </head>
104
+ <body>
105
+ <!-- Load the p5.js sketch -->
106
+ <script src="sketch.js"></script>
107
+ </body>
108
+ </html>
109
+ ```
110
+
111
+ #### p5.js
112
+
113
+ ```javascript
114
+ let mic;
115
+ let mySound;
116
+
117
+ function preload() {
118
+ // Load sound file if needed
119
+ // mySound = loadSound('assets/sound.mp3');
120
+ }
121
+
122
+ function setup() {
123
+ // Show debug panel FIRST to catch setup errors
124
+ showDebug();
125
+
126
+ createCanvas(windowWidth, windowHeight);
127
+
128
+ // Lock mobile gestures to prevent browser interference
129
+ lockGestures();
130
+
131
+ // Enable motion sensors with tap-to-start
132
+ enableGyroTap('Tap to enable motion sensors');
133
+
134
+ // Enable microphone with tap-to-start (also enables sound output)
135
+ mic = new p5.AudioIn();
136
+ enableMicTap('Tap to enable microphone');
137
+
138
+ // OR enable sound output only (no microphone input)
139
+ // enableSoundTap('Tap to enable sound');
140
+ }
141
+
142
+ function draw() {
143
+ background(220);
144
+
145
+ // Always check status before using hardware features
146
+ if (window.sensorsEnabled) {
147
+ // Use device rotation and acceleration
148
+ fill(255, 0, 0);
149
+ circle(width/2 + rotationY * 5, height/2 + rotationX * 5, 50);
150
+ }
151
+
152
+ if (window.micEnabled) {
153
+ // Use microphone input
154
+ let level = mic.getLevel();
155
+ fill(0, 255, 0);
156
+ rect(10, 10, level * 200, 20);
157
+ }
158
+
159
+ if (window.soundEnabled) {
160
+ // Safe to play sounds
161
+ // mySound.play();
162
+ }
163
+ }
164
+
165
+ // Prevent default touch behavior (optional but recommended)
166
+ function touchStarted() {
167
+ return false;
168
+ }
169
+
170
+ function touchEnded() {
171
+ return false;
172
+ }
173
+ ```
174
+
175
+ ## API Reference
176
+
177
+ ### Core Functions
178
+
179
+ ```javascript
180
+ // Essential mobile setup
181
+ lockGestures() // Prevent browser gestures (call in setup())
182
+
183
+ // Motion sensor activation
184
+ enableGyroTap(message) // Tap anywhere to enable sensors
185
+ enableGyroButton(text) // Button-based sensor activation
186
+
187
+ // Microphone activation
188
+ enableMicTap(message) // Tap anywhere to enable microphone
189
+ enableMicButton(text) // Button-based microphone activation
190
+
191
+ // Sound output activation (no microphone input)
192
+ enableSoundTap(message) // Tap anywhere to enable sound playback
193
+ enableSoundButton(text) // Button-based sound activation
194
+
195
+ // Status variables (check these in your code)
196
+ window.sensorsEnabled // Boolean: true when motion sensors are active
197
+ window.micEnabled // Boolean: true when microphone is active
198
+ window.soundEnabled // Boolean: true when sound output is active
199
+
200
+ // Debug system (enhanced in v1.4.0)
201
+ showDebug() // Show on-screen debug panel with automatic error catching
202
+ hideDebug() // Hide debug panel
203
+ toggleDebug() // Toggle panel visibility
204
+ debug(...args) // Console.log with on-screen display and timestamps
205
+ debugError(...args) // Display errors with red styling
206
+ debugWarn(...args) // Display warnings with yellow styling
207
+ debug.clear() // Clear debug messages
208
+ ```
209
+
210
+ **p5.js Namespace Support**: All functions are also available as `p5.prototype` methods:
211
+ ```javascript
212
+ // You can use either syntax:
213
+ lockGestures(); // Global function (recommended)
214
+ this.lockGestures(); // p5.js instance method
215
+
216
+ // Both approaches work identically
217
+ enableGyroTap('Tap to start');
218
+ this.enableGyroTap('Tap to start');
219
+ ```
220
+
221
+ ### Status Variables
222
+
223
+ **Purpose:** Check whether permissions have been granted and sensors are active.
224
+
225
+ **Variables:**
226
+ - `window.sensorsEnabled` - Boolean indicating if motion sensors are active
227
+ - `window.micEnabled` - Boolean indicating if microphone is active
228
+ - `window.soundEnabled` - Boolean indicating if sound output is active
229
+
230
+ **Usage:**
231
+ ```javascript
232
+ function draw() {
233
+ // Always check before using sensor data
234
+ if (window.sensorsEnabled) {
235
+ // Safe to use rotationX, rotationY, accelerationX, etc.
236
+ let tilt = rotationX;
237
+ }
238
+
239
+ if (window.micEnabled) {
240
+ // Safe to use microphone
241
+ let audioLevel = mic.getLevel();
242
+ }
243
+
244
+ if (window.soundEnabled) {
245
+ // Safe to play sounds
246
+ mySound.play();
247
+ }
248
+ }
249
+
250
+ // You can also use them for conditional UI
251
+ function setup() {
252
+ enableGyroTap('Tap to enable motion');
253
+
254
+ // Show different instructions based on status
255
+ if (!window.sensorsEnabled) {
256
+ debug("Motion sensors not yet enabled");
257
+ }
258
+ }
259
+ ```
260
+
261
+ ### lockGestures()
262
+
263
+ **Purpose:** Prevents unwanted mobile browser gestures that can interfere with your p5.js app.
264
+
265
+ **When to use:** Call once in your `setup()` function after creating the canvas.
266
+
267
+ **What it blocks:**
268
+ - **Pinch-to-zoom** - Prevents users from accidentally zooming the page
269
+ - **Pull-to-refresh** - Stops the browser refresh gesture when pulling down
270
+ - **Swipe navigation** - Disables back/forward swipe gestures
271
+ - **Long-press context menus** - Prevents copy/paste menus from appearing
272
+ - **Text selection** - Stops accidental text highlighting on touch and hold
273
+ - **Double-tap zoom** - Eliminates double-tap to zoom behavior
274
+
275
+ ```javascript
276
+ function setup() {
277
+ createCanvas(windowWidth, windowHeight);
278
+ lockGestures(); // Essential for smooth mobile interaction
279
+ }
280
+ ```
281
+
282
+ ### Motion Sensor Activation
283
+
284
+ **Purpose:** Enable device motion and orientation sensors with user permission handling.
285
+
286
+ **Commands:**
287
+ - `enableGyroTap(message)` - Tap anywhere on screen to enable sensors
288
+ - `enableGyroButton(text)` - Creates a button with custom text to enable sensors
289
+
290
+ **Usage:**
291
+ ```javascript
292
+ // Tap-to-enable (recommended)
293
+ enableGyroTap('Tap to enable motion sensors');
294
+
295
+ // Button-based activation
296
+ enableGyroButton('Enable Motion');
297
+ ```
298
+
299
+ **Available p5.js Variables (when `window.sensorsEnabled` is true):**
300
+
301
+ | Variable | Description | Range/Units |
302
+ |----------|-------------|-------------|
303
+ | [`rotationX`](https://p5js.org/reference/p5/rotationX/) | Device tilt forward/backward | -180° to 180° |
304
+ | [`rotationY`](https://p5js.org/reference/p5/rotationY/) | Device tilt left/right | -180° to 180° |
305
+ | [`rotationZ`](https://p5js.org/reference/p5/rotationZ/) | Device rotation around screen | -180° to 180° |
306
+ | [`accelerationX`](https://p5js.org/reference/p5/accelerationX/) | Acceleration left/right | m/s² |
307
+ | [`accelerationY`](https://p5js.org/reference/p5/accelerationY/) | Acceleration up/down | m/s² |
308
+ | [`accelerationZ`](https://p5js.org/reference/p5/accelerationZ/) | Acceleration forward/back | m/s² |
309
+ | [`deviceShaken`](https://p5js.org/reference/p5/deviceShaken/) | Shake detection event | true when shaken |
310
+ | [`deviceMoved`](https://p5js.org/reference/p5/deviceMoved/) | Movement detection event | true when moved |
311
+
312
+ **Important:** All motion sensor variables, including `deviceShaken` and `deviceMoved`, are only available when `window.sensorsEnabled` is true. Always check this status before using any motion data.
313
+
314
+ **Example:**
315
+ ```javascript
316
+ function draw() {
317
+ // CRITICAL: Always check window.sensorsEnabled first
318
+ if (window.sensorsEnabled) {
319
+ // Tilt-controlled circle
320
+ let x = width/2 + rotationY * 3;
321
+ let y = height/2 + rotationX * 3;
322
+ circle(x, y, 50);
323
+
324
+ // Shake detection - only works when sensors are enabled
325
+ if (deviceShaken) {
326
+ background(random(255), random(255), random(255));
327
+ }
328
+
329
+ // Movement detection - also requires sensors to be enabled
330
+ if (deviceMoved) {
331
+ fill(255, 0, 0);
332
+ }
333
+ } else {
334
+ // Show fallback when sensors not enabled
335
+ text('Tap to enable motion sensors', 20, 20);
336
+ }
337
+ }
338
+ ```
339
+
340
+ ### Microphone Activation
341
+
342
+ **Purpose:** Enable device microphone with user permission handling for audio-reactive applications.
343
+
344
+ **Important:** Microphone examples require the p5.sound library. Add this script tag to your HTML:
345
+ ```html
346
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.0/addons/p5.sound.min.js"></script>
347
+ ```
348
+
349
+ **Commands:**
350
+ - `enableMicTap(message)` - Tap anywhere on screen to enable microphone
351
+ - `enableMicButton(text)` - Creates a button with custom text to enable microphone
352
+
353
+ **Usage:**
354
+ ```javascript
355
+ // Tap-to-enable (recommended)
356
+ enableMicTap('Tap to enable microphone');
357
+
358
+ // Button-based activation
359
+ enableMicButton('Enable Audio');
360
+ ```
361
+
362
+ **Available p5.js Variables (when `window.micEnabled` is true):**
363
+
364
+ | Variable | Description | Range |
365
+ |----------|-------------|-------|
366
+ | [`p5.AudioIn()`](https://p5js.org/reference/p5.sound/p5.AudioIn/) | Audio input object (stored in `mic`) | Object |
367
+ | [`mic.getLevel()`](https://p5js.org/reference/p5.sound/p5.AudioIn/getLevel/) | Current audio input level | 0.0 to 1.0 |
368
+
369
+ **Example:**
370
+ ```javascript
371
+ let mic;
372
+
373
+ function setup() {
374
+ createCanvas(windowWidth, windowHeight);
375
+
376
+ // Create a new p5.AudioIn() instance
377
+ mic = new p5.AudioIn();
378
+
379
+ // Enable microphone with tap
380
+ enableMicTap();
381
+ }
382
+
383
+ function draw() {
384
+ if (window.micEnabled) {
385
+ // The mic object is a p5.AudioIn() instance
386
+ // Audio-reactive visualization
387
+ let level = mic.getLevel();
388
+ let size = map(level, 0, 1, 10, 200);
389
+
390
+ background(level * 255);
391
+ circle(width/2, height/2, size);
392
+ }
393
+ }
394
+ ```
395
+
396
+ ### Sound Output Activation
397
+
398
+ **Purpose:** Enable audio playback without requiring microphone input. Perfect for playing sounds, music, synthesizers, and audio effects in mobile browsers.
399
+
400
+ **Important:** Sound examples require the p5.sound library. Add this script tag to your HTML:
401
+ ```html
402
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.0/addons/p5.sound.min.js"></script>
403
+ ```
404
+
405
+ **Commands:**
406
+ - `enableSoundTap(message)` - Tap anywhere on screen to enable sound playback
407
+ - `enableSoundButton(text)` - Creates a button with custom text to enable sound
408
+
409
+ **Usage:**
410
+ ```javascript
411
+ // Tap-to-enable (recommended)
412
+ enableSoundTap('Tap to enable sound');
413
+
414
+ // Button-based activation
415
+ enableSoundButton('Enable Sound');
416
+ ```
417
+
418
+ **When to use Sound vs. Microphone:**
419
+ - Use `enableSound` for: Playing audio files, synthesizers, oscillators, sound effects
420
+ - Use `enableMic` for: Recording audio, audio-reactive visualizations, voice input
421
+ - Note: `enableMic` also enables sound output, so you don't need both
422
+
423
+ **Example:**
424
+ ```javascript
425
+ let mySound;
426
+
427
+ function preload() {
428
+ // Load audio file
429
+ mySound = loadSound('assets/sound.mp3');
430
+ }
431
+
432
+ function setup() {
433
+ createCanvas(windowWidth, windowHeight);
434
+
435
+ // Enable sound playback with tap
436
+ enableSoundTap('Tap to enable sound');
437
+ }
438
+
439
+ function draw() {
440
+ background(220);
441
+
442
+ if (window.soundEnabled) {
443
+ text('Tap anywhere to play sound', 20, 20);
444
+ } else {
445
+ text('Waiting for sound activation...', 20, 20);
446
+ }
447
+ }
448
+
449
+ function mousePressed() {
450
+ // Check if sound is enabled before playing
451
+ if (window.soundEnabled && !mySound.isPlaying()) {
452
+ mySound.play();
453
+ }
454
+ }
455
+ ```
456
+
457
+ ### Debug System
458
+
459
+ **Purpose:** Essential on-screen debugging system for mobile development where traditional browser dev tools aren't accessible. Provides automatic error catching, timestamped logging, and color-coded messages.
460
+
461
+ **Why use it:** Mobile browsers often hide JavaScript errors, making debugging difficult. This system displays all errors, warnings, and custom messages directly on your mobile screen with timestamps and color coding.
462
+
463
+ **Commands:**
464
+
465
+ | Function | Purpose | Example |
466
+ |----------|---------|---------|
467
+ | `showDebug()` | Show debug panel and enable error catching | `showDebug()` |
468
+ | `hideDebug()` | Hide debug panel | `hideDebug()` |
469
+ | `toggleDebug()` | Toggle panel visibility | `toggleDebug()` |
470
+ | `debug(...args)` | Log messages (white text) | `debug("App started", frameRate())` |
471
+ | `debugError(...args)` | Display errors (red text) | `debugError("Connection failed")` |
472
+ | `debugWarn(...args)` | Display warnings (yellow text) | `debugWarn("Low battery")` |
473
+ | `debug.clear()` | Clear all messages | `debug.clear()` |
474
+
475
+ **Key Features:**
476
+ - **Automatic Error Catching** - JavaScript errors automatically displayed with red styling
477
+ - **Error Location** - Shows filename and line number for easy debugging
478
+ - **Timestamps** - All messages include precise timestamps
479
+ - **Color Coding** - Errors (red), warnings (yellow), normal messages (white)
480
+ - **Mobile Optimized** - Touch-friendly interface that works on small screens
481
+ - **Keyboard Shortcuts** - Press 'D' to toggle, 'C' to clear (when debug is enabled)
482
+
483
+ **Critical Setup:**
484
+ ```javascript
485
+ function setup() {
486
+ // IMPORTANT: Call showDebug() FIRST to catch setup errors
487
+ showDebug();
488
+
489
+ createCanvas(windowWidth, windowHeight);
490
+ // Any errors after this point will be automatically caught and displayed
491
+ }
492
+ ```
493
+
494
+ **Usage Examples:**
495
+ ```javascript
496
+ // Basic logging
497
+ debug("Touch at:", mouseX, mouseY);
498
+ debug("Sensors enabled:", window.sensorsEnabled);
499
+
500
+ // Error handling
501
+ debugError("Failed to load image");
502
+ debugWarn("Frame rate dropping:", frameRate());
503
+
504
+ // Objects and arrays
505
+ debug("Touch points:", touches);
506
+ debug({rotation: rotationX, acceleration: accelerationX});
507
+ ```
508
+
509
+