Repository navigation
Expand file tree
/
Copy pathtouch_visualizer.smash
More file actions
153 lines (128 loc) 路 4.13 KB
/
Copy pathtouch_visualizer.smash
File metadata and controls
153 lines (128 loc) 路 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// touch_visualizer.smash - Visualize touch events on a canvas
import { input } from "hardware";
import { graphics } from "std";
import { sleep } from "std/time";
// Check if touch input is available
if (!input.isTouchAvailable()) {
print("Touch input is not available on this system.");
print("This example is designed for touch-enabled devices like tablets, phones, or touch screens.");
exit(1);
}
print("Touch Visualizer - Touch the screen to see events (Close window to exit)\n");
// Create a window for visualizing touch events
const window = graphics.createWindow({
title: "Touch Visualizer",
width: 800,
height: 600,
fullscreen: false,
resizable: true
});
// Store active touch points and their trails
const touchPoints = new Map();
const trails = new Map();
const MAX_TRAIL_POINTS = 50;
// Register for touch events
const registration = input.registerEvents(["touch"], fn(event) => {
if (event.type === "touchstart") {
print(`Touch started with ${event.touches.length} points`);
// Add new touch points
event.touches.forEach(fn(touch) => {
touchPoints.set(touch.id, touch);
// Initialize trail for this touch
if (!trails.has(touch.id)) {
trails.set(touch.id, []);
}
// Add point to trail
trails.get(touch.id).push({
x: touch.x,
y: touch.y,
pressure: touch.pressure,
time: Date.now()
});
});
} else if (event.type === "touchmove") {
print(`Touch moved with ${event.touches.length} points`);
// Update touch points
event.touches.forEach(fn(touch) => {
touchPoints.set(touch.id, touch);
// Add point to trail
const trail = trails.get(touch.id) || [];
trail.push({
x: touch.x,
y: touch.y,
pressure: touch.pressure,
time: Date.now()
});
// Limit trail length
if (trail.length > MAX_TRAIL_POINTS) {
trail.shift();
}
trails.set(touch.id, trail);
});
} else if (event.type === "touchend" || event.type === "touchcancel") {
print(`Touch ${event.type === "touchend" ? "ended" : "canceled"} for ${event.touches.length} points`);
// Remove ended touch points
event.touches.forEach(fn(touch) => {
touchPoints.delete(touch.id);
// Keep the trail for a short time before removing
setTimeout(fn() => {
trails.delete(touch.id);
}, 2000);
});
}
});
// Draw function for the window
window.onDraw = (ctx) => {
// Clear the canvas
ctx.fillStyle = "#f0f0f0";
ctx.fillRect(0, 0, window.width, window.height);
// Draw instructions
ctx.fillStyle = "#333333";
ctx.font = "18px sans-serif";
ctx.fillText("Touch the screen to draw", 20, 30);
ctx.fillText(`Active touch points: ${touchPoints.size}`, 20, 60);
// Draw touch trails
trails.forEach((trail, id) => {
if (trail.length > 1) {
ctx.beginPath();
ctx.moveTo(trail[0].x, trail[0].y);
for (let i = 1; i < trail.length; i++) {
ctx.lineTo(trail[i].x, trail[i].y);
}
// Use different colors for different touch points
const hue = (id * 137) % 360; // Generate a unique hue based on touch ID
ctx.strokeStyle = `hsl(${hue}, 80%, 50%)`;
ctx.lineWidth = 3;
ctx.stroke();
}
});
// Draw active touch points
touchPoints.forEach(touch => {
const hue = (touch.id * 137) % 360;
ctx.fillStyle = `hsl(${hue}, 80%, 50%)`;
// Size based on pressure
const radius = 10 + (touch.pressure * 20);
ctx.beginPath();
ctx.arc(touch.x, touch.y, radius, 0, Math.PI * 2);
ctx.fill();
// Draw touch ID
ctx.fillStyle = "#ffffff";
ctx.font = "12px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(touch.id.toString(), touch.x, touch.y);
});
};
// Keep the program running until window is closed
try {
while (window.isOpen()) {
window.update();
await sleep(16); // ~60 FPS
}
} finally {
// Always clean up by unregistering events
print("Unregistering touch events...");
input.unregisterEvents(registration);
window.close();
print("Done.");
}