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.
- package/LICENSE +21 -0
- package/README.md +509 -0
- package/dist/p5-phone.js +1062 -0
- package/dist/p5-phone.min.js +10 -0
- package/examples/Phone Sensor Examples/microphone/01_mic_level/index.html +19 -0
- package/examples/Phone Sensor Examples/microphone/01_mic_level/sketch.js +117 -0
- package/examples/Phone Sensor Examples/movement/01_orientation_basic/index.html +28 -0
- package/examples/Phone Sensor Examples/movement/01_orientation_basic/sketch.js +123 -0
- package/examples/Phone Sensor Examples/movement/02_rotational_velocity/index.html +28 -0
- package/examples/Phone Sensor Examples/movement/02_rotational_velocity/sketch.js +144 -0
- package/examples/Phone Sensor Examples/movement/03_acceleration/index.html +28 -0
- package/examples/Phone Sensor Examples/movement/03_acceleration/sketch.js +87 -0
- package/examples/Phone Sensor Examples/sound/01_dual_audio/index.html +31 -0
- package/examples/Phone Sensor Examples/sound/01_dual_audio/sketch.js +225 -0
- package/examples/Phone Sensor Examples/sound/01_dual_audio/tracks/audio1.mp3 +0 -0
- package/examples/Phone Sensor Examples/sound/01_dual_audio/tracks/audio2.mp3 +0 -0
- package/examples/Phone Sensor Examples/sound/02_volume_touches/index.html +31 -0
- package/examples/Phone Sensor Examples/sound/02_volume_touches/sketch.js +269 -0
- package/examples/Phone Sensor Examples/sound/02_volume_touches/tracks/audio1.mp3 +0 -0
- package/examples/Phone Sensor Examples/touch/01_touch_basic/index.html +28 -0
- package/examples/Phone Sensor Examples/touch/01_touch_basic/sketch.js +94 -0
- package/examples/Phone Sensor Examples/touch/02_touch_zones/index.html +28 -0
- package/examples/Phone Sensor Examples/touch/02_touch_zones/sketch.js +220 -0
- package/examples/Phone Sensor Examples/touch/03_touch_count/index.html +28 -0
- package/examples/Phone Sensor Examples/touch/03_touch_count/sketch.js +93 -0
- package/examples/Phone Sensor Examples/touch/04_touch_distance/index.html +28 -0
- package/examples/Phone Sensor Examples/touch/04_touch_distance/sketch.js +120 -0
- package/examples/Phone Sensor Examples/touch/05_touch_angle/index.html +28 -0
- package/examples/Phone Sensor Examples/touch/05_touch_angle/sketch.js +117 -0
- package/examples/Phone Sensor Examples - Minimal/microphone/01_mic_level/index.html +19 -0
- package/examples/Phone Sensor Examples - Minimal/microphone/01_mic_level/sketch.js +72 -0
- package/examples/Phone Sensor Examples - Minimal/movement/01_orientation_basic/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/movement/01_orientation_basic/sketch.js +51 -0
- package/examples/Phone Sensor Examples - Minimal/movement/02_rotational_velocity/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/movement/02_rotational_velocity/sketch.js +70 -0
- package/examples/Phone Sensor Examples - Minimal/movement/03_acceleration/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/movement/03_acceleration/sketch.js +83 -0
- package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/index.html +19 -0
- package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/sketch.js +96 -0
- package/examples/Phone Sensor Examples - Minimal/sound/01_sound_basic/tracks/audio1.mp3 +0 -0
- package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/index.html +19 -0
- package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/sketch.js +118 -0
- package/examples/Phone Sensor Examples - Minimal/sound/02_sound_amplitude/tracks/audio1.mp3 +0 -0
- package/examples/Phone Sensor Examples - Minimal/touch/01_touch_basic/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/touch/01_touch_basic/sketch.js +58 -0
- package/examples/Phone Sensor Examples - Minimal/touch/02_touch_zones/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/touch/02_touch_zones/sketch.js +78 -0
- package/examples/Phone Sensor Examples - Minimal/touch/03_touch_count/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/touch/03_touch_count/sketch.js +64 -0
- package/examples/Phone Sensor Examples - Minimal/touch/04_touch_distance/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/touch/04_touch_distance/sketch.js +69 -0
- package/examples/Phone Sensor Examples - Minimal/touch/05_touch_angle/index.html +18 -0
- package/examples/Phone Sensor Examples - Minimal/touch/05_touch_angle/sketch.js +85 -0
- package/examples/Phone and Gif/collision/README.md +31 -0
- Gif/collision/gifs/spaceSuit2.png +0 -0
- package/examples/Phone and Gif/collision/index.html +19 -0
- package/examples/Phone and Gif/collision/sketch.js +226 -0
- Gif/fetch/gifs/corgiswimflip.gif +0 -0
- package/examples/Phone and Gif/fetch/index.html +18 -0
- package/examples/Phone and Gif/fetch/sketch.js +139 -0
- Gif/fly/gifs/comparison.gif +0 -0
- package/examples/Phone and Gif/fly/index.html +18 -0
- package/examples/Phone and Gif/fly/sketch.js +103 -0
- Gif/roll/gifs/how-penciles-are-made.gif +0 -0
- package/examples/Phone and Gif/roll/index.html +18 -0
- package/examples/Phone and Gif/roll/sketch.js +121 -0
- package/examples/UXcompare/button-vs-movement/index.html +45 -0
- package/examples/UXcompare/button-vs-movement/sketch.js +355 -0
- package/examples/UXcompare/button-vs-orientation/index.html +25 -0
- package/examples/UXcompare/button-vs-orientation/sketch.js +317 -0
- package/examples/UXcompare/button-vs-shake/index.html +45 -0
- package/examples/UXcompare/button-vs-shake/sketch.js +320 -0
- package/examples/UXcompare/gyroscope-demo/index.html +78 -0
- package/examples/UXcompare/gyroscope-demo/sketch.js +166 -0
- package/examples/UXcompare/index.html +419 -0
- package/examples/UXcompare/microphone-demo/index.html +79 -0
- package/examples/UXcompare/microphone-demo/sketch.js +210 -0
- package/examples/UXcompare/slider-vs-angle/index.html +25 -0
- package/examples/UXcompare/slider-vs-angle/sketch.js +429 -0
- package/examples/UXcompare/slider-vs-distance/index.html +25 -0
- package/examples/UXcompare/slider-vs-distance/sketch.js +401 -0
- package/examples/UXcompare/slider-vs-microphone/index.html +26 -0
- package/examples/UXcompare/slider-vs-microphone/sketch.js +336 -0
- package/examples/UXcompare/slider-vs-touches/index.html +25 -0
- package/examples/UXcompare/slider-vs-touches/sketch.js +376 -0
- package/examples/UXcompare/sliders-vs-acceleration/index.html +25 -0
- package/examples/UXcompare/sliders-vs-acceleration/sketch.js +361 -0
- package/examples/UXcompare/sliders-vs-rotation/index.html +25 -0
- package/examples/UXcompare/sliders-vs-rotation/sketch.js +375 -0
- package/examples/blankTemplate/index.html +31 -0
- package/examples/blankTemplate/sketch.js +55 -0
- package/examples/homepage/index.html +506 -0
- package/package.json +73 -0
- package/src/p5-phone.js +1062 -0
- package/src/permissionMic.js +240 -0
- package/src/permissionsGesture.js +213 -0
- package/src/permissionsGyro.js +246 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// ==============================================
|
|
2
|
+
// DUAL AUDIO PLAYBACK EXAMPLE (TOUCH VERSION)
|
|
3
|
+
// ==============================================
|
|
4
|
+
// This example shows how to enable audio playback
|
|
5
|
+
// on mobile devices and play different sounds
|
|
6
|
+
// based on screen touch zones
|
|
7
|
+
//
|
|
8
|
+
// CONCEPTS COVERED:
|
|
9
|
+
// - Audio playback on mobile (enableSoundTap)
|
|
10
|
+
// - Loading audio files
|
|
11
|
+
// - Touch zone detection
|
|
12
|
+
// - Audio visualization with amplitude
|
|
13
|
+
// - Mobile gesture locking
|
|
14
|
+
// ==============================================
|
|
15
|
+
|
|
16
|
+
// Global variables to store our audio files
|
|
17
|
+
// We declare these at the top so they can be accessed by all functions
|
|
18
|
+
let audioTrack1;
|
|
19
|
+
let audioTrack2;
|
|
20
|
+
|
|
21
|
+
// Global variables for audio level detection
|
|
22
|
+
// Amplitude analyzes the volume level of audio in real-time
|
|
23
|
+
let amplitude1;
|
|
24
|
+
let amplitude2;
|
|
25
|
+
|
|
26
|
+
// ==============================================
|
|
27
|
+
// PRELOAD FUNCTION - Runs before setup
|
|
28
|
+
// ==============================================
|
|
29
|
+
// preload() function runs BEFORE setup()
|
|
30
|
+
// This is the best place to load external assets like images, sounds, or fonts
|
|
31
|
+
// p5.js will wait for all preload operations to complete before moving to setup()
|
|
32
|
+
function preload()
|
|
33
|
+
{
|
|
34
|
+
// Load both audio files from the tracks folder
|
|
35
|
+
// The paths are relative to where our HTML file is located
|
|
36
|
+
audioTrack1 = loadSound('tracks/audio1.mp3');
|
|
37
|
+
audioTrack2 = loadSound('tracks/audio2.mp3');
|
|
38
|
+
|
|
39
|
+
// Note: Audio files must be loaded in preload() to ensure they're ready
|
|
40
|
+
// Modern browsers require user interaction before playing audio
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ==============================================
|
|
44
|
+
// SETUP FUNCTION - Runs once when page loads
|
|
45
|
+
// ==============================================
|
|
46
|
+
// setup() runs once after preload() is complete
|
|
47
|
+
// This is where we configure our canvas and initial settings
|
|
48
|
+
function setup()
|
|
49
|
+
{
|
|
50
|
+
// Create a canvas that fills the entire screen
|
|
51
|
+
createCanvas(windowWidth, windowHeight);
|
|
52
|
+
|
|
53
|
+
// Lock mobile gestures to prevent scrolling, zooming, etc.
|
|
54
|
+
lockGestures();
|
|
55
|
+
|
|
56
|
+
// Enable audio playback on mobile devices
|
|
57
|
+
// This creates a tap-to-start overlay for iOS/mobile browsers
|
|
58
|
+
enableSoundTap();
|
|
59
|
+
|
|
60
|
+
// Set the background to a dark color
|
|
61
|
+
background(50);
|
|
62
|
+
|
|
63
|
+
// Set text properties for displaying instructions
|
|
64
|
+
textAlign(CENTER, CENTER);
|
|
65
|
+
textSize(16);
|
|
66
|
+
fill(255);
|
|
67
|
+
|
|
68
|
+
// Set the volume of both audio tracks (0.0 to 1.0)
|
|
69
|
+
audioTrack1.setVolume(0.7);
|
|
70
|
+
audioTrack2.setVolume(0.7);
|
|
71
|
+
|
|
72
|
+
// Set both tracks to loop continuously
|
|
73
|
+
audioTrack1.loop();
|
|
74
|
+
audioTrack2.loop();
|
|
75
|
+
// Pause them initially (they will start looped when played)
|
|
76
|
+
audioTrack1.pause();
|
|
77
|
+
audioTrack2.pause();
|
|
78
|
+
|
|
79
|
+
// Create amplitude objects to analyze audio levels for both tracks
|
|
80
|
+
amplitude1 = new p5.Amplitude();
|
|
81
|
+
amplitude2 = new p5.Amplitude();
|
|
82
|
+
// Connect the amplitude analyzers to our audio tracks
|
|
83
|
+
amplitude1.setInput(audioTrack1);
|
|
84
|
+
amplitude2.setInput(audioTrack2);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ==============================================
|
|
88
|
+
// DRAW FUNCTION - Runs continuously (like a loop)
|
|
89
|
+
// ==============================================
|
|
90
|
+
function draw()
|
|
91
|
+
{
|
|
92
|
+
// Clear the background each frame
|
|
93
|
+
background(50);
|
|
94
|
+
|
|
95
|
+
// Draw the split screen zones
|
|
96
|
+
drawSplitScreen();
|
|
97
|
+
|
|
98
|
+
// Show instruction text if audio context hasn't been started yet
|
|
99
|
+
// window.soundEnabled is set by enableSoundTap() after user interaction
|
|
100
|
+
if (!window.soundEnabled)
|
|
101
|
+
{
|
|
102
|
+
fill(255, 255, 0); // yellow text
|
|
103
|
+
textSize(24);
|
|
104
|
+
text("Touch anywhere to start", width/2, height/2);
|
|
105
|
+
return; // don't draw anything else until audio is started
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Draw audio visualizers once sound is enabled
|
|
109
|
+
drawVisualizers();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ==============================================
|
|
113
|
+
// HELPER FUNCTIONS
|
|
114
|
+
// ==============================================
|
|
115
|
+
|
|
116
|
+
function drawSplitScreen()
|
|
117
|
+
{
|
|
118
|
+
// Top half - Audio 1 zone (light blue)
|
|
119
|
+
noStroke();
|
|
120
|
+
fill(150, 200, 255);
|
|
121
|
+
rect(0, 0, width, height/2);
|
|
122
|
+
|
|
123
|
+
// Bottom half - Audio 2 zone (light green)
|
|
124
|
+
fill(150, 255, 200);
|
|
125
|
+
rect(0, height/2, width, height/2);
|
|
126
|
+
|
|
127
|
+
// Draw a horizontal line to divide the canvas in half
|
|
128
|
+
stroke(255);
|
|
129
|
+
strokeWeight(2);
|
|
130
|
+
line(0, height/2, width, height/2);
|
|
131
|
+
noStroke();
|
|
132
|
+
|
|
133
|
+
// Draw labels if audio has been enabled
|
|
134
|
+
if (window.soundEnabled)
|
|
135
|
+
{
|
|
136
|
+
fill(50);
|
|
137
|
+
textSize(20);
|
|
138
|
+
text("AUDIO 1", width/2, height/4 - 60);
|
|
139
|
+
text("Touch & hold to play", width/2, height/4 - 35);
|
|
140
|
+
|
|
141
|
+
text("AUDIO 2", width/2, (height/4) * 3 - 60);
|
|
142
|
+
text("Touch & hold to play", width/2, (height/4) * 3 - 35);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function drawVisualizers()
|
|
147
|
+
{
|
|
148
|
+
// Visual audio level feedback for track 1
|
|
149
|
+
if (audioTrack1.isPlaying())
|
|
150
|
+
{
|
|
151
|
+
let level1 = amplitude1.getLevel();
|
|
152
|
+
let circleSize1 = map(level1, 0, 0.3, 30, 200);
|
|
153
|
+
fill(255, 150, 100, 200); // Orange, semi-transparent
|
|
154
|
+
noStroke();
|
|
155
|
+
ellipse(width/2, height/4, circleSize1, circleSize1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Visual audio level feedback for track 2
|
|
159
|
+
if (audioTrack2.isPlaying())
|
|
160
|
+
{
|
|
161
|
+
let level2 = amplitude2.getLevel();
|
|
162
|
+
let circleSize2 = map(level2, 0, 0.3, 30, 200);
|
|
163
|
+
fill(255, 100, 200, 200); // Magenta, semi-transparent
|
|
164
|
+
noStroke();
|
|
165
|
+
ellipse(width/2, 3*height/4, circleSize2, circleSize2);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ==============================================
|
|
170
|
+
// TOUCH EVENT FUNCTIONS
|
|
171
|
+
// ==============================================
|
|
172
|
+
|
|
173
|
+
// touchStarted() runs when a touch begins
|
|
174
|
+
// This function starts playing audio based on which zone is touched
|
|
175
|
+
function touchStarted()
|
|
176
|
+
{
|
|
177
|
+
// Only process if sound system is enabled
|
|
178
|
+
if (!window.soundEnabled)
|
|
179
|
+
{
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Get the first touch point
|
|
184
|
+
if (touches.length > 0)
|
|
185
|
+
{
|
|
186
|
+
let touchY = touches[0].y;
|
|
187
|
+
|
|
188
|
+
// Check if touch is in top half (Audio 1 zone)
|
|
189
|
+
if (touchY < height/2)
|
|
190
|
+
{
|
|
191
|
+
if (!audioTrack1.isPlaying())
|
|
192
|
+
{
|
|
193
|
+
audioTrack1.play();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Check if touch is in bottom half (Audio 2 zone)
|
|
197
|
+
else
|
|
198
|
+
{
|
|
199
|
+
if (!audioTrack2.isPlaying())
|
|
200
|
+
{
|
|
201
|
+
audioTrack2.play();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Prevent default touch behavior
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// touchEnded() runs when a touch is released
|
|
211
|
+
// This function stops the audio when touch is released
|
|
212
|
+
function touchEnded()
|
|
213
|
+
{
|
|
214
|
+
// Only process if sound system is enabled
|
|
215
|
+
if (window.soundEnabled)
|
|
216
|
+
{
|
|
217
|
+
// Pause both audio tracks when touch is released
|
|
218
|
+
audioTrack1.pause();
|
|
219
|
+
audioTrack2.pause();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Prevent default touch behavior
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Touch Volume - Mobile p5.js</title>
|
|
7
|
+
|
|
8
|
+
<!-- Basic CSS to remove browser defaults and align canvas -->
|
|
9
|
+
<style>
|
|
10
|
+
body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
padding: 0;
|
|
13
|
+
overflow: hidden;
|
|
14
|
+
}
|
|
15
|
+
</style>
|
|
16
|
+
|
|
17
|
+
<!-- Load p5.js library -->
|
|
18
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.10/p5.min.js"></script>
|
|
19
|
+
|
|
20
|
+
<!-- Load p5.sound library for audio -->
|
|
21
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.10/addons/p5.sound.min.js"></script>
|
|
22
|
+
|
|
23
|
+
<!-- Load the mobile p5.js permissions library -->
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
25
|
+
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<!-- Load the p5.js sketch -->
|
|
29
|
+
<script src="sketch.js"></script>
|
|
30
|
+
</body>
|
|
31
|
+
</html>
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// ==============================================
|
|
2
|
+
// VOLUME BY TOUCHES EXAMPLE
|
|
3
|
+
// ==============================================
|
|
4
|
+
// This example demonstrates controlling audio volume
|
|
5
|
+
// based on the number of simultaneous touches
|
|
6
|
+
//
|
|
7
|
+
// CONCEPTS COVERED:
|
|
8
|
+
// - Audio playback on mobile (enableSoundTap)
|
|
9
|
+
// - Loading audio files
|
|
10
|
+
// - Multi-touch detection (touches.length)
|
|
11
|
+
// - Dynamic volume control based on touch count
|
|
12
|
+
// - Audio visualization with amplitude
|
|
13
|
+
// - Mobile gesture locking
|
|
14
|
+
// ==============================================
|
|
15
|
+
|
|
16
|
+
// Global variables to store our audio file
|
|
17
|
+
let audioTrack;
|
|
18
|
+
|
|
19
|
+
// Global variable for audio level detection
|
|
20
|
+
// Amplitude analyzes the volume level of audio in real-time
|
|
21
|
+
let amplitude;
|
|
22
|
+
|
|
23
|
+
// Maximum number of touches to detect (for volume control)
|
|
24
|
+
const MAX_TOUCHES = 5;
|
|
25
|
+
|
|
26
|
+
// ==============================================
|
|
27
|
+
// PRELOAD FUNCTION - Runs before setup
|
|
28
|
+
// ==============================================
|
|
29
|
+
function preload()
|
|
30
|
+
{
|
|
31
|
+
// Load the audio file from the tracks folder
|
|
32
|
+
// The path is relative to where our HTML file is located
|
|
33
|
+
audioTrack = loadSound('tracks/audio1.mp3');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ==============================================
|
|
37
|
+
// SETUP FUNCTION - Runs once when page loads
|
|
38
|
+
// ==============================================
|
|
39
|
+
function setup()
|
|
40
|
+
{
|
|
41
|
+
// Create a canvas that fills the entire screen
|
|
42
|
+
createCanvas(windowWidth, windowHeight);
|
|
43
|
+
|
|
44
|
+
// Lock mobile gestures to prevent scrolling, zooming, etc.
|
|
45
|
+
lockGestures();
|
|
46
|
+
|
|
47
|
+
// Enable audio playback on mobile devices
|
|
48
|
+
// This creates a tap-to-start overlay for iOS/mobile browsers
|
|
49
|
+
enableSoundTap();
|
|
50
|
+
|
|
51
|
+
// Set the background to a dark color
|
|
52
|
+
background(30);
|
|
53
|
+
|
|
54
|
+
// Set text properties for displaying instructions
|
|
55
|
+
textAlign(CENTER, CENTER);
|
|
56
|
+
textSize(16);
|
|
57
|
+
fill(255);
|
|
58
|
+
|
|
59
|
+
// Set the audio track to loop continuously
|
|
60
|
+
audioTrack.loop();
|
|
61
|
+
// Pause it initially (will start when touched)
|
|
62
|
+
audioTrack.pause();
|
|
63
|
+
|
|
64
|
+
// Create amplitude object to analyze audio level
|
|
65
|
+
amplitude = new p5.Amplitude();
|
|
66
|
+
// Connect the amplitude analyzer to our audio track
|
|
67
|
+
amplitude.setInput(audioTrack);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ==============================================
|
|
71
|
+
// DRAW FUNCTION - Runs continuously (like a loop)
|
|
72
|
+
// ==============================================
|
|
73
|
+
function draw()
|
|
74
|
+
{
|
|
75
|
+
// Clear the background each frame with a gradient effect
|
|
76
|
+
background(30, 20, 50);
|
|
77
|
+
|
|
78
|
+
// Show instruction text if audio hasn't been started yet
|
|
79
|
+
if (!window.soundEnabled)
|
|
80
|
+
{
|
|
81
|
+
fill(255, 200, 100);
|
|
82
|
+
textSize(24);
|
|
83
|
+
text("Touch anywhere to start", width/2, height/2 - 40);
|
|
84
|
+
textSize(18);
|
|
85
|
+
text("Use 1-5 fingers to control volume", width/2, height/2 + 10);
|
|
86
|
+
return; // don't draw anything else until audio is started
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Draw instructions at the top
|
|
90
|
+
fill(200, 200, 255);
|
|
91
|
+
textSize(16);
|
|
92
|
+
text("Touch & hold with 1-5 fingers to play", width/2, 30);
|
|
93
|
+
text("More fingers = Higher volume", width/2, 55);
|
|
94
|
+
|
|
95
|
+
// Draw visualizer and touch feedback
|
|
96
|
+
drawVisualizer();
|
|
97
|
+
drawTouchInfo();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ==============================================
|
|
101
|
+
// HELPER FUNCTIONS
|
|
102
|
+
// ==============================================
|
|
103
|
+
|
|
104
|
+
function drawVisualizer()
|
|
105
|
+
{
|
|
106
|
+
// Only draw visualizer if audio is playing
|
|
107
|
+
if (audioTrack.isPlaying())
|
|
108
|
+
{
|
|
109
|
+
// Get the current audio level
|
|
110
|
+
let level = amplitude.getLevel();
|
|
111
|
+
|
|
112
|
+
// Map the level to a circle size (30 to 400 pixels)
|
|
113
|
+
let circleSize = map(level, 0, 0.5, 30, 400);
|
|
114
|
+
|
|
115
|
+
// Create a color that changes with the number of touches
|
|
116
|
+
let numTouches = min(touches.length, MAX_TOUCHES);
|
|
117
|
+
let hue = map(numTouches, 0, MAX_TOUCHES, 180, 300); // Blue to purple
|
|
118
|
+
|
|
119
|
+
// Draw the main visualizer circle
|
|
120
|
+
push();
|
|
121
|
+
colorMode(HSB);
|
|
122
|
+
fill(hue, 80, 100, 0.7); // Semi-transparent
|
|
123
|
+
noStroke();
|
|
124
|
+
circle(width/2, height/2, circleSize);
|
|
125
|
+
|
|
126
|
+
// Draw a smaller inner circle
|
|
127
|
+
fill(hue, 60, 100, 0.9);
|
|
128
|
+
circle(width/2, height/2, circleSize * 0.5);
|
|
129
|
+
pop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function drawTouchInfo()
|
|
134
|
+
{
|
|
135
|
+
// Display current number of touches and volume level
|
|
136
|
+
if (window.soundEnabled)
|
|
137
|
+
{
|
|
138
|
+
let numTouches = touches.length;
|
|
139
|
+
let volume = audioTrack.getVolume();
|
|
140
|
+
|
|
141
|
+
fill(255);
|
|
142
|
+
textSize(20);
|
|
143
|
+
text("Touches: " + numTouches, width/2, height - 80);
|
|
144
|
+
|
|
145
|
+
textSize(18);
|
|
146
|
+
text("Volume: " + (volume * 100).toFixed(0) + "%", width/2, height - 50);
|
|
147
|
+
|
|
148
|
+
// Draw visual indicators for each possible touch
|
|
149
|
+
drawTouchIndicators(numTouches);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function drawTouchIndicators(currentTouches)
|
|
154
|
+
{
|
|
155
|
+
// Draw 5 circles representing the maximum touch count
|
|
156
|
+
let spacing = 50;
|
|
157
|
+
let startX = width/2 - (spacing * 2);
|
|
158
|
+
let y = height - 120;
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < MAX_TOUCHES; i++)
|
|
161
|
+
{
|
|
162
|
+
let x = startX + (i * spacing);
|
|
163
|
+
|
|
164
|
+
push();
|
|
165
|
+
if (i < currentTouches)
|
|
166
|
+
{
|
|
167
|
+
// Active touch - filled circle
|
|
168
|
+
fill(100, 200, 255);
|
|
169
|
+
stroke(255);
|
|
170
|
+
}
|
|
171
|
+
else
|
|
172
|
+
{
|
|
173
|
+
// Inactive touch - outline only
|
|
174
|
+
noFill();
|
|
175
|
+
stroke(100);
|
|
176
|
+
}
|
|
177
|
+
strokeWeight(2);
|
|
178
|
+
circle(x, y, 30);
|
|
179
|
+
|
|
180
|
+
// Draw finger number
|
|
181
|
+
if (i < currentTouches)
|
|
182
|
+
{
|
|
183
|
+
fill(255);
|
|
184
|
+
noStroke();
|
|
185
|
+
textSize(14);
|
|
186
|
+
text(i + 1, x, y);
|
|
187
|
+
}
|
|
188
|
+
pop();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ==============================================
|
|
193
|
+
// TOUCH EVENT FUNCTIONS
|
|
194
|
+
// ==============================================
|
|
195
|
+
|
|
196
|
+
// touchStarted() runs when a touch begins
|
|
197
|
+
function touchStarted()
|
|
198
|
+
{
|
|
199
|
+
// Only process if sound system is enabled
|
|
200
|
+
if (!window.soundEnabled)
|
|
201
|
+
{
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Start playing the audio if it's not already playing
|
|
206
|
+
if (!audioTrack.isPlaying())
|
|
207
|
+
{
|
|
208
|
+
audioTrack.play();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Update volume based on number of touches
|
|
212
|
+
updateVolume();
|
|
213
|
+
|
|
214
|
+
// Prevent default touch behavior
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// touchMoved() runs continuously while touches are moving
|
|
219
|
+
function touchMoved()
|
|
220
|
+
{
|
|
221
|
+
// Update volume as touches are added or removed
|
|
222
|
+
if (window.soundEnabled)
|
|
223
|
+
{
|
|
224
|
+
updateVolume();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Prevent default touch behavior
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// touchEnded() runs when a touch is released
|
|
232
|
+
function touchEnded()
|
|
233
|
+
{
|
|
234
|
+
// If no more touches, pause the audio
|
|
235
|
+
if (window.soundEnabled && touches.length === 0)
|
|
236
|
+
{
|
|
237
|
+
audioTrack.pause();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Prevent default touch behavior
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ==============================================
|
|
245
|
+
// VOLUME CONTROL FUNCTION
|
|
246
|
+
// ==============================================
|
|
247
|
+
|
|
248
|
+
function updateVolume()
|
|
249
|
+
{
|
|
250
|
+
// Get the number of current touches (capped at MAX_TOUCHES)
|
|
251
|
+
let numTouches = min(touches.length, MAX_TOUCHES);
|
|
252
|
+
|
|
253
|
+
// Map number of touches (1-5) to volume (0.2 to 1.0)
|
|
254
|
+
// This ensures there's always some volume with 1 touch
|
|
255
|
+
let volume = map(numTouches, 1, MAX_TOUCHES, 0.2, 1.0);
|
|
256
|
+
|
|
257
|
+
// Set the audio volume
|
|
258
|
+
audioTrack.setVolume(volume);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ==============================================
|
|
262
|
+
// RESPONSIVE CANVAS
|
|
263
|
+
// ==============================================
|
|
264
|
+
|
|
265
|
+
// Resize canvas when window size changes
|
|
266
|
+
function windowResized()
|
|
267
|
+
{
|
|
268
|
+
resizeCanvas(windowWidth, windowHeight);
|
|
269
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Basic Touch Detection - Mobile p5.js</title>
|
|
7
|
+
|
|
8
|
+
<!-- Basic CSS to remove browser defaults and align canvas -->
|
|
9
|
+
<style>
|
|
10
|
+
body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
padding: 0;
|
|
13
|
+
overflow: hidden;
|
|
14
|
+
}
|
|
15
|
+
</style>
|
|
16
|
+
|
|
17
|
+
<!-- Load p5.js library -->
|
|
18
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.10/p5.min.js"></script>
|
|
19
|
+
|
|
20
|
+
<!-- Load the mobile p5.js permissions library -->
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
22
|
+
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<!-- Load the p5.js sketch -->
|
|
26
|
+
<script src="sketch.js"></script>
|
|
27
|
+
</body>
|
|
28
|
+
</html>
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// ==============================================
|
|
2
|
+
// BASIC TOUCH DETECTION EXAMPLE
|
|
3
|
+
// ==============================================
|
|
4
|
+
// This example shows how to detect when the user
|
|
5
|
+
// is touching the screen and display appropriate text
|
|
6
|
+
//
|
|
7
|
+
// CONCEPTS COVERED:
|
|
8
|
+
// - Touch detection (touchStarted, touchEnded)
|
|
9
|
+
// - Visual feedback based on user interaction
|
|
10
|
+
// - Basic p5.js setup and draw functions
|
|
11
|
+
// - Mobile gesture locking
|
|
12
|
+
// ==============================================
|
|
13
|
+
|
|
14
|
+
// Variables to store our current state
|
|
15
|
+
let isCurrentlyTouching = false; // Track if screen is being touched
|
|
16
|
+
let touchCounter = 0; // Count how many times screen has been touched
|
|
17
|
+
let textColor; // Color of the text
|
|
18
|
+
let touchStartTime = 0; // When the current touch started (in milliseconds)
|
|
19
|
+
let touchDuration = 0; // How long the current touch has been active (in seconds)
|
|
20
|
+
|
|
21
|
+
// ==============================================
|
|
22
|
+
// SETUP FUNCTION - Runs once when page loads
|
|
23
|
+
// ==============================================
|
|
24
|
+
function setup() {
|
|
25
|
+
// Create a canvas that fills the entire screen
|
|
26
|
+
// windowWidth and windowHeight are p5.js variables for screen size
|
|
27
|
+
createCanvas(windowWidth, windowHeight);
|
|
28
|
+
|
|
29
|
+
// Lock mobile gestures to prevent scrolling, zooming, etc.
|
|
30
|
+
// This function comes from the mobile-p5-permissions library
|
|
31
|
+
lockGestures();
|
|
32
|
+
|
|
33
|
+
// Set initial text properties
|
|
34
|
+
textAlign(CENTER, CENTER); // Center the text horizontally and vertically
|
|
35
|
+
textSize(48); // Make text large enough to read on mobile
|
|
36
|
+
|
|
37
|
+
// Set initial colors
|
|
38
|
+
textColor = color(50, 50, 50); // Dark gray text
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ==============================================
|
|
42
|
+
// DRAW FUNCTION - Runs continuously (like a loop)
|
|
43
|
+
// ==============================================
|
|
44
|
+
function draw()
|
|
45
|
+
{
|
|
46
|
+
background(200, 255, 200);
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// Clear the screen each frame
|
|
50
|
+
// Change background color based on touch state
|
|
51
|
+
if (isCurrentlyTouching)
|
|
52
|
+
{
|
|
53
|
+
touchDuration = (millis() - touchStartTime) / 1000; // Convert to seconds
|
|
54
|
+
text("TOUCHED",width/2,height/2);
|
|
55
|
+
|
|
56
|
+
// Display the touch duration
|
|
57
|
+
textSize(24);
|
|
58
|
+
text("Touch Time: " + touchDuration.toFixed(1) + "s", width/2, height/2 + 60);
|
|
59
|
+
textSize(48); // Reset to original size
|
|
60
|
+
}
|
|
61
|
+
else
|
|
62
|
+
{
|
|
63
|
+
text("NOT TOUCHED",width/2,height/2);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Show the touch counter at the top of the screen
|
|
67
|
+
textSize(32); // Smaller text for the counter
|
|
68
|
+
text("Touch Count: " + touchCounter, width/2, 60);
|
|
69
|
+
textSize(48); // Reset to original size
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ==============================================
|
|
74
|
+
// TOUCH EVENT FUNCTIONS
|
|
75
|
+
// ==============================================
|
|
76
|
+
|
|
77
|
+
// This function runs when a touch begins (finger touches screen)
|
|
78
|
+
function touchStarted()
|
|
79
|
+
{
|
|
80
|
+
isCurrentlyTouching = true;
|
|
81
|
+
touchCounter = touchCounter + 1; // Add 1 to our counter each time screen is touched
|
|
82
|
+
touchStartTime = millis(); // Record when this touch started
|
|
83
|
+
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// This function runs when a touch ends (finger lifts off screen)
|
|
88
|
+
function touchEnded()
|
|
89
|
+
{
|
|
90
|
+
isCurrentlyTouching = false;
|
|
91
|
+
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Touch Zones - Mobile p5.js</title>
|
|
7
|
+
|
|
8
|
+
<!-- Basic CSS to remove browser defaults and align canvas -->
|
|
9
|
+
<style>
|
|
10
|
+
body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
padding: 0;
|
|
13
|
+
overflow: hidden;
|
|
14
|
+
}
|
|
15
|
+
</style>
|
|
16
|
+
|
|
17
|
+
<!-- Load p5.js library -->
|
|
18
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.10/p5.min.js"></script>
|
|
19
|
+
|
|
20
|
+
<!-- Load the mobile p5.js permissions library -->
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
22
|
+
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<!-- Load the p5.js sketch -->
|
|
26
|
+
<script src="sketch.js"></script>
|
|
27
|
+
</body>
|
|
28
|
+
</html>
|