File size: 5,283 Bytes
065d164 |
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 |
/**
* Utility functions for hand detection and drawing
*/
/**
* Draw hand landmarks on canvas
* @param {CanvasRenderingContext2D} ctx - Canvas context
* @param {Array} landmarks - Hand landmarks from MediaPipe
* @param {HTMLCanvasElement} canvas - Canvas element
* @param {boolean} isMobile - Whether the device is mobile
*/
export const drawLandmarks = (ctx, landmarks, canvas, isMobile) => {
// Draw connections
const connections = [
// Thumb
[0, 1], [1, 2], [2, 3], [3, 4],
// Index finger
[0, 5], [5, 6], [6, 7], [7, 8],
// Middle finger
[9, 10], [10, 11], [11, 12], [5, 9],
// Ring finger
[9, 13], [13, 14], [14, 15], [15, 16],
// Pinky
[13, 17], [17, 18], [18, 19], [19, 20],
// Palm
[0, 17]
];
// Use the requested colors
ctx.lineWidth = isMobile ? 2 : 3; // Thinner lines on mobile
ctx.strokeStyle = '#217BFE'; // Primary hand color
for (const [start, end] of connections) {
ctx.beginPath();
ctx.moveTo(landmarks[start].x * canvas.width, landmarks[start].y * canvas.height);
ctx.lineTo(landmarks[end].x * canvas.width, landmarks[end].y * canvas.height);
ctx.stroke();
}
// Draw landmarks with secondary color
ctx.fillStyle = '#AC87EB';
landmarks.forEach(landmark => {
ctx.beginPath();
ctx.arc(landmark.x * canvas.width, landmark.y * canvas.height, isMobile ? 3 : 4, 0, 2 * Math.PI);
ctx.fill();
});
// Highlight thumb and index finger
const thumbTip = landmarks[4];
const indexTip = landmarks[8];
ctx.fillStyle = '#217BFE';
[thumbTip, indexTip].forEach(tip => {
ctx.beginPath();
ctx.arc(tip.x * canvas.width, tip.y * canvas.height, isMobile ? 4 : 6, 0, 2 * Math.PI);
ctx.fill();
});
};
/**
* Check if hand is open (thumb away from index finger)
* @param {Array} landmarks - Hand landmarks from MediaPipe
* @returns {Object} - Object containing isOpen and other hand information
*/
export const analyzeHandGesture = (landmarks) => {
const thumbTip = landmarks[4]; // Thumb tip
const indexTip = landmarks[8]; // Index finger tip
const wrist = landmarks[0]; // Wrist
// Determine if it's a left or right hand
// If thumb is to the left of the wrist, it's likely a right hand
const isLeftHand = !(thumbTip.x < wrist.x);
// Calculate distance between thumb and index finger
const distance = Math.sqrt(
Math.pow(thumbTip.x - indexTip.x, 2) +
Math.pow(thumbTip.y - indexTip.y, 2) +
Math.pow(thumbTip.z - indexTip.z, 2)
);
// Hand is open when distance is greater than threshold
const isOpen = distance > 0.1;
return {
isOpen,
isLeftHand,
thumbPosition: {
x: thumbTip.x,
y: thumbTip.y
}
};
};
/**
* Create animation keyframes for hand detection animations
* @returns {string} - CSS keyframes string
*/
export const getAnimationKeyframes = () => {
return `
@keyframes thinking-blink {
0%, 100% { opacity: 1; transform: scale(1); }
50% { opacity: 0.7; transform: scale(0.95); }
}
@keyframes spring-out {
0% {
transform: scale(0) translateY(0);
opacity: 0;
}
20% {
transform: scale(0.3) translateY(-5px);
opacity: 0.7;
}
40% {
transform: scale(1.5) translateY(-30px);
}
60% {
transform: scale(0.8) translateY(15px);
}
75% {
transform: scale(1.2) translateY(-10px);
}
90% {
transform: scale(0.95) translateY(5px);
}
100% {
transform: scale(1) translateY(0);
opacity: 1;
}
}
@keyframes wiggle {
0% { transform: rotate(0deg); }
15% { transform: rotate(-15deg); }
30% { transform: rotate(12deg); }
45% { transform: rotate(-8deg); }
60% { transform: rotate(5deg); }
75% { transform: rotate(-2deg); }
100% { transform: rotate(0deg); }
}
/* Combined animation that handles both scale and rotation */
@keyframes spring-wiggle {
0% {
transform: scale(0) rotate(0deg) translateY(0);
opacity: 0;
}
15% {
transform: scale(0.2) rotate(-5deg) translateY(-5px);
opacity: 0.5;
}
30% {
transform: scale(1.5) rotate(12deg) translateY(-30px);
opacity: 1;
}
45% {
transform: scale(0.8) rotate(-8deg) translateY(15px);
}
60% {
transform: scale(1.2) rotate(5deg) translateY(-10px);
}
75% {
transform: scale(0.95) rotate(-2deg) translateY(5px);
}
90% {
transform: scale(1.05) rotate(1deg) translateY(-2px);
}
100% {
transform: scale(1) rotate(0deg) translateY(0);
}
}
/* Add new animations for particles and popping */
@keyframes float-particle {
0% {
transform: translate(0, 0) rotate(0deg);
opacity: 1;
}
100% {
transform: translate(var(--tx), var(--ty)) rotate(var(--r));
opacity: 0;
}
}
@keyframes pop-out {
0% {
transform: scale(1);
opacity: 1;
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(0);
opacity: 0;
}
}
`;
}; |