Skip to content

Commit

Permalink
Avoid to have a white line around the canvas
Browse files Browse the repository at this point in the history
The canvas must have the same dims as the page in order to avoid to see the page
background.
  • Loading branch information
calixteman committed Sep 6, 2024
1 parent 4b906ad commit d641dc5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,12 @@ function setLayerDimensions(

const w = `var(--scale-factor) * ${pageWidth}px`,
h = `var(--scale-factor) * ${pageHeight}px`;
const widthStr = useRound ? `round(${w}, 1px)` : `calc(${w})`,
heightStr = useRound ? `round(${h}, 1px)` : `calc(${h})`;
const widthStr = useRound
? `round(down, ${w}, var(--scale-round-x))`
: `calc(${w})`,
heightStr = useRound
? `round(down, ${h}, var(--scale-round-y))`
: `calc(${h})`;

if (!mustFlip || viewport.rotation % 180 === 0) {
style.width = widthStr;
Expand Down
30 changes: 25 additions & 5 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class PDFPageView {

#previousRotation = null;

#scaleRoundX = 1;

#scaleRoundY = 1;

#renderError = null;

#renderingState = RenderingStates.INITIAL;
Expand Down Expand Up @@ -1036,11 +1040,27 @@ class PDFPageView {
const sfx = approximateFraction(outputScale.sx);
const sfy = approximateFraction(outputScale.sy);

canvas.width = floorToDivide(width * outputScale.sx, sfx[0]);
canvas.height = floorToDivide(height * outputScale.sy, sfy[0]);
const { style } = canvas;
style.width = floorToDivide(width, sfx[1]) + "px";
style.height = floorToDivide(height, sfy[1]) + "px";
const canvasWidth = (canvas.width = floorToDivide(
width * outputScale.sx,
sfx[0]
));
const canvasHeight = (canvas.height = floorToDivide(
height * outputScale.sy,
sfy[0]
));
const pageWidth = floorToDivide(width, sfx[1]);
const pageHeight = floorToDivide(height, sfy[1]);
outputScale.sx = canvasWidth / pageWidth;
outputScale.sy = canvasHeight / pageHeight;

if (this.#scaleRoundX !== sfx[1]) {
this.div.style.setProperty("--scale-round-x", `${sfx[1]}px`);
this.#scaleRoundX = sfx[1];
}
if (this.#scaleRoundY !== sfy[1]) {
this.div.style.setProperty("--scale-round-y", `${sfy[1]}px`);
this.#scaleRoundY = sfy[1];
}

// Add the viewport so it's known what it was originally drawn with.
this.#viewportMap.set(canvas, viewport);
Expand Down
5 changes: 5 additions & 0 deletions web/pdf_viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
canvas {
margin: 0;
display: block;
width: 100%;
height: 100%;

&[hidden] {
display: none;
Expand All @@ -101,6 +103,9 @@
}

.pdfViewer .page {
--scale-round-x: 1px;
--scale-round-y: 1px;

direction: ltr;
width: 816px;
height: 1056px;
Expand Down

0 comments on commit d641dc5

Please sign in to comment.