// Constants var MIN_ROTATION = 20; var MIN_SCALE = .3; var MIN_SWIPE_DISTANCE = 50; var rotation, scale, recognized, translationX, translationY; function resetGesture(event) { rotation = 0; scale = 1; recognized = false; translationX = translationY = 0; } function detectGesture(event) { // Disable built-in inertia if (event.detail == event.MSGESTURE_FLAG_INERTIA) { return; } // Update total values rotation += event.rotation * 180 / Math.PI; scale *= event.scale; translationX += event.translationX; translationY += event.translationY; if (!recognized) { var direction; if (Math.abs(translationX) > MIN_SWIPE_DISTANCE) { // Horizontal swipe recognized = true; direction = translationX < 0 ? 'left' : 'right'; console.log('Horizontal swipe: ', direction); } else if (Math.abs(translationY) > MIN_SWIPE_DISTANCE) { // Vertical swipe recognized = true; direction = translationY < 0 ? 'up' : 'down' console.log('Vertical swipe: ', direction); } if (Math.abs(rotation) >= MIN_ROTATION) { // Rotation recognized = true; console.log('Rotate: ', rotation < 0 ? 'left' : 'right'); } else if (Math.abs(scale - 1) > MIN_SCALE) { // Pinch/Stretch recognized = true; console.log('Scale: ', scale > 1 ? 'stretch' : 'pinch'); } } }