Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for start offset on panning, to prevent jumps when panning star… #16

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default function Zoom(props: PropsWithChildren<ZoomProps>): 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<number | undefined> = useRef()

Expand Down Expand Up @@ -233,6 +235,10 @@ export default function Zoom(props: PropsWithChildren<ZoomProps>): React.ReactNo
})

const panGesture = Gesture.Pan()
.onStart((event: GestureUpdateEvent<PanGestureHandlerEventPayload>): void => {
panStartOffsetX.value = event.translationX
panStartOffsetY.value = event.translationY
})
.onUpdate((event: GestureUpdateEvent<PanGestureHandlerEventPayload>): void => {
let { translationX, translationY } = event

Expand All @@ -243,6 +249,8 @@ export default function Zoom(props: PropsWithChildren<ZoomProps>): 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
Expand All @@ -254,6 +262,8 @@ export default function Zoom(props: PropsWithChildren<ZoomProps>): 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
Expand Down