Skip to content

Commit

Permalink
Workaround WebKit canvas rendering bug when zooming PDFs
Browse files Browse the repository at this point in the history
WebKit has a rendering bug when rendering canvas that have their width
relative to their parent, and their parent is resized. This causes a
"tearing" effect when zooming in PDFs past a certain threshold, which
is device-dependent but tends to be between 120% and 200%.

The DOM structure around the canvas is as follows:
  div.pdfViewer [ --scale-factor: ...; ]
    > div.page [ width: round(var(--scale-factor) * ..., 1px); ]
      > div.canvasWrapper
        > canvas

Setting `width: inherit` on the div.canvasWrapper and on the canvas is
equivalent to explicitly specifying the same `width` property in pixels
as in div.page, thus making Safari properly redraw the canvas on resize.

See https://bugs.webkit.org/show_bug.cgi?id=267986 for more details
on the WebKit bug.

Fixes #16155, fixes #16329, fixes #17459
  • Loading branch information
nicolo-ribaudo committed Jan 24, 2024
1 parent ae62787 commit a31f5f4
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions web/pdf_viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@

.pdfViewer .canvasWrapper {
overflow: hidden;
width: 100%;
/* See `width: inherit` comment in `.pdfViewer .page canvas[zooming]` for the reason
* we use `inherit` and not `100%`. */
width: inherit;
height: 100%;
z-index: 1;
}
Expand Down Expand Up @@ -166,7 +168,25 @@
}

.pdfViewer .page canvas[zooming] {
width: 100%;
/* WebKit has a rendering bug when rendering canvas that have their width
* relative to their parent, and their parent is resized. This causes a
* "tearing" effect when zooming in PDFs past a certain threshold, which
* is devide-dependent but tends to be between 120% and 200%.
*
* The DOM structure around the canvas is as follows:
* div.pdfViewer [ --scale-factor: ...; ]
* > div.page [ width: round(var(--scale-factor) * ..., 1px); ]
* > div.canvasWrapper
* > canvas
*
* Setting `width: inherit` on the div.canvasWrapper and on the canvas is
* equivalent to explicitly specifying the same `width` property in pixels
* as in div.page, thus making Safari properly redraw the canvas on resize.
*
* See https://bugs.webkit.org/show_bug.cgi?id=267986 for more details
* on the WebKit bug.
*/
width: inherit;
height: 100%;
}

Expand Down

0 comments on commit a31f5f4

Please sign in to comment.