Skip to content

Improve PDF.js zoom functionality; add configurability#1518

Merged
demiankatz merged 5 commits into
UniversalViewer:devfrom
FalveyLibraryTechnology:pdf-js-zoom
Aug 15, 2025
Merged

Improve PDF.js zoom functionality; add configurability#1518
demiankatz merged 5 commits into
UniversalViewer:devfrom
FalveyLibraryTechnology:pdf-js-zoom

Conversation

@demiankatz

@demiankatz demiankatz commented Jul 30, 2025

Copy link
Copy Markdown
Contributor

There were several problems with PDF.js zoom functionality:

  • Logic for applying zoom was duplicated in two places
  • There was no way to configure zoom limits
  • Scaling worked with a simple addition/subtraction mechanism which made it impossible to zoom out far enough on very large documents

This PR fixes the issues:

  • It eliminates redundant code and uses events more consistently
  • It adds new config settings for minimum and maximum scale
  • It retains the legacy "add or subtract 0.5" scaling functionality, but it adds a "multiply or divide by 1.5" behavior to allow more granular behavior at the extremes

I'm sure there is room for further improvement, especially in terms of the algorithm for zooming, but at least this code centralizes everything into support methods that can be more easily customized and changed in future.

Here is a good manifest for testing purposes:

https://digital.library.villanova.edu/Item/vudl:189057/Manifest

This contains a very large PDF -- you can't zoom out far enough to see the whole thing on screen at once with the default settings, but if you adjust the new config parameters, you can view it more comfortably.

@vercel

vercel Bot commented Jul 30, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
universalviewer Ready Preview Comment Aug 14, 2025 4:02pm

@Geoffsc

Geoffsc commented Jul 31, 2025

Copy link
Copy Markdown
Contributor

@demiankatz from a quick read this looks good overall and I like that it's configurable now. From testing zooming in and out seems to work fine, as I'm not sure exactly what the calculations in PDFCenterPanel are doing.

@demiankatz

Copy link
Copy Markdown
Contributor Author

@demiankatz from a quick read this looks good overall and I like that it's configurable now. From testing zooming in and out seems to work fine, as I'm not sure exactly what the calculations in PDFCenterPanel are doing.

There's probably a much more mathematically intelligent way to handle the zooming than what we're currently doing, but geometry is not my strength, so I basically just extended the existing algorithm in a way that retains existing behavior while extending past its existing limitations. Previously, the code just added or subtracted 0.5 to the zoom factor, which meant that you couldn't zoom at a finer grain once you hit 0.5. I extended that by multiplying by 1.5 once we're in those smaller numbers, which provides a theoretically less limited zoom capability. It's all still based on arbitrary magic numbers, though! I'd love to do better, but I'm probably not the best person to develop a replacement algorithm. Hopefully I've rearranged the code so that it's easier to drop one in now, though. :-)

@sturkel89

Copy link
Copy Markdown

I tested this PR via the Vercel viewer. Big improvement! The test branch allows for a much better zooming experience.

In dev, the image is too large to fit on the screen unless you're in full screen mode. In the test branch, the zoom options span the full range from the whole image being teeny tiny to a comfortable viewing size to gigantic, all at either normal browser widths or at full screen. It's useful for the reader to have this range of options.

The only thing I'd ask for is a button next to the + and - to reset to the default (or to a size that fills the screen).

Thanks for your work on this! It will make the UV much more friendly for users of PDFs.

Comment thread manual/CONFIG.md Outdated

@Geoffsc Geoffsc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good by me!

@demiankatz

Copy link
Copy Markdown
Contributor Author

Thanks, @sturkel89 and @Geoffsc.

I agree that a reset button would be helpful, but I think that goes along with revising the zooming algorithm to have a better starting point and more logical progression, which is beyond what I currently have time to do. I'll merge this as-is since I think it's a significant improvement, and we can do further work in a follow-up as time permits and need dictates.

@demiankatz demiankatz merged commit 3e34329 into UniversalViewer:dev Aug 15, 2025
4 checks passed
@demiankatz demiankatz deleted the pdf-js-zoom branch August 15, 2025 12:40
@github-project-automation github-project-automation Bot moved this from In testing to Completed in Universal Viewer Community Board Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Completed

Development

Successfully merging this pull request may close these issues.

3 participants