askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
gesture-recognition.js · 13.0 KB · 437 lines
| 1 | /** |
| 2 | * Gesture Recognition System |
| 3 | * Recognizes touch gestures and maps them to semantic intents |
| 4 | * Supports: swipe, pinch, rotate, long-press, double-tap, multi-finger |
| 5 | */ |
| 6 | |
| 7 | class GestureRecognizer { |
| 8 | constructor() { |
| 9 | this.gestures = new Map(); |
| 10 | this.activeTouch = null; |
| 11 | this.touchStart = null; |
| 12 | this.touchHistory = []; |
| 13 | this.config = { |
| 14 | swipeThreshold: 50, // pixels |
| 15 | swipeVelocityThreshold: 0.3, // pixels/ms |
| 16 | longPressDelay: 500, // ms |
| 17 | doubleTapDelay: 300, // ms |
| 18 | pinchThreshold: 20, // pixels |
| 19 | rotateThreshold: 15 // degrees |
| 20 | }; |
| 21 | |
| 22 | this.lastTap = null; |
| 23 | this.longPressTimer = null; |
| 24 | |
| 25 | this.init(); |
| 26 | } |
| 27 | |
| 28 | init() { |
| 29 | this.registerDefaultGestures(); |
| 30 | this.attachGlobalListeners(); |
| 31 | console.log('Gesture Recognition initialized'); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Register default gesture patterns |
| 36 | */ |
| 37 | registerDefaultGestures() { |
| 38 | // Swipe gestures |
| 39 | this.registerGesture({ |
| 40 | name: 'swipe-left', |
| 41 | pattern: (data) => data.dx < -this.config.swipeThreshold && Math.abs(data.dy) < 50, |
| 42 | intent: 'navigate-back', |
| 43 | description: 'Swipe left to go back' |
| 44 | }); |
| 45 | |
| 46 | this.registerGesture({ |
| 47 | name: 'swipe-right', |
| 48 | pattern: (data) => data.dx > this.config.swipeThreshold && Math.abs(data.dy) < 50, |
| 49 | intent: 'navigate-forward', |
| 50 | description: 'Swipe right to go forward' |
| 51 | }); |
| 52 | |
| 53 | this.registerGesture({ |
| 54 | name: 'swipe-up', |
| 55 | pattern: (data) => data.dy < -this.config.swipeThreshold && Math.abs(data.dx) < 50, |
| 56 | intent: 'scroll-down', |
| 57 | description: 'Swipe up to scroll down' |
| 58 | }); |
| 59 | |
| 60 | this.registerGesture({ |
| 61 | name: 'swipe-down', |
| 62 | pattern: (data) => data.dy > this.config.swipeThreshold && Math.abs(data.dx) < 50, |
| 63 | intent: 'scroll-up', |
| 64 | description: 'Swipe down to scroll up or refresh' |
| 65 | }); |
| 66 | |
| 67 | // Pinch gestures |
| 68 | this.registerGesture({ |
| 69 | name: 'pinch-in', |
| 70 | pattern: (data) => data.fingers === 2 && data.scale < 0.8, |
| 71 | intent: 'zoom-out', |
| 72 | description: 'Pinch in to zoom out' |
| 73 | }); |
| 74 | |
| 75 | this.registerGesture({ |
| 76 | name: 'pinch-out', |
| 77 | pattern: (data) => data.fingers === 2 && data.scale > 1.2, |
| 78 | intent: 'zoom-in', |
| 79 | description: 'Pinch out to zoom in' |
| 80 | }); |
| 81 | |
| 82 | // Rotate gesture |
| 83 | this.registerGesture({ |
| 84 | name: 'rotate', |
| 85 | pattern: (data) => data.fingers === 2 && Math.abs(data.rotation) > this.config.rotateThreshold, |
| 86 | intent: 'rotate', |
| 87 | description: 'Two-finger rotate' |
| 88 | }); |
| 89 | |
| 90 | // Multi-finger gestures |
| 91 | this.registerGesture({ |
| 92 | name: 'three-finger-swipe-up', |
| 93 | pattern: (data) => data.fingers === 3 && data.dy < -this.config.swipeThreshold, |
| 94 | intent: 'show-overview', |
| 95 | description: 'Three-finger swipe up for overview' |
| 96 | }); |
| 97 | |
| 98 | this.registerGesture({ |
| 99 | name: 'three-finger-swipe-down', |
| 100 | pattern: (data) => data.fingers === 3 && data.dy > this.config.swipeThreshold, |
| 101 | intent: 'minimize', |
| 102 | description: 'Three-finger swipe down to minimize' |
| 103 | }); |
| 104 | |
| 105 | // Edge swipes |
| 106 | this.registerGesture({ |
| 107 | name: 'edge-swipe-right', |
| 108 | pattern: (data) => data.startX < 20 && data.dx > this.config.swipeThreshold, |
| 109 | intent: 'open-menu', |
| 110 | description: 'Swipe from left edge to open menu' |
| 111 | }); |
| 112 | |
| 113 | this.registerGesture({ |
| 114 | name: 'edge-swipe-left', |
| 115 | pattern: (data) => data.startX > window.innerWidth - 20 && data.dx < -this.config.swipeThreshold, |
| 116 | intent: 'close-panel', |
| 117 | description: 'Swipe from right edge to close panel' |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Register a custom gesture |
| 123 | */ |
| 124 | registerGesture(gesture) { |
| 125 | this.gestures.set(gesture.name, gesture); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Attach global touch listeners |
| 130 | */ |
| 131 | attachGlobalListeners() { |
| 132 | document.addEventListener('touchstart', this.handleTouchStart.bind(this), { passive: false }); |
| 133 | document.addEventListener('touchmove', this.handleTouchMove.bind(this), { passive: false }); |
| 134 | document.addEventListener('touchend', this.handleTouchEnd.bind(this), { passive: false }); |
| 135 | document.addEventListener('touchcancel', this.handleTouchCancel.bind(this), { passive: false }); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Handle touch start |
| 140 | */ |
| 141 | handleTouchStart(e) { |
| 142 | const touch = e.touches[0]; |
| 143 | const now = Date.now(); |
| 144 | |
| 145 | this.touchStart = { |
| 146 | x: touch.clientX, |
| 147 | y: touch.clientY, |
| 148 | time: now, |
| 149 | fingers: e.touches.length, |
| 150 | target: e.target |
| 151 | }; |
| 152 | |
| 153 | this.touchHistory = [{ |
| 154 | x: touch.clientX, |
| 155 | y: touch.clientY, |
| 156 | time: now |
| 157 | }]; |
| 158 | |
| 159 | // Check for double-tap |
| 160 | if (this.lastTap && now - this.lastTap.time < this.config.doubleTapDelay) { |
| 161 | const distance = Math.hypot( |
| 162 | touch.clientX - this.lastTap.x, |
| 163 | touch.clientY - this.lastTap.y |
| 164 | ); |
| 165 | |
| 166 | if (distance < 20) { |
| 167 | this.emitGestureIntent('double-tap', { |
| 168 | x: touch.clientX, |
| 169 | y: touch.clientY, |
| 170 | target: e.target |
| 171 | }); |
| 172 | this.lastTap = null; |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | this.lastTap = { x: touch.clientX, y: touch.clientY, time: now }; |
| 178 | |
| 179 | // Start long-press timer |
| 180 | this.longPressTimer = setTimeout(() => { |
| 181 | this.emitGestureIntent('long-press', { |
| 182 | x: touch.clientX, |
| 183 | y: touch.clientY, |
| 184 | target: e.target |
| 185 | }); |
| 186 | }, this.config.longPressDelay); |
| 187 | |
| 188 | // Handle multi-touch |
| 189 | if (e.touches.length === 2) { |
| 190 | this.touchStart.initialDistance = this.getTouchDistance(e.touches); |
| 191 | this.touchStart.initialAngle = this.getTouchAngle(e.touches); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Handle touch move |
| 197 | */ |
| 198 | handleTouchMove(e) { |
| 199 | if (!this.touchStart) return; |
| 200 | |
| 201 | clearTimeout(this.longPressTimer); |
| 202 | |
| 203 | const touch = e.touches[0]; |
| 204 | const now = Date.now(); |
| 205 | |
| 206 | this.touchHistory.push({ |
| 207 | x: touch.clientX, |
| 208 | y: touch.clientY, |
| 209 | time: now |
| 210 | }); |
| 211 | |
| 212 | // Keep history limited |
| 213 | if (this.touchHistory.length > 10) { |
| 214 | this.touchHistory.shift(); |
| 215 | } |
| 216 | |
| 217 | // Handle multi-touch gestures |
| 218 | if (e.touches.length === 2 && this.touchStart.initialDistance) { |
| 219 | const currentDistance = this.getTouchDistance(e.touches); |
| 220 | const currentAngle = this.getTouchAngle(e.touches); |
| 221 | |
| 222 | const scale = currentDistance / this.touchStart.initialDistance; |
| 223 | const rotation = currentAngle - this.touchStart.initialAngle; |
| 224 | |
| 225 | // Check for pinch |
| 226 | if (scale < 0.8 || scale > 1.2) { |
| 227 | this.emitGestureIntent(scale < 1 ? 'pinch-in' : 'pinch-out', { |
| 228 | scale, |
| 229 | target: e.target |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | // Check for rotate |
| 234 | if (Math.abs(rotation) > this.config.rotateThreshold) { |
| 235 | this.emitGestureIntent('rotate', { |
| 236 | rotation, |
| 237 | target: e.target |
| 238 | }); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Handle touch end |
| 245 | */ |
| 246 | handleTouchEnd(e) { |
| 247 | if (!this.touchStart) return; |
| 248 | |
| 249 | clearTimeout(this.longPressTimer); |
| 250 | |
| 251 | const touch = e.changedTouches[0]; |
| 252 | const dx = touch.clientX - this.touchStart.x; |
| 253 | const dy = touch.clientY - this.touchStart.y; |
| 254 | const dt = Date.now() - this.touchStart.time; |
| 255 | |
| 256 | const distance = Math.hypot(dx, dy); |
| 257 | const velocity = distance / dt; |
| 258 | |
| 259 | // Build gesture data |
| 260 | const gestureData = { |
| 261 | dx, |
| 262 | dy, |
| 263 | distance, |
| 264 | velocity, |
| 265 | duration: dt, |
| 266 | startX: this.touchStart.x, |
| 267 | startY: this.touchStart.y, |
| 268 | endX: touch.clientX, |
| 269 | endY: touch.clientY, |
| 270 | fingers: this.touchStart.fingers, |
| 271 | target: this.touchStart.target |
| 272 | }; |
| 273 | |
| 274 | // If multi-touch, add scale/rotation |
| 275 | if (this.touchStart.initialDistance) { |
| 276 | gestureData.scale = this.getTouchDistance([touch]) / this.touchStart.initialDistance; |
| 277 | gestureData.rotation = this.getTouchAngle([touch]) - this.touchStart.initialAngle; |
| 278 | } |
| 279 | |
| 280 | // Match gesture patterns |
| 281 | for (const [name, gesture] of this.gestures.entries()) { |
| 282 | if (gesture.pattern(gestureData)) { |
| 283 | this.emitGestureIntent(name, gestureData); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Check for tap (short duration, small distance) |
| 289 | if (dt < 200 && distance < 10) { |
| 290 | this.emitGestureIntent('tap', { |
| 291 | x: touch.clientX, |
| 292 | y: touch.clientY, |
| 293 | target: this.touchStart.target |
| 294 | }); |
| 295 | } |
| 296 | |
| 297 | this.touchStart = null; |
| 298 | this.touchHistory = []; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Handle touch cancel |
| 303 | */ |
| 304 | handleTouchCancel(e) { |
| 305 | clearTimeout(this.longPressTimer); |
| 306 | this.touchStart = null; |
| 307 | this.touchHistory = []; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Emit gesture intent event |
| 312 | */ |
| 313 | emitGestureIntent(gestureName, data) { |
| 314 | const gesture = this.gestures.get(gestureName); |
| 315 | |
| 316 | const event = new CustomEvent('gesture', { |
| 317 | bubbles: true, |
| 318 | composed: true, |
| 319 | detail: { |
| 320 | gesture: gestureName, |
| 321 | intent: gesture?.intent || gestureName, |
| 322 | description: gesture?.description || `Gesture: ${gestureName}`, |
| 323 | ...data |
| 324 | } |
| 325 | }); |
| 326 | |
| 327 | (data.target || document).dispatchEvent(event); |
| 328 | |
| 329 | // Also emit as semantic intent |
| 330 | if (gesture) { |
| 331 | const intentEvent = new CustomEvent('intent', { |
| 332 | bubbles: true, |
| 333 | composed: true, |
| 334 | detail: { |
| 335 | type: gesture.intent, |
| 336 | gesture: gestureName, |
| 337 | ...data |
| 338 | } |
| 339 | }); |
| 340 | |
| 341 | (data.target || document).dispatchEvent(intentEvent); |
| 342 | } |
| 343 | |
| 344 | console.log(`Gesture: ${gestureName}`, data); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get distance between two touches |
| 349 | */ |
| 350 | getTouchDistance(touches) { |
| 351 | const dx = touches[0].clientX - touches[1].clientX; |
| 352 | const dy = touches[0].clientY - touches[1].clientY; |
| 353 | return Math.hypot(dx, dy); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Get angle between two touches (in degrees) |
| 358 | */ |
| 359 | getTouchAngle(touches) { |
| 360 | const dx = touches[1].clientX - touches[0].clientX; |
| 361 | const dy = touches[1].clientY - touches[0].clientY; |
| 362 | return Math.atan2(dy, dx) * 180 / Math.PI; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get velocity from touch history |
| 367 | */ |
| 368 | getVelocity() { |
| 369 | if (this.touchHistory.length < 2) return { vx: 0, vy: 0 }; |
| 370 | |
| 371 | const recent = this.touchHistory.slice(-2); |
| 372 | const dx = recent[1].x - recent[0].x; |
| 373 | const dy = recent[1].y - recent[0].y; |
| 374 | const dt = recent[1].time - recent[0].time; |
| 375 | |
| 376 | return { |
| 377 | vx: dx / dt, |
| 378 | vy: dy / dt |
| 379 | }; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Visual feedback for gestures (debug mode) |
| 384 | */ |
| 385 | showGestureFeedback(gesture, x, y) { |
| 386 | const feedback = document.createElement('div'); |
| 387 | feedback.style.cssText = ` |
| 388 | position: fixed; |
| 389 | left: ${x}px; |
| 390 | top: ${y}px; |
| 391 | padding: 8px 16px; |
| 392 | background: rgba(0, 122, 255, 0.9); |
| 393 | color: white; |
| 394 | border-radius: 8px; |
| 395 | font-size: 14px; |
| 396 | font-weight: 600; |
| 397 | z-index: 999999; |
| 398 | pointer-events: none; |
| 399 | animation: fadeOut 1s forwards; |
| 400 | `; |
| 401 | feedback.textContent = gesture; |
| 402 | |
| 403 | document.body.appendChild(feedback); |
| 404 | |
| 405 | setTimeout(() => feedback.remove(), 1000); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Initialize gesture recognition |
| 410 | if (typeof window !== 'undefined') { |
| 411 | window.addEventListener('DOMContentLoaded', () => { |
| 412 | window.gestureRecognizer = new GestureRecognizer(); |
| 413 | |
| 414 | // Listen to gestures globally |
| 415 | document.addEventListener('gesture', (e) => { |
| 416 | console.log('Gesture detected:', e.detail); |
| 417 | }); |
| 418 | |
| 419 | // Add CSS for feedback animation |
| 420 | const style = document.createElement('style'); |
| 421 | style.textContent = ` |
| 422 | @keyframes fadeOut { |
| 423 | from { opacity: 1; transform: scale(1); } |
| 424 | to { opacity: 0; transform: scale(1.5); } |
| 425 | } |
| 426 | `; |
| 427 | document.head.appendChild(style); |
| 428 | |
| 429 | console.log('Gesture Recognition ready'); |
| 430 | console.log('Available gestures:', Array.from(window.gestureRecognizer.gestures.keys())); |
| 431 | }); |
| 432 | } |
| 433 | |
| 434 | // Export |
| 435 | if (typeof module !== 'undefined' && module.exports) { |
| 436 | module.exports = GestureRecognizer; |
| 437 | } |