Merge pull request #15935 from Snuffleupagus/_centerAtPos

Add a `PDFViewerApplication` helper method to center the position after wheel/pinch-zooming
This commit is contained in:
Jonas Jenwald 2023-01-19 14:25:10 +01:00 committed by GitHub
commit c629a85a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2077,6 +2077,16 @@ const PDFViewerApplication = {
return newFactor; return newFactor;
}, },
_centerAtPos(previousScale, x, y) {
const { pdfViewer } = this;
const scaleDiff = pdfViewer.currentScale / previousScale - 1;
if (scaleDiff !== 0) {
const [top, left] = pdfViewer.containerTopLeft;
pdfViewer.container.scrollLeft += (x - left) * scaleDiff;
pdfViewer.container.scrollTop += (y - top) * scaleDiff;
}
},
/** /**
* Should be called *after* all pages have loaded, or if an error occurred, * Should be called *after* all pages have loaded, or if an error occurred,
* to unblock the "load" event; see https://bugzilla.mozilla.org/show_bug.cgi?id=1618553 * to unblock the "load" event; see https://bugzilla.mozilla.org/show_bug.cgi?id=1618553
@ -2689,6 +2699,8 @@ function webViewerWheel(evt) {
PDFViewerApplication.zoomOut(null, scaleFactor); PDFViewerApplication.zoomOut(null, scaleFactor);
} else if (scaleFactor > 1) { } else if (scaleFactor > 1) {
PDFViewerApplication.zoomIn(null, scaleFactor); PDFViewerApplication.zoomIn(null, scaleFactor);
} else {
return;
} }
} else { } else {
// It is important that we query deltaMode before delta{X,Y}, so that // It is important that we query deltaMode before delta{X,Y}, so that
@ -2729,21 +2741,15 @@ function webViewerWheel(evt) {
PDFViewerApplication.zoomOut(-ticks); PDFViewerApplication.zoomOut(-ticks);
} else if (ticks > 0) { } else if (ticks > 0) {
PDFViewerApplication.zoomIn(ticks); PDFViewerApplication.zoomIn(ticks);
} else {
return;
} }
} }
const currentScale = pdfViewer.currentScale; // After scaling the page via zoomIn/zoomOut, the position of the upper-
if (previousScale !== currentScale) { // left corner is restored. When the mouse wheel is used, the position
// After scaling the page via zoomIn/zoomOut, the position of the upper- // under the cursor should be restored instead.
// left corner is restored. When the mouse wheel is used, the position PDFViewerApplication._centerAtPos(previousScale, evt.clientX, evt.clientY);
// under the cursor should be restored instead.
const scaleCorrectionFactor = currentScale / previousScale - 1;
const [top, left] = pdfViewer.containerTopLeft;
const dx = evt.clientX - left;
const dy = evt.clientY - top;
pdfViewer.container.scrollLeft += dx * scaleCorrectionFactor;
pdfViewer.container.scrollTop += dy * scaleCorrectionFactor;
}
} else { } else {
setZoomDisabledTimeout(); setZoomDisabledTimeout();
} }
@ -2858,6 +2864,8 @@ function webViewerTouchMove(evt) {
PDFViewerApplication.zoomOut(null, newScaleFactor); PDFViewerApplication.zoomOut(null, newScaleFactor);
} else if (newScaleFactor > 1) { } else if (newScaleFactor > 1) {
PDFViewerApplication.zoomIn(null, newScaleFactor); PDFViewerApplication.zoomIn(null, newScaleFactor);
} else {
return;
} }
} else { } else {
const PIXELS_PER_LINE_SCALE = 30; const PIXELS_PER_LINE_SCALE = 30;
@ -2869,21 +2877,16 @@ function webViewerTouchMove(evt) {
PDFViewerApplication.zoomOut(-ticks); PDFViewerApplication.zoomOut(-ticks);
} else if (ticks > 0) { } else if (ticks > 0) {
PDFViewerApplication.zoomIn(ticks); PDFViewerApplication.zoomIn(ticks);
} else {
return;
} }
} }
const currentScale = pdfViewer.currentScale; PDFViewerApplication._centerAtPos(
if (previousScale !== currentScale) { previousScale,
const scaleCorrectionFactor = currentScale / previousScale - 1; (page0X + page1X) / 2,
const newCenterX = (page0X + page1X) / 2; (page0Y + page1Y) / 2
const newCenterY = (page0Y + page1Y) / 2; );
const [top, left] = pdfViewer.containerTopLeft;
const dx = newCenterX - left;
const dy = newCenterY - top;
pdfViewer.container.scrollLeft += dx * scaleCorrectionFactor;
pdfViewer.container.scrollTop += dy * scaleCorrectionFactor;
}
} }
function webViewerTouchEnd(evt) { function webViewerTouchEnd(evt) {