diff --git a/src/index.js b/src/index.js index 7636d32..c60676c 100644 --- a/src/index.js +++ b/src/index.js @@ -90,12 +90,10 @@ function init() { function setupCustomTouchControls() { const canvas = renderer.domElement; let touchStartDistance = 0; - let touchStartRotation = { x: 0, y: 0 }; let lastTouchMoveTime = 0; // Variables for touch tracking let touches = []; - let isRotating = false; let isZooming = false; // Helper function to get touch distance (for pinch zoom) @@ -105,39 +103,25 @@ function setupCustomTouchControls() { return Math.sqrt(dx * dx + dy * dy); } - // Helper function to get touch center point - function getTouchCenter(touch1, touch2) { - return { - x: (touch1.clientX + touch2.clientX) / 2, - y: (touch1.clientY + touch2.clientY) / 2 - }; - } - - // Touch start handler + // Touch start handler - only handle two-finger gestures, let OrbitControls handle single finger canvas.addEventListener('touchstart', function(event) { - event.preventDefault(); touches = Array.from(event.touches); if (touches.length === 2) { - // Two finger pinch/zoom start + // Two finger pinch/zoom start - prevent default for custom handling + event.preventDefault(); touchStartDistance = getTouchDistance(touches[0], touches[1]); isZooming = true; - isRotating = false; - } else if (touches.length === 1) { - // Single finger rotation start - touchStartRotation.x = touches[0].clientX; - touchStartRotation.y = touches[0].clientY; - isRotating = true; + } else { + // Single finger - let OrbitControls handle it natively isZooming = false; } lastTouchMoveTime = Date.now(); }, { passive: false }); - // Touch move handler for enhanced responsiveness + // Touch move handler - only custom handle pinch zoom canvas.addEventListener('touchmove', function(event) { - event.preventDefault(); - const currentTime = Date.now(); const timeDelta = currentTime - lastTouchMoveTime; @@ -147,6 +131,9 @@ function setupCustomTouchControls() { touches = Array.from(event.touches); if (touches.length === 2 && isZooming) { + // Prevent default only for two-finger gestures + event.preventDefault(); + // Handle pinch zoom const currentDistance = getTouchDistance(touches[0], touches[1]); const zoomDelta = (currentDistance - touchStartDistance) * 0.01; @@ -164,42 +151,20 @@ function setupCustomTouchControls() { controls.object.position.copy(controls.target).add(direction.multiplyScalar(clampedDistance)); touchStartDistance = currentDistance; - - } else if (touches.length === 1 && isRotating) { - // Enhanced single finger rotation - const rotationSensitivity = 0.005; - const deltaX = (touches[0].clientX - touchStartRotation.x) * rotationSensitivity; - const deltaY = (touches[0].clientY - touchStartRotation.y) * rotationSensitivity; - - // Apply rotation with smooth movement - controls.rotateLeft(-deltaX); - controls.rotateUp(-deltaY); - - touchStartRotation.x = touches[0].clientX; - touchStartRotation.y = touches[0].clientY; + controls.update(); } + // Single finger rotation handled natively by OrbitControls lastTouchMoveTime = currentTime; - controls.update(); }, { passive: false }); // Touch end handler canvas.addEventListener('touchend', function(event) { - event.preventDefault(); touches = Array.from(event.touches); if (touches.length < 2) { isZooming = false; } - if (touches.length < 1) { - isRotating = false; - } - - // Reset if no touches - if (touches.length === 0) { - isZooming = false; - isRotating = false; - } }, { passive: false }); // Prevent context menu on long press @@ -214,7 +179,7 @@ function setupCustomTouchControls() { canvas.style.mozUserSelect = 'none'; canvas.style.msUserSelect = 'none'; - console.log('Custom touch controls initialized'); + console.log('Custom touch controls initialized - OrbitControls handles single finger rotation'); } function setupLighting() {