From 19563b696b0da752c41ba5fa0f2ee8828151df7c Mon Sep 17 00:00:00 2001 From: Alvaro Date: Thu, 7 Nov 2024 13:13:29 +0100 Subject: [PATCH] Fix mobile drag on globe --- src/components/energy/section-2/index.tsx | 6 +- src/components/globe-map/index.tsx | 65 +++++++++++++++---- .../image-slider-with-text/index.tsx | 19 ------ 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/components/energy/section-2/index.tsx b/src/components/energy/section-2/index.tsx index baad0b7..b607e0c 100644 --- a/src/components/energy/section-2/index.tsx +++ b/src/components/energy/section-2/index.tsx @@ -25,8 +25,8 @@ export default function Section2() { return (
-
- +
+
- +
); diff --git a/src/components/globe-map/index.tsx b/src/components/globe-map/index.tsx index 8adb283..93f0b04 100644 --- a/src/components/globe-map/index.tsx +++ b/src/components/globe-map/index.tsx @@ -50,27 +50,58 @@ export default function GlobeMap({ videoMaterial, className, style, hasMarkers = } }, [hasMarkers]); - // Handle vertical swipe and wheel events to scroll the page + // Handle vertical wheel events to scroll the page const bind = useGesture( { - onDrag: (props) => { - const { event, direction, delta } = props; - if (direction[1] !== 0) { // Vertical swipe - event.stopPropagation(); - event.preventDefault(); - window.scrollBy(0, -delta[1]); - } - }, onWheel: (props) => { const { event, direction, delta } = props; if (direction[1] !== 0) { event.stopPropagation(); + event.preventDefault(); window.scrollBy(0, delta[1]); } } } ) - const { onWheel, onDrag } = bind(); + const { onWheel } = bind(); + + useEffect(() => { + let touchPosition: number | null = null; + const handleTouchStart = (event: TouchEvent) => { + const updatedTouchPosition = event.touches[0].clientY; + touchPosition = updatedTouchPosition; + + if (event.cancelable) { + event.preventDefault(); + } + }; + + const handleTouchEnd = (event: TouchEvent) => { + const touchPositionEnd = event.changedTouches[0].clientY; + const delta = touchPosition ? touchPositionEnd - touchPosition : 0; + if (delta) { // Vertical swipe + event.stopPropagation(); + event.preventDefault(); + window.scrollBy({ top: -delta * 2, behavior: 'smooth' }); + } + if (event.cancelable) { + event.preventDefault(); + } + }; + + const containerElement = canvasRef.current; + if (containerElement) { + containerElement.addEventListener('touchstart', handleTouchStart, { passive: false }); + containerElement.addEventListener('touchend', handleTouchEnd, { passive: false }); + } + + return () => { + if (containerElement) { + containerElement.removeEventListener('touchstart', handleTouchStart); + containerElement.removeEventListener('touchstart', handleTouchEnd); + } + }; + }, []); // const dcontrols = new DeviceOrientationControls(camera, renderer.domElement); @@ -83,11 +114,10 @@ export default function GlobeMap({ videoMaterial, className, style, hasMarkers = const { ErrorBoundary, didCatch, error } = useErrorBoundary(); useEffect(() => { if (didCatch) { - console.log(error) + console.error('Globe error', error) } } , [didCatch, error]); - return ( <>
Sorry, no WebGL supported in your browser
} onWheel={onWheel} - onDrag={onDrag} > - + { - const handleTouchMove = (event: TouchEvent) => { - if (event.cancelable) { - event.preventDefault(); - } - }; - - const containerElement = containerRef.current; - if (containerElement) { - containerElement.addEventListener('touchmove', handleTouchMove, { passive: false }); - } - - return () => { - if (containerElement) { - containerElement.removeEventListener('touchmove', handleTouchMove); - } - }; - }, []); - return (