-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
271 lines (233 loc) · 9.26 KB
/
Copy pathscript.js
File metadata and controls
271 lines (233 loc) · 9.26 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Get DOM elements
const videoElement = document.getElementById('videoElement');
const canvasElement = document.getElementById('canvasElement');
const drawingCanvas = document.getElementById('drawingCanvas');
const loadingScreen = document.getElementById('loadingScreen');
const gestureIndicator = document.getElementById('currentGesture');
const modeIndicator = document.getElementById('currentMode');
const cursor = document.getElementById('cursor');
const canvasCtx = canvasElement.getContext('2d');
const drawingCtx = drawingCanvas.getContext('2d');
// Drawing state
let currentColor = '#FF0000';
let isDrawing = false;
let lastPoint = null;
let currentMode = 'hover';
// Set canvas sizes
function resizeCanvas() {
const container = document.getElementById('container');
canvasElement.width = container.offsetWidth;
canvasElement.height = container.offsetHeight;
drawingCanvas.width = container.offsetWidth;
drawingCanvas.height = container.offsetHeight;
drawingCtx.lineCap = 'round';
drawingCtx.lineJoin = 'round';
drawingCtx.lineWidth = 5;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Color selection
document.querySelectorAll('.color-option').forEach(option => {
option.addEventListener('click', () => {
document.querySelectorAll('.color-option').forEach(o => o.classList.remove('selected'));
option.classList.add('selected');
currentColor = option.dataset.color;
});
});
// Control buttons
document.getElementById('clearBtn').addEventListener('click', () => {
drawingCtx.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);
});
document.getElementById('saveBtn').addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'hand-drawing.png';
link.href = drawingCanvas.toDataURL();
link.click();
});
// Detect gesture based on hand landmarks
function detectGesture(landmarks) {
// Check if finger is extended (tip is above PIP joint)
function isFingerExtended(tipIdx, pipIdx) {
return landmarks[tipIdx].y < landmarks[pipIdx].y;
}
const indexExtended = isFingerExtended(8, 6);
const middleExtended = isFingerExtended(12, 10);
const ringExtended = isFingerExtended(16, 14);
const pinkyExtended = isFingerExtended(20, 18);
const thumbExtended = landmarks[4].x < landmarks[3].x;
// Count extended fingers
const extendedFingers = [
indexExtended,
middleExtended,
ringExtended,
pinkyExtended,
thumbExtended
].filter(Boolean).length;
// Index finger only - Drawing
if (indexExtended && !middleExtended && !ringExtended && !pinkyExtended) {
return 'draw';
}
// Index + Middle - Hover/Select
else if (indexExtended && middleExtended && !ringExtended && !pinkyExtended) {
return 'hover';
}
// All fingers extended - Erase
else if (extendedFingers >= 4) {
return 'erase';
}
return 'none';
}
// Check if point is over color palette
function checkColorSelection(x, y) {
const colorOptions = document.querySelectorAll('.color-option');
colorOptions.forEach(option => {
const rect = option.getBoundingClientRect();
const containerRect = document.getElementById('container').getBoundingClientRect();
// Adjust for container position
const optionX = rect.left - containerRect.left;
const optionY = rect.top - containerRect.top;
const optionWidth = rect.width;
const optionHeight = rect.height;
if (x >= optionX && x <= optionX + optionWidth &&
y >= optionY && y <= optionY + optionHeight) {
document.querySelectorAll('.color-option').forEach(o => o.classList.remove('selected'));
option.classList.add('selected');
currentColor = option.dataset.color;
}
});
}
// Process hand tracking results
function onResults(results) {
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
const landmarks = results.multiHandLandmarks[0];
// Draw hand landmarks
if (typeof drawConnectors !== 'undefined' && typeof HAND_CONNECTIONS !== 'undefined') {
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {color: '#00FF00', lineWidth: 2});
drawLandmarks(canvasCtx, landmarks, {color: '#FF0000', lineWidth: 1, radius: 3});
}
// Get index finger tip position (corrected for mirroring)
const indexTip = landmarks[8];
// For cursor positioning (mirrored for display)
const cursorX = (1 - indexTip.x) * canvasElement.width;
const cursorY = indexTip.y * canvasElement.height;
// For drawing on canvas (canvas is already mirrored, so use original coordinates)
const drawX = indexTip.x * canvasElement.width;
const drawY = indexTip.y * canvasElement.height;
// Update cursor position
cursor.style.left = cursorX + 'px';
cursor.style.top = cursorY + 'px';
cursor.style.backgroundColor = currentColor;
cursor.style.display = 'block';
// Detect gesture
const gesture = detectGesture(landmarks);
gestureIndicator.textContent = gesture.toUpperCase();
if (gesture === 'draw') {
currentMode = 'Drawing';
modeIndicator.textContent = currentMode;
cursor.style.transform = 'scale(1.5)';
if (lastPoint) {
drawingCtx.strokeStyle = currentColor;
drawingCtx.beginPath();
drawingCtx.moveTo(lastPoint.x, lastPoint.y);
drawingCtx.lineTo(drawX, drawY);
drawingCtx.stroke();
}
lastPoint = {x: drawX, y: drawY};
}
else if (gesture === 'hover') {
currentMode = 'Hover/Select';
modeIndicator.textContent = currentMode;
cursor.style.transform = 'scale(1.2)';
lastPoint = null;
// Check color selection using cursor position
checkColorSelection(cursorX, cursorY);
}
else if (gesture === 'erase') {
currentMode = 'Erasing';
modeIndicator.textContent = currentMode;
cursor.style.transform = 'scale(2)';
cursor.style.backgroundColor = 'white';
// Erase in circular area using drawing coordinates
drawingCtx.globalCompositeOperation = 'destination-out';
drawingCtx.beginPath();
drawingCtx.arc(drawX, drawY, 30, 0, Math.PI * 2);
drawingCtx.fill();
drawingCtx.globalCompositeOperation = 'source-over';
lastPoint = null;
}
else {
currentMode = 'Hover';
modeIndicator.textContent = currentMode;
cursor.style.transform = 'scale(1)';
lastPoint = null;
}
} else {
cursor.style.display = 'none';
lastPoint = null;
}
canvasCtx.restore();
}
// Initialize MediaPipe and Camera
function initializeApp() {
// Check if MediaPipe Hands is loaded
if (typeof Hands === 'undefined') {
loadingScreen.innerHTML = `
<h2>Loading Error</h2>
<p>MediaPipe Hands library failed to load</p>
<p>Please check your internet connection and refresh the page</p>
`;
return;
}
// MediaPipe Hands setup
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}
});
hands.setOptions({
maxNumHands: 1,
modelComplexity: 1,
minDetectionConfidence: 0.7,
minTrackingConfidence: 0.7
});
hands.onResults(onResults);
// Camera setup
if (typeof Camera === 'undefined') {
loadingScreen.innerHTML = `
<h2>Loading Error</h2>
<p>Camera utilities failed to load</p>
<p>Please check your internet connection and refresh the page</p>
`;
return;
}
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({image: videoElement});
},
width: 1280,
height: 720
});
camera.start()
.then(() => {
console.log('Camera started successfully');
loadingScreen.classList.add('hidden');
})
.catch(err => {
console.error('Camera error:', err);
loadingScreen.innerHTML = `
<h2>Camera Error</h2>
<p>${err.message}</p>
<p>Please allow camera access and refresh the page</p>
`;
});
}
// Wait for libraries to load before initializing
window.addEventListener('load', () => {
// Give libraries a moment to load
setTimeout(() => {
initializeApp();
}, 1000);
});