Skip to content

Commit

Permalink
Add new pinch gesture patch
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Reimer committed Nov 10, 2019
1 parent 0112dad commit 4d09e1d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -43,7 +43,7 @@ content:

rm content.build/web/compressed.tracemonkey-pldi-09.pdf
patch -p1 --no-backup-if-mismatch -d content.build < patches/pdfjs-origin-fix.patch
cat patches/pdfjs-pinch-gestures.js >> content.build/web/viewer.js
cat patches/pdfjs-pinch-gestures-larsneo.js >> content.build/web/viewer.js
mv content.build content

clean:
Expand Down
54 changes: 54 additions & 0 deletions patches/pdfjs-pinch-gestures-larsneo.js
@@ -0,0 +1,54 @@
// Pinch gesture workaround for Firefox for Android
// https://gist.github.com/larsneo/bb75616e9426ae589f50e8c8411020f6
// https://github.com/M-Reimer/android-pdf-js

(function AndroidPDFJSPinchGesture() {
let pinchZoomEnabled = false;
function enablePinchZoom(pdfViewer) {
let startX = 0, startY = 0;
let initialPinchDistance = 0;
let pinchScale = 1;
const viewer = document.getElementById("viewer");
const container = document.getElementById("viewerContainer");
const reset = () => { startX = startY = initialPinchDistance = 0; pinchScale = 1; };
// Prevent native iOS page zoom
//document.addEventListener("touchmove", (e) => { if (e.scale !== 1) { e.preventDefault(); } }, { passive: false });
document.addEventListener("touchstart", (e) => {
if (e.touches.length > 1) {
startX = (e.touches[0].pageX + e.touches[1].pageX) / 2;
startY = (e.touches[0].pageY + e.touches[1].pageY) / 2;
initialPinchDistance = Math.hypot((e.touches[1].pageX - e.touches[0].pageX), (e.touches[1].pageY - e.touches[0].pageY));
} else {
initialPinchDistance = 0;
}
});
document.addEventListener("touchmove", (e) => {
if (initialPinchDistance <= 0 || e.touches.length < 2) { return; }
if (e.scale !== 1) { e.preventDefault(); }
const pinchDistance = Math.hypot((e.touches[1].pageX - e.touches[0].pageX), (e.touches[1].pageY - e.touches[0].pageY));
const originX = startX + container.scrollLeft;
const originY = startY + container.scrollTop;
pinchScale = pinchDistance / initialPinchDistance;
viewer.style.transform = `scale(${pinchScale})`;
viewer.style.transformOrigin = `${originX}px ${originY}px`;
}, { passive: false });
document.addEventListener("touchend", (e) => {
if (initialPinchDistance <= 0) { return; }
viewer.style.transform = `none`;
viewer.style.transformOrigin = `unset`;
PDFViewerApplication.pdfViewer.currentScale *= pinchScale;
const rect = container.getBoundingClientRect();
const dx = startX - rect.left;
const dy = startY - rect.top;
container.scrollLeft += dx * (pinchScale - 1);
container.scrollTop += dy * (pinchScale - 1);
reset();
});
}
document.addEventListener('webviewerloaded', () => {
if (!pinchZoomEnabled) {
pinchZoomEnabled = true;
enablePinchZoom();
}
});
})();

0 comments on commit 4d09e1d

Please sign in to comment.