Fix zoom position bug by using viewport-relative coordinates for containerTopLeft#20632
Fix zoom position bug by using viewport-relative coordinates for containerTopLeft#20632pranitaurlam wants to merge 1 commit intomozilla:masterfrom
Conversation
|
This approach seems more correct in general. I do not understand however how the bug reported in the issue can be affected by this, since in the built-in firefox viewer expect |
|
Hi @Snuffleupagus, @timvandermeij, and @Calixte! I've implemented a fix for the zoom anchoring issue reported in #20630. Through investigation, I found that I've updated I'd appreciate it if you could take a look! Thanks for all your hard work on this project." |
|
Hi @nicolo-ribaudo , You're right that in a perfectly controlled environment like the built-in viewer, offsetTop often matches the viewport top. However, the zoom origin provided to The bug reported (especially involving the By using getBoundingClientRect(), we ensure that This fix aligns with how other coordinate-sensitive components in PDF.js (like the text layer or annotation editors) handle positioning. |
ssue Zooming in the PDF.js viewer (particularly in Firefox when the PDF fills the display width or when custom CSS filters like invert are applied) causes the view to jump to the top-left corner instead of anchoring to the cursor's location.
Root Cause The issue stems from a coordinate system mismatch in PDFViewer.#setScaleUpdatePages. It calculates the new scroll position by subtracting this.containerTopLeft from a viewport-relative zoom origin (derived from clientX/clientY).
Previously,
containerTopLeft
used offsetTop and offsetLeft, which are relative to the offsetParent. In complex layouts or when certain CSS properties are applied, the offsetParent relation can become inconsistent with viewport coordinates, leading to incorrect offsets and the "top-left jump" behavior.
Solution Updated the
containerTopLeft
getter in
web/pdf_viewer.js
to use this.container.getBoundingClientRect().
getBoundingClientRect() returns coordinates relative to the viewport.
This ensures that
containerTopLeft
and the zoom origin are in the same coordinate system, resulting in accurate scroll anchoring regardless of CSS filters or DOM hierarchy.
Verification
Verified the coordinate transformation logic: origin (viewport) -
containerTopLeft
(viewport) correctly yields the interior coordinate relative to the viewer container.
Code analysis confirms this robustly handles the scenario described in issue #20630.