-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
503 lines (431 loc) · 21.2 KB
/
index.html
File metadata and controls
503 lines (431 loc) · 21.2 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Orbit- Hand Controlling</title>
<script defer src="https://cloud.umami.is/script.js" data-website-id="2ecfa36b-eeb7-4e11-9daa-4da4cadcf2c9"></script>
<style>
body {
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
.container {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
#webcam {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
transform: scaleX(-1); /* Mirror the webcam feed */
}
#canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform: scaleX(-1); /* Mirror the canvas to match webcam */
pointer-events: none;
}
#three-canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 5;
}
#status {
position: absolute;
top: 20px;
left: 20px;
color: #fff;
background-color: rgba(0, 0, 0, 0.5);
padding: 10px;
border-radius: 5px;
font-family: Arial, sans-serif;
z-index: 10;
}
#links-para{
position: absolute;
bottom: 5px;
left: 5px;
font-family: Helvetica, sans-serif;
font-size: 16px;
background-color: rgba(255, 255, 255, 0.5);
padding: 10px;
}
a {
color: #0000FF !important;
}
</style>
</head>
<body>
<div class="container">
<video id="webcam" autoplay playsinline></video>
<canvas id="canvas"></canvas>
<div id="three-canvas"></div>
<div id="status">Loading MediaPipe...</div>
</div>
<p id="links-para"><a href="https://abhin.pages.dev" target="_blank">Portfolio</a> | <a href="https://www.instagram.com/abhin.io/" target="_blank">Instagram</a> | <a href="https://github.com/dearabhin/Orbit" target="_blank">GitHub</a> | <a href="https://www.linkedin.com/in/abhin-krishna/" target="_blank">LinkedIn</a></p>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const videoElement = document.getElementById('webcam');
const canvasElement = document.getElementById('canvas');
const canvasCtx = canvasElement.getContext('2d');
const statusElement = document.getElementById('status');
const threeContainer = document.getElementById('three-canvas');
// Three.js variables
let scene, camera, renderer;
let sphere, solidMesh, wireframeMesh;
// Hand tracking variables
let rightHandActive = false;
let leftHandActive = false;
let lastColorChangeTime = 0;
const colorChangeDelay = 500; // milliseconds
let currentSphereSize = 1.0; // Default/starting size
let targetSphereSize = 1.0; // Target size based on hand gesture
const smoothingFactor = 0.15; // Adjust this value between 0-1 (lower = smoother but slower)
// Keep canvas size in sync with window size
function updateCanvasSize() {
canvasElement.width = window.innerWidth;
canvasElement.height = window.innerHeight;
}
// Initialize and properly set element sizes
function initializeLayout() {
updateCanvasSize();
}
// Update layout when window is resized
window.addEventListener('resize', () => {
initializeLayout();
if (renderer) {
renderer.setSize(window.innerWidth, window.innerHeight);
}
if (camera) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
});
// Set initial layout
initializeLayout();
// Initialize webcam
async function initWebcam() {
try {
// Try to get a high resolution stream
const stream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 1920 },
height: { ideal: 1080 },
facingMode: 'user'
}
});
videoElement.srcObject = stream;
return new Promise((resolve) => {
videoElement.onloadedmetadata = () => {
// Ensure layout is updated when video loads
initializeLayout();
resolve(videoElement);
};
});
} catch (error) {
statusElement.textContent = `Error accessing webcam: ${error.message}`;
console.error('Error accessing webcam:', error);
throw error;
}
}
// Helper function to generate random neon colors
function getRandomNeonColor() {
const neonColors = [
0xFF00FF, // Magenta
0x00FFFF, // Cyan
0xFF3300, // Neon Orange
0x39FF14, // Neon Green
0xFF0099, // Neon Pink
0x00FF00, // Lime
0xFF6600, // Neon Orange-Red
0xFFFF00 // Yellow
];
return neonColors[Math.floor(Math.random() * neonColors.length)];
}
// Initialize Three.js
function initThreeJS() {
// Create scene
scene = new THREE.Scene();
// Create camera
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 5;
// Create renderer with transparent background
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true // Enable transparency
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0); // Transparent background
threeContainer.appendChild(renderer.domElement);
// Create a sphere with vibrant neon color fill and white wireframe
const geometry = new THREE.SphereGeometry(2, 32, 32);
// Create a group to hold both the solid and wireframe meshes
sphere = new THREE.Group();
// Create solid mesh with neon color and slight transparency
const solidMaterial = new THREE.MeshBasicMaterial({
color: 0xff00ff, // Initial color
transparent: true,
opacity: 0.5
});
solidMesh = new THREE.Mesh(geometry, solidMaterial);
sphere.add(solidMesh);
// Create wireframe mesh with white color
const wireframeMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: true,
transparent: false,
});
wireframeMesh = new THREE.Mesh(geometry, wireframeMaterial);
sphere.add(wireframeMesh);
scene.add(sphere);
// Add some ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 1.0);
scene.add(ambientLight);
// Start animation loop
animate();
}
// Animation loop for Three.js
function animate() {
requestAnimationFrame(animate);
// Rotate the sphere continuously, regardless of hand interaction
if (sphere) {
// Remove the condition that stops rotation during hand interaction
sphere.rotation.x += 0.003;
sphere.rotation.y += 0.008;
// Add pulsing glow effect to the neon fill
const time = Date.now() * 0.001; // Convert to seconds
const pulseIntensity = 0.1 * Math.sin(time * 2) + 0.9; // Oscillate between 0.8 and 1.0
// Apply pulse to opacity
if (solidMesh && solidMesh.material) {
solidMesh.material.opacity = 0.4 + 0.1 * pulseIntensity;
}
}
// Render the scene
renderer.render(scene, camera);
}
// Calculate distance between two 3D points
function calculateDistance(point1, point2) {
const dx = point1.x - point2.x;
const dy = point1.y - point2.y;
const dz = point1.z - point2.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
// Detect if a point is inside/near the sphere
function isPointInSphere(point) {
// Convert normalized coordinates to world space
const worldX = (point.x - 0.5) * 10;
const worldY = (0.5 - point.y) * 10; // Invert Y to match Three.js coordinate system
const worldZ = 0; // Assume point is on Z=0 plane
// Get sphere position in world space
const spherePos = new THREE.Vector3();
sphere.getWorldPosition(spherePos);
// Calculate distance between point and sphere center
const distance = Math.sqrt(
Math.pow(worldX - spherePos.x, 2) +
Math.pow(worldY - spherePos.y, 2) +
Math.pow(worldZ - spherePos.z, 2)
);
// Consider interaction if point is within or near the sphere surface
// Using the current sphere size for detection
const currentSize = sphere.scale.x * 2;
return distance < currentSize * 1; // Add some buffer for easier interaction
}
// Initialize MediaPipe Hands
async function initMediaPipeHands() {
statusElement.textContent = 'Initializing MediaPipe Hands...';
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}
});
hands.setOptions({
maxNumHands: 2,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
await hands.initialize();
statusElement.textContent = 'Hand tracking ready!';
return hands;
}
// Draw hand landmarks on canvas with dynamic sizing
function drawLandmarks(landmarks, isLeft) {
// Adjust line width and point size based on screen dimension
const screenSize = Math.min(window.innerWidth, window.innerHeight);
const lineWidth = Math.max(2, Math.min(5, screenSize / 300));
const pointSize = Math.max(2, Math.min(8, screenSize / 250));
// Define connections between landmarks
const connections = [
// Thumb
[0, 1], [1, 2], [2, 3], [3, 4],
// Index finger
[0, 5], [5, 6], [6, 7], [7, 8],
// Middle finger
[0, 9], [9, 10], [10, 11], [11, 12],
// Ring finger
[0, 13], [13, 14], [14, 15], [15, 16],
// Pinky
[0, 17], [17, 18], [18, 19], [19, 20],
// Palm
[0, 5], [5, 9], [9, 13], [13, 17]
];
// Choose a different color for each hand
const handColor = isLeft ? '#00FF00' : '#00FFFF';
// Draw connections
canvasCtx.lineWidth = lineWidth;
canvasCtx.strokeStyle = handColor;
connections.forEach(([i, j]) => {
const start = landmarks[i];
const end = landmarks[j];
canvasCtx.beginPath();
canvasCtx.moveTo(start.x * canvasElement.width, start.y * canvasElement.height);
canvasCtx.lineTo(end.x * canvasElement.width, end.y * canvasElement.height);
canvasCtx.stroke();
});
// Draw landmarks
landmarks.forEach((landmark, index) => {
// Special color for thumb tip (index 4) and index finger tip (index 8)
let pointColor = handColor;
if (index === 4 || index === 8) {
pointColor = '#FF0000';
}
canvasCtx.fillStyle = pointColor;
canvasCtx.beginPath();
canvasCtx.arc(
landmark.x * canvasElement.width,
landmark.y * canvasElement.height,
pointSize * 1.2, // Make thumb and index fingertips slightly larger
0,
2 * Math.PI
);
canvasCtx.fill();
});
}
// Process video frames
function onResults(results) {
// Clear canvas
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
// Make sure canvas size matches window
if (canvasElement.width !== window.innerWidth ||
canvasElement.height !== window.innerHeight) {
updateCanvasSize();
}
// Reset the tracking flags for this frame
rightHandActive = false;
leftHandActive = false;
// Process hand landmarks if detected
if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
// Update status text
statusElement.textContent =
results.multiHandLandmarks.length === 1 ? '1 hand detected' :
`${results.multiHandLandmarks.length} hands detected`;
// Process each hand
for (let handIndex = 0; handIndex < results.multiHandLandmarks.length; handIndex++) {
const landmarks = results.multiHandLandmarks[handIndex];
const handedness = results.multiHandedness[handIndex].label;
const isLeftHand = handedness === 'Left';
// Draw the hand landmarks with appropriate color
drawLandmarks(landmarks, isLeftHand);
if (!isLeftHand) {
// RIGHT HAND: Control sphere size with thumb-index distance
// Get thumb tip and index finger tip positions
const thumbTip = landmarks[4];
const indexTip = landmarks[8];
// Calculate the distance between thumb and index finger
const pinchDistance = calculateDistance(thumbTip, indexTip);
// Map the pinch distance to a sphere size (adjusted for better UX)
// Smaller distance = smaller sphere, larger distance = larger sphere
// Calibrate distance range based on reasonable hand movement
// Normal pinch is around 0.05-0.1, open hand is 0.2-0.3
if (pinchDistance < 0.05) {
targetSphereSize = 0.2; // Minimum size when pinched
} else if (pinchDistance > 0.25) {
targetSphereSize = 2.0; // Maximum size when fully open
} else {
// Linear mapping from pinch distance to sphere size
targetSphereSize = 0.2 + (pinchDistance - 0.05) * (2.0 - 0.2) / (0.25 - 0.05);
}
// Apply smooth interpolation toward the target size
currentSphereSize = currentSphereSize + (targetSphereSize - currentSphereSize) * smoothingFactor;
// Apply the smoothed size to the sphere
if (sphere) {
sphere.scale.set(currentSphereSize, currentSphereSize, currentSphereSize);
}
rightHandActive = true;
} else {
// LEFT HAND: Change color when index finger touches sphere
// Get index finger tip position (landmark 8)
const indexTip = landmarks[8];
// Check if index finger is touching the sphere
if (isPointInSphere(indexTip)) {
// Change color with a cooldown to prevent rapid changes
const currentTime = Date.now();
if (currentTime - lastColorChangeTime > colorChangeDelay) {
// Change to a random neon color
const newColor = getRandomNeonColor();
if (solidMesh && solidMesh.material) {
solidMesh.material.color.setHex(newColor);
}
lastColorChangeTime = currentTime;
}
leftHandActive = true;
}
}
}
} else {
statusElement.textContent = 'No hands detected';
}
}
// Initialize and start the app
async function startApp() {
try {
await initWebcam();
initThreeJS(); // Initialize Three.js
const hands = await initMediaPipeHands();
hands.onResults(onResults);
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({image: videoElement});
},
width: 1920,
height: 1080
});
camera.start();
} catch (error) {
statusElement.textContent = `Error: ${error.message}`;
console.error('Error starting application:', error);
}
}
// Start the application
startApp();
});
</script>
</body>
</html>