From 300bce3e181f9193d31f23f3cf4efb189afa320e Mon Sep 17 00:00:00 2001 From: fede Date: Thu, 6 Jul 2023 17:19:10 -0300 Subject: [PATCH] Allow for start offset on panning, to prevent jumps when panning starts on Android 12 --- index.tsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.tsx b/index.tsx index b356d63..c59e454 100644 --- a/index.tsx +++ b/index.tsx @@ -44,6 +44,8 @@ export default function Zoom(props: PropsWithChildren): React.ReactNo const translateY = useSharedValue(0) const lastOffsetX = useSharedValue(0) const lastOffsetY = useSharedValue(0) + const panStartOffsetX = useSharedValue(0) + const panStartOffsetY = useSharedValue(0) const handlePanOutsideTimeoutId: React.MutableRefObject = useRef() @@ -233,6 +235,10 @@ export default function Zoom(props: PropsWithChildren): React.ReactNo }) const panGesture = Gesture.Pan() + .onStart((event: GestureUpdateEvent): void => { + panStartOffsetX.value = event.translationX + panStartOffsetY.value = event.translationY + }) .onUpdate((event: GestureUpdateEvent): void => { let { translationX, translationY } = event @@ -243,6 +249,8 @@ export default function Zoom(props: PropsWithChildren): React.ReactNo translationX -= panOffsetsBeforeGestureStart.value.x translationY -= panOffsetsBeforeGestureStart.value.y + translationX -= panStartOffsetX.value + translationY -= panStartOffsetY.value translateX.value = lastOffsetX.value + translationX / lastScale.value translateY.value = lastOffsetY.value + translationY / lastScale.value @@ -254,6 +262,8 @@ export default function Zoom(props: PropsWithChildren): React.ReactNo translationX -= panOffsetsBeforeGestureStart.value.x translationY -= panOffsetsBeforeGestureStart.value.y } + translationX -= panStartOffsetX.value + translationY -= panStartOffsetY.value // SAVES LAST POSITION lastOffsetX.value = lastOffsetX.value + translationX / lastScale.value