Skip to content

Commit

Permalink
Merge pull request #10 from HenestrosaDev/dev
Browse files Browse the repository at this point in the history
Add `@touchstart`, `@touchmove` and `@touchend` to `SimplePdfReader`
  • Loading branch information
HenestrosaDev authored Jun 14, 2024
2 parents 5482205 + f48b967 commit cb38134
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions resources/js/Components/Common/SimplePdfReader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const isPageInputFocus = ref(false);
const isZoomInputFocus = ref(false);
const zoomValue = ref(props.defaultZoom);
let initialPinchDistance = null;
let lastPinchScale = 1;
onMounted(() => {
configurePdfjsWorker();
loadPdf();
Expand Down Expand Up @@ -184,6 +187,43 @@ const onWheel = (event) => {
$zoomInput.value.value = newZoom;
}
};
// Pinch-to-Zoom Handlers
const onTouchStart = (event) => {
if (event.touches.length === 2) {
initialPinchDistance = getDistance(event.touches[0], event.touches[1]);
lastPinchScale = currentPdf.zoom;
}
};
const onTouchMove = (event) => {
if (event.touches.length === 2 && initialPinchDistance) {
const newDistance = getDistance(event.touches[0], event.touches[1]);
const scaleFactor = newDistance / initialPinchDistance;
const newZoom = Math.round(lastPinchScale * scaleFactor * 100);
const minZoom = parseInt($zoomInput.value.min);
const maxZoom = parseInt($zoomInput.value.max);
if (newZoom >= minZoom && newZoom <= maxZoom) {
changeZoom(newZoom);
$zoomInput.value.value = newZoom;
}
}
};
const onTouchEnd = (event) => {
if (event.touches.length < 2) {
initialPinchDistance = null;
}
};
const getDistance = (touch1, touch2) => {
const dx = touch2.clientX - touch1.clientX;
const dy = touch2.clientY - touch1.clientY;
return Math.sqrt(dx * dy + dy * dy);
};
</script>

<template>
Expand All @@ -200,6 +240,9 @@ const onWheel = (event) => {
ref="$textLayerDiv"
class="text-layer"
@wheel="shouldZoomOnWheel && onWheel($event)"
@touchstart="onTouchStart"
@touchmove="onTouchMove"
@touchend="onTouchEnd"
/>
</div>
</main>
Expand Down

0 comments on commit cb38134

Please sign in to comment.