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