Skip to content

Commit

Permalink
Avoid to compute the client rect of the viewer
Browse files Browse the repository at this point in the history
The container position and dimensions should be almost constant, hence
it's pretty useless to query them on each rescale.
Finally it avoids to trigger some reflows.
  • Loading branch information
calixteman committed Dec 16, 2022
1 parent 0c83beb commit c550953
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
7 changes: 3 additions & 4 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,6 @@ function webViewerResize() {
// Work-around issue 15324 by ignoring "resize" events during printing.
return;
}
pdfViewer.updateContainerHeightCss();

if (!pdfDocument) {
return;
Expand Down Expand Up @@ -2646,9 +2645,9 @@ function webViewerWheel(evt) {
// left corner is restored. When the mouse wheel is used, the position
// under the cursor should be restored instead.
const scaleCorrectionFactor = currentScale / previousScale - 1;
const rect = pdfViewer.container.getBoundingClientRect();
const dx = evt.clientX - rect.left;
const dy = evt.clientY - rect.top;
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;
}
Expand Down
34 changes: 28 additions & 6 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,14 @@ class PDFViewer {

#annotationMode = AnnotationMode.ENABLE_FORMS;

#containerTopLeft = null;

#enablePermissions = false;

#previousContainerHeight = 0;

#resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));

#scrollModePageState = null;

#onVisibilityChange = null;
Expand All @@ -247,6 +251,8 @@ class PDFViewer {
);
}
this.container = options.container;
this.#resizeObserver.observe(this.container);

this.viewer = options.viewer || options.container.firstElementChild;

if (
Expand Down Expand Up @@ -330,7 +336,8 @@ class PDFViewer {
if (this.removePageBorders) {
this.viewer.classList.add("removePageBorders");
}
this.updateContainerHeightCss();

this.#updateContainerHeightCss();
}

get pagesCount() {
Expand Down Expand Up @@ -1137,7 +1144,6 @@ class PDFViewer {
if (this.defaultRenderingQueue) {
this.update();
}
this.updateContainerHeightCss();
}

/**
Expand Down Expand Up @@ -2186,16 +2192,32 @@ class PDFViewer {
this.currentScaleValue = newScale;
}

updateContainerHeightCss() {
const height = this.container.clientHeight;

#updateContainerHeightCss(height = this.container.clientHeight) {
if (height !== this.#previousContainerHeight) {
this.#previousContainerHeight = height;

docStyle.setProperty("--viewer-container-height", `${height}px`);
}
}

#resizeObserverCallback(entries) {
for (const entry of entries) {
if (entry.target === this.container) {
this.#updateContainerHeightCss(
Math.floor(entry.borderBoxSize[0].blockSize)
);
this.#containerTopLeft = null;
break;
}
}
}

get containerTopLeft() {
return (this.#containerTopLeft ||= [
this.container.offsetTop,
this.container.offsetLeft,
]);
}

/**
* @type {number}
*/
Expand Down

0 comments on commit c550953

Please sign in to comment.