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,78 @@
|
|
|
1
|
+
// MINIMAL VERSION - Touch Zones
|
|
2
|
+
// No visual feedback - data displayed in debug panel only
|
|
3
|
+
// Demonstrates: Detecting which zone of the screen is touched (divided into 4 quadrants)
|
|
4
|
+
|
|
5
|
+
// Global variables for zone detection
|
|
6
|
+
let xDivision = 2; // Number of columns (2 = left/right)
|
|
7
|
+
let yDivision = 2; // Number of rows (2 = top/bottom)
|
|
8
|
+
let zoneCounters = [0, 0, 0, 0]; // Touch counters for each zone
|
|
9
|
+
|
|
10
|
+
function setup()
|
|
11
|
+
{
|
|
12
|
+
createCanvas(windowWidth, windowHeight);
|
|
13
|
+
|
|
14
|
+
// Show debug panel FIRST
|
|
15
|
+
showDebug();
|
|
16
|
+
|
|
17
|
+
// Lock mobile gestures
|
|
18
|
+
lockGestures();
|
|
19
|
+
|
|
20
|
+
debug("Touch Zones - Minimal Version");
|
|
21
|
+
debug("Screen divided into " + (xDivision * yDivision) + " zones");
|
|
22
|
+
debug("Zone 0: Top-Left");
|
|
23
|
+
debug("Zone 1: Top-Right");
|
|
24
|
+
debug("Zone 2: Bottom-Left");
|
|
25
|
+
debug("Zone 3: Bottom-Right");
|
|
26
|
+
debug("Touch anywhere to see which zone");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function draw()
|
|
30
|
+
{
|
|
31
|
+
// No visual feedback in minimal version
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Helper function to determine which zone a touch is in
|
|
35
|
+
function getZone(x, y)
|
|
36
|
+
{
|
|
37
|
+
// Calculate which column and row the touch is in
|
|
38
|
+
let col = floor(x / (width / xDivision));
|
|
39
|
+
let row = floor(y / (height / yDivision));
|
|
40
|
+
|
|
41
|
+
// Convert to zone number (0-3)
|
|
42
|
+
let zone = row * xDivision + col;
|
|
43
|
+
|
|
44
|
+
// Make sure zone is within valid range
|
|
45
|
+
zone = constrain(zone, 0, (xDivision * yDivision) - 1);
|
|
46
|
+
|
|
47
|
+
return zone;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Prevent default touch behavior and unwanted gestures
|
|
51
|
+
function touchStarted()
|
|
52
|
+
{
|
|
53
|
+
// Check all active touches
|
|
54
|
+
for (let i = 0; i < touches.length; i++)
|
|
55
|
+
{
|
|
56
|
+
let x = touches[i].x;
|
|
57
|
+
let y = touches[i].y;
|
|
58
|
+
|
|
59
|
+
// Get zone for this touch
|
|
60
|
+
let zone = getZone(x, y);
|
|
61
|
+
|
|
62
|
+
// Increment counter for this zone
|
|
63
|
+
zoneCounters[zone] = zoneCounters[zone] + 1;
|
|
64
|
+
|
|
65
|
+
// Output to debug panel
|
|
66
|
+
debug("--- Touch in Zone " + zone + " ---");
|
|
67
|
+
debug("Position: (" + int(x) + ", " + int(y) + ")");
|
|
68
|
+
debug("Zone " + zone + " Total Touches: " + zoneCounters[zone]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return false; // Prevents default behavior
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Prevent default touch behavior and unwanted gestures
|
|
75
|
+
function touchEnded()
|
|
76
|
+
{
|
|
77
|
+
return false; // Prevents default behavior
|
|
78
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>P5.js Mobile - Touch Count (Minimal)</title>
|
|
5
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.0/lib/p5.min.js"></script>
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
padding: 0;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<script src="sketch.js"></script>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// MINIMAL VERSION - Touch Count
|
|
2
|
+
// No visual feedback - data displayed in debug panel only
|
|
3
|
+
// Demonstrates: Counting simultaneous touches on the screen
|
|
4
|
+
|
|
5
|
+
// Global variable for tracking touch count
|
|
6
|
+
let currentTouchCount = 0;
|
|
7
|
+
|
|
8
|
+
function setup()
|
|
9
|
+
{
|
|
10
|
+
createCanvas(windowWidth, windowHeight);
|
|
11
|
+
|
|
12
|
+
// Show debug panel FIRST
|
|
13
|
+
showDebug();
|
|
14
|
+
|
|
15
|
+
// Lock mobile gestures
|
|
16
|
+
lockGestures();
|
|
17
|
+
|
|
18
|
+
debug("Touch Count - Minimal Version");
|
|
19
|
+
debug("Use multiple fingers to touch the screen");
|
|
20
|
+
debug("Waiting for touches...");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function draw()
|
|
24
|
+
{
|
|
25
|
+
// No visual feedback in minimal version
|
|
26
|
+
|
|
27
|
+
// Update touch count
|
|
28
|
+
let newTouchCount = touches.length;
|
|
29
|
+
|
|
30
|
+
// Only output to debug if count changed
|
|
31
|
+
if (newTouchCount !== currentTouchCount)
|
|
32
|
+
{
|
|
33
|
+
currentTouchCount = newTouchCount;
|
|
34
|
+
|
|
35
|
+
if (currentTouchCount > 0)
|
|
36
|
+
{
|
|
37
|
+
debug("--- Touch Count Changed ---");
|
|
38
|
+
debug("Current Touches: " + currentTouchCount);
|
|
39
|
+
|
|
40
|
+
// List all touch positions
|
|
41
|
+
for (let i = 0; i < touches.length; i++)
|
|
42
|
+
{
|
|
43
|
+
debug("Touch " + (i + 1) + ": (" + int(touches[i].x) + ", " + int(touches[i].y) + ")");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
{
|
|
48
|
+
debug("All touches ended");
|
|
49
|
+
debug("Waiting for touches...");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Prevent default touch behavior and unwanted gestures
|
|
55
|
+
function touchStarted()
|
|
56
|
+
{
|
|
57
|
+
return false; // Prevents default behavior
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Prevent default touch behavior and unwanted gestures
|
|
61
|
+
function touchEnded()
|
|
62
|
+
{
|
|
63
|
+
return false; // Prevents default behavior
|
|
64
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>P5.js Mobile - Touch Distance (Minimal)</title>
|
|
5
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.0/lib/p5.min.js"></script>
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
padding: 0;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<script src="sketch.js"></script>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// MINIMAL VERSION - Touch Distance
|
|
2
|
+
// No visual feedback - data displayed in debug panel only
|
|
3
|
+
// Demonstrates: Measuring distance between two simultaneous touches
|
|
4
|
+
|
|
5
|
+
function setup()
|
|
6
|
+
{
|
|
7
|
+
createCanvas(windowWidth, windowHeight);
|
|
8
|
+
|
|
9
|
+
// Show debug panel FIRST
|
|
10
|
+
showDebug();
|
|
11
|
+
|
|
12
|
+
// Lock mobile gestures
|
|
13
|
+
lockGestures();
|
|
14
|
+
|
|
15
|
+
debug("Touch Distance - Minimal Version");
|
|
16
|
+
debug("Use two fingers to touch the screen");
|
|
17
|
+
debug("Waiting for two touches...");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function draw()
|
|
21
|
+
{
|
|
22
|
+
// No visual feedback in minimal version
|
|
23
|
+
|
|
24
|
+
// Only measure when there are exactly 2 touches
|
|
25
|
+
if (touches.length === 2)
|
|
26
|
+
{
|
|
27
|
+
// Get positions of both touches
|
|
28
|
+
let x1 = touches[0].x;
|
|
29
|
+
let y1 = touches[0].y;
|
|
30
|
+
let x2 = touches[1].x;
|
|
31
|
+
let y2 = touches[1].y;
|
|
32
|
+
|
|
33
|
+
// Calculate distance using Pythagorean theorem
|
|
34
|
+
let distance = dist(x1, y1, x2, y2);
|
|
35
|
+
|
|
36
|
+
// Output to debug panel
|
|
37
|
+
debug("--- Distance Measurement ---");
|
|
38
|
+
debug("Touch 1: (" + int(x1) + ", " + int(y1) + ")");
|
|
39
|
+
debug("Touch 2: (" + int(x2) + ", " + int(y2) + ")");
|
|
40
|
+
debug("Distance: " + int(distance) + " pixels");
|
|
41
|
+
}
|
|
42
|
+
else if (touches.length === 0)
|
|
43
|
+
{
|
|
44
|
+
// No touches
|
|
45
|
+
debug("Waiting for two touches...");
|
|
46
|
+
}
|
|
47
|
+
else if (touches.length === 1)
|
|
48
|
+
{
|
|
49
|
+
// Only one touch
|
|
50
|
+
debug("Need one more finger (1/2 touches)");
|
|
51
|
+
}
|
|
52
|
+
else if (touches.length > 2)
|
|
53
|
+
{
|
|
54
|
+
// Too many touches
|
|
55
|
+
debug("Too many touches! Use only 2 fingers");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Prevent default touch behavior and unwanted gestures
|
|
60
|
+
function touchStarted()
|
|
61
|
+
{
|
|
62
|
+
return false; // Prevents default behavior
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Prevent default touch behavior and unwanted gestures
|
|
66
|
+
function touchEnded()
|
|
67
|
+
{
|
|
68
|
+
return false; // Prevents default behavior
|
|
69
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>P5.js Mobile - Touch Angle (Minimal)</title>
|
|
5
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.0/lib/p5.min.js"></script>
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
padding: 0;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<script src="sketch.js"></script>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// MINIMAL VERSION - Touch Angle
|
|
2
|
+
// No visual feedback - data displayed in debug panel only
|
|
3
|
+
// Demonstrates: Calculating the angle between two simultaneous touches
|
|
4
|
+
|
|
5
|
+
function setup()
|
|
6
|
+
{
|
|
7
|
+
createCanvas(windowWidth, windowHeight);
|
|
8
|
+
|
|
9
|
+
// Show debug panel FIRST
|
|
10
|
+
showDebug();
|
|
11
|
+
|
|
12
|
+
// Lock mobile gestures
|
|
13
|
+
lockGestures();
|
|
14
|
+
|
|
15
|
+
debug("Touch Angle - Minimal Version");
|
|
16
|
+
debug("Use two fingers to touch the screen");
|
|
17
|
+
debug("Waiting for two touches...");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function draw()
|
|
21
|
+
{
|
|
22
|
+
// No visual feedback in minimal version
|
|
23
|
+
|
|
24
|
+
// Only measure when there are exactly 2 touches
|
|
25
|
+
if (touches.length === 2)
|
|
26
|
+
{
|
|
27
|
+
// Get positions of both touches
|
|
28
|
+
let x1 = touches[0].x;
|
|
29
|
+
let y1 = touches[0].y;
|
|
30
|
+
let x2 = touches[1].x;
|
|
31
|
+
let y2 = touches[1].y;
|
|
32
|
+
|
|
33
|
+
// Calculate angle from touch 1 to touch 2
|
|
34
|
+
let angleRadians = atan2(y2 - y1, x2 - x1);
|
|
35
|
+
let angleDegrees = degrees(angleRadians);
|
|
36
|
+
|
|
37
|
+
// Normalize angle to 0-360 range
|
|
38
|
+
if (angleDegrees < 0)
|
|
39
|
+
{
|
|
40
|
+
angleDegrees = angleDegrees + 360;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Output to debug panel
|
|
44
|
+
debug("--- Angle Measurement ---");
|
|
45
|
+
debug("Touch 1: (" + int(x1) + ", " + int(y1) + ")");
|
|
46
|
+
debug("Touch 2: (" + int(x2) + ", " + int(y2) + ")");
|
|
47
|
+
debug("Angle: " + int(angleDegrees) + "°");
|
|
48
|
+
debug("Radians: " + nf(angleRadians, 1, 2));
|
|
49
|
+
|
|
50
|
+
// Add directional hint
|
|
51
|
+
let direction = "";
|
|
52
|
+
if (angleDegrees < 45 || angleDegrees >= 315) direction = "Right";
|
|
53
|
+
else if (angleDegrees < 135) direction = "Down";
|
|
54
|
+
else if (angleDegrees < 225) direction = "Left";
|
|
55
|
+
else direction = "Up";
|
|
56
|
+
debug("Direction: " + direction);
|
|
57
|
+
}
|
|
58
|
+
else if (touches.length === 0)
|
|
59
|
+
{
|
|
60
|
+
// No touches
|
|
61
|
+
debug("Waiting for two touches...");
|
|
62
|
+
}
|
|
63
|
+
else if (touches.length === 1)
|
|
64
|
+
{
|
|
65
|
+
// Only one touch
|
|
66
|
+
debug("Need one more finger (1/2 touches)");
|
|
67
|
+
}
|
|
68
|
+
else if (touches.length > 2)
|
|
69
|
+
{
|
|
70
|
+
// Too many touches
|
|
71
|
+
debug("Too many touches! Use only 2 fingers");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Prevent default touch behavior and unwanted gestures
|
|
76
|
+
function touchStarted()
|
|
77
|
+
{
|
|
78
|
+
return false; // Prevents default behavior
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Prevent default touch behavior and unwanted gestures
|
|
82
|
+
function touchEnded()
|
|
83
|
+
{
|
|
84
|
+
return false; // Prevents default behavior
|
|
85
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Touch Collision Detection Example
|
|
2
|
+
|
|
3
|
+
This example demonstrates using `collidePointRect()` from the p5.collide2D library to detect touches on multiple moving objects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
- Multiple space people moving around the screen
|
|
7
|
+
- Each object independently detects touches using collision detection
|
|
8
|
+
- Objects bounce off screen edges
|
|
9
|
+
- Individual and total touch counters
|
|
10
|
+
- Visual feedback when touched
|
|
11
|
+
|
|
12
|
+
## Setup
|
|
13
|
+
|
|
14
|
+
1. The space suit image is already included at `gifs/spaceSuit2.png`
|
|
15
|
+
2. Open `index.html` in a browser or on a mobile device
|
|
16
|
+
|
|
17
|
+
If the image fails to load, the example will display simple placeholder astronaut shapes.
|
|
18
|
+
|
|
19
|
+
## How It Works
|
|
20
|
+
|
|
21
|
+
- **SpacePerson Class**: Simple class that handles position, movement, collision detection, and display
|
|
22
|
+
- **collidePointRect()**: Detects if a touch point (x, y) is inside each object's rectangular bounds
|
|
23
|
+
- **No Screen Division**: Each object can be touched anywhere without dividing the screen into zones
|
|
24
|
+
|
|
25
|
+
## Customization
|
|
26
|
+
|
|
27
|
+
In `sketch.js` you can change:
|
|
28
|
+
- Number of space people: Modify the loop in `setup()` (currently 5)
|
|
29
|
+
- Size: Change `this.width` and `this.height` in the class
|
|
30
|
+
- Speed: Adjust the `random()` values for `speedX` and `speedY`
|
|
31
|
+
- Colors: Modify the `backgroundColor` values
|
|
Binary file
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>P5.js Mobile - Touch Collision with GIF</title>
|
|
5
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.0/lib/p5.min.js"></script>
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
7
|
+
<script src="https://cdn.jsdelivr.net/gh/bmoren/p5.collide2D/p5.collide2d.min.js"></script>
|
|
8
|
+
<style>
|
|
9
|
+
body {
|
|
10
|
+
margin: 0;
|
|
11
|
+
padding: 0;
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
}
|
|
14
|
+
</style>
|
|
15
|
+
</head>
|
|
16
|
+
<body>
|
|
17
|
+
<script src="sketch.js"></script>
|
|
18
|
+
</body>
|
|
19
|
+
</html>
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Touch Collision Detection with Multiple Objects
|
|
2
|
+
// Demonstrates using collidePointRect() to detect touches on multiple moving objects
|
|
3
|
+
// Each space person can be touched independently!
|
|
4
|
+
|
|
5
|
+
// Global variables
|
|
6
|
+
let spaceSuitImg;
|
|
7
|
+
let spacePeople = []; // Array to hold multiple space person objects
|
|
8
|
+
let totalTouches = 0;
|
|
9
|
+
let backgroundColor;
|
|
10
|
+
|
|
11
|
+
// Simple SpacePerson class
|
|
12
|
+
class SpacePerson
|
|
13
|
+
{
|
|
14
|
+
constructor(x, y)
|
|
15
|
+
{
|
|
16
|
+
this.x = x;
|
|
17
|
+
this.y = y;
|
|
18
|
+
this.width = 120;
|
|
19
|
+
this.height = 120;
|
|
20
|
+
this.speedX = random(-2, 2);
|
|
21
|
+
this.speedY = random(-2, 2);
|
|
22
|
+
this.isTouched = false;
|
|
23
|
+
this.celebrationTimer = 0;
|
|
24
|
+
this.touchCount = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
update()
|
|
28
|
+
{
|
|
29
|
+
// Move the space person
|
|
30
|
+
this.x = this.x + this.speedX;
|
|
31
|
+
this.y = this.y + this.speedY;
|
|
32
|
+
|
|
33
|
+
// Bounce off edges
|
|
34
|
+
if (this.x - this.width/2 < 0 || this.x + this.width/2 > width)
|
|
35
|
+
{
|
|
36
|
+
this.speedX = this.speedX * -1;
|
|
37
|
+
}
|
|
38
|
+
if (this.y - this.height/2 < 0 || this.y + this.height/2 > height)
|
|
39
|
+
{
|
|
40
|
+
this.speedY = this.speedY * -1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Update celebration timer
|
|
44
|
+
if (this.celebrationTimer > 0)
|
|
45
|
+
{
|
|
46
|
+
this.celebrationTimer = this.celebrationTimer - 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
display()
|
|
51
|
+
{
|
|
52
|
+
// Draw border (green if touched, white otherwise)
|
|
53
|
+
if (this.isTouched || this.celebrationTimer > 0)
|
|
54
|
+
{
|
|
55
|
+
stroke(100, 255, 100);
|
|
56
|
+
strokeWeight(6);
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
{
|
|
60
|
+
stroke(255);
|
|
61
|
+
strokeWeight(2);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fill(255, 255, 255, 0);
|
|
65
|
+
rectMode(CORNER);
|
|
66
|
+
rect(this.x - this.width/2, this.y - this.height/2, this.width, this.height);
|
|
67
|
+
|
|
68
|
+
// Display the space suit image (or placeholder if not loaded)
|
|
69
|
+
noStroke();
|
|
70
|
+
imageMode(CENTER);
|
|
71
|
+
if (spaceSuitImg.width > 0)
|
|
72
|
+
{
|
|
73
|
+
image(spaceSuitImg, this.x, this.y, this.width, this.height);
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
{
|
|
77
|
+
// Placeholder: draw a simple astronaut shape
|
|
78
|
+
fill(220);
|
|
79
|
+
circle(this.x, this.y - 15, 40); // helmet
|
|
80
|
+
fill(180);
|
|
81
|
+
rect(this.x - 20, this.y, 40, 50); // body
|
|
82
|
+
fill(220);
|
|
83
|
+
rect(this.x - 25, this.y + 10, 10, 30); // left arm
|
|
84
|
+
rect(this.x + 15, this.y + 10, 10, 30); // right arm
|
|
85
|
+
fill(100, 150, 255);
|
|
86
|
+
circle(this.x, this.y - 15, 25); // visor
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Show touch count for this space person
|
|
90
|
+
fill(255);
|
|
91
|
+
stroke(0);
|
|
92
|
+
strokeWeight(3);
|
|
93
|
+
textSize(20);
|
|
94
|
+
textAlign(CENTER, CENTER);
|
|
95
|
+
text(this.touchCount, this.x, this.y - this.height/2 - 20);
|
|
96
|
+
|
|
97
|
+
// Reset touched state
|
|
98
|
+
this.isTouched = false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
checkTouch(touchX, touchY)
|
|
102
|
+
{
|
|
103
|
+
// Calculate the top-left corner for collidePointRect
|
|
104
|
+
let rectX = this.x - this.width/2;
|
|
105
|
+
let rectY = this.y - this.height/2;
|
|
106
|
+
|
|
107
|
+
// Use collidePointRect to check if touch is inside bounds
|
|
108
|
+
if (collidePointRect(touchX, touchY, rectX, rectY, this.width, this.height))
|
|
109
|
+
{
|
|
110
|
+
this.isTouched = true;
|
|
111
|
+
this.touchCount = this.touchCount + 1;
|
|
112
|
+
this.celebrationTimer = 20;
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function preload()
|
|
120
|
+
{
|
|
121
|
+
// Load the space suit image
|
|
122
|
+
spaceSuitImg = loadImage('gifs/spaceSuit2.png');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function setup()
|
|
126
|
+
{
|
|
127
|
+
createCanvas(windowWidth, windowHeight);
|
|
128
|
+
backgroundColor = color(20, 20, 40);
|
|
129
|
+
|
|
130
|
+
// Lock mobile gestures to prevent browser interference
|
|
131
|
+
lockGestures();
|
|
132
|
+
|
|
133
|
+
// Create 5 space people at random positions
|
|
134
|
+
for (let i = 0; i < 5; i = i + 1)
|
|
135
|
+
{
|
|
136
|
+
let x = random(100, width - 100);
|
|
137
|
+
let y = random(100, height - 100);
|
|
138
|
+
spacePeople.push(new SpacePerson(x, y));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function draw()
|
|
143
|
+
{
|
|
144
|
+
background(backgroundColor);
|
|
145
|
+
|
|
146
|
+
// Update and display all space people
|
|
147
|
+
for (let i = 0; i < spacePeople.length; i = i + 1)
|
|
148
|
+
{
|
|
149
|
+
spacePeople[i].update();
|
|
150
|
+
spacePeople[i].display();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Display total touch counter
|
|
154
|
+
fill(255);
|
|
155
|
+
stroke(0);
|
|
156
|
+
strokeWeight(4);
|
|
157
|
+
textSize(32);
|
|
158
|
+
textAlign(CENTER, CENTER);
|
|
159
|
+
text("Total Touches: " + totalTouches, width/2, 40);
|
|
160
|
+
|
|
161
|
+
// Instructions
|
|
162
|
+
textSize(20);
|
|
163
|
+
text("Touch the space people!", width/2, height - 50);
|
|
164
|
+
|
|
165
|
+
// Show collision detection info
|
|
166
|
+
textSize(16);
|
|
167
|
+
fill(200);
|
|
168
|
+
text("Using collidePointRect() - each object detects touches independently", width/2, height - 20);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function touchStarted()
|
|
172
|
+
{
|
|
173
|
+
// Check all touch points
|
|
174
|
+
for (let i = 0; i < touches.length; i = i + 1)
|
|
175
|
+
{
|
|
176
|
+
let touchX = touches[i].x;
|
|
177
|
+
let touchY = touches[i].y;
|
|
178
|
+
|
|
179
|
+
// Check each space person for collision with this touch
|
|
180
|
+
for (let j = 0; j < spacePeople.length; j = j + 1)
|
|
181
|
+
{
|
|
182
|
+
if (spacePeople[j].checkTouch(touchX, touchY))
|
|
183
|
+
{
|
|
184
|
+
totalTouches = totalTouches + 1;
|
|
185
|
+
// Flash the background
|
|
186
|
+
backgroundColor = color(50, 50, 100);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Prevent default touch behavior and unwanted gestures
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function touchMoved()
|
|
196
|
+
{
|
|
197
|
+
// Check touches during movement too
|
|
198
|
+
for (let i = 0; i < touches.length; i = i + 1)
|
|
199
|
+
{
|
|
200
|
+
let touchX = touches[i].x;
|
|
201
|
+
let touchY = touches[i].y;
|
|
202
|
+
|
|
203
|
+
// Check each space person
|
|
204
|
+
for (let j = 0; j < spacePeople.length; j = j + 1)
|
|
205
|
+
{
|
|
206
|
+
spacePeople[j].checkTouch(touchX, touchY);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Prevent default touch behavior and unwanted gestures
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function touchEnded()
|
|
215
|
+
{
|
|
216
|
+
// Reset background color when touch ends
|
|
217
|
+
backgroundColor = color(20, 20, 40);
|
|
218
|
+
|
|
219
|
+
// Prevent default touch behavior and unwanted gestures
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function windowResized()
|
|
224
|
+
{
|
|
225
|
+
resizeCanvas(windowWidth, windowHeight);
|
|
226
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>P5.js Mobile - Touch with GIF</title>
|
|
5
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.0/lib/p5.min.js"></script>
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/mobile-p5-permissions@1.4.4/dist/p5.mobile-permissions.min.js"></script>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
padding: 0;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<script src="sketch.js"></script>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|