Skip to content

Commit 8630e29

Browse files
JonasJonas
authored andcommitted
Avoid triggering rendering multiple times when zooming using the mouse wheel
1 parent 427dd39 commit 8630e29

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

web/viewer.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,12 +1119,14 @@ canvas {
11191119
background-color: white;
11201120
}
11211121

1122-
.page > a {
1122+
.page > a,
1123+
.annotationLayer > a {
11231124
display: block;
11241125
position: absolute;
11251126
}
11261127

1127-
.page > a:hover {
1128+
.page > a:hover,
1129+
.annotationLayer > a:hover {
11281130
opacity: 0.2;
11291131
background: #ff0;
11301132
box-shadow: 0px 2px 10px #ff0;

web/viewer.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,17 +311,23 @@ var PDFView = {
311311
selectScaleOption(value);
312312
},
313313

314-
zoomIn: function pdfViewZoomIn() {
315-
var newScale = (this.currentScale * DEFAULT_SCALE_DELTA).toFixed(2);
316-
newScale = Math.ceil(newScale * 10) / 10;
317-
newScale = Math.min(MAX_SCALE, newScale);
314+
zoomIn: function pdfViewZoomIn(ticks) {
315+
var newScale = this.currentScale;
316+
do {
317+
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
318+
newScale = Math.ceil(newScale * 10) / 10;
319+
newScale = Math.min(MAX_SCALE, newScale);
320+
} while (--ticks && newScale < MAX_SCALE);
318321
this.parseScale(newScale, true);
319322
},
320323

321-
zoomOut: function pdfViewZoomOut() {
322-
var newScale = (this.currentScale / DEFAULT_SCALE_DELTA).toFixed(2);
323-
newScale = Math.floor(newScale * 10) / 10;
324-
newScale = Math.max(MIN_SCALE, newScale);
324+
zoomOut: function pdfViewZoomOut(ticks) {
325+
var newScale = this.currentScale;
326+
do {
327+
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
328+
newScale = Math.floor(newScale * 10) / 10;
329+
newScale = Math.max(MIN_SCALE, newScale);
330+
} while (--ticks && newScale > MIN_SCALE);
325331
this.parseScale(newScale, true);
326332
},
327333

@@ -1554,6 +1560,8 @@ var PageView = function pageView(container, id, scale,
15541560
this.textContent = null;
15551561
this.textLayer = null;
15561562

1563+
this.annotationLayer = null;
1564+
15571565
var anchor = document.createElement('a');
15581566
anchor.name = '' + this.id;
15591567

@@ -1607,6 +1615,8 @@ var PageView = function pageView(container, id, scale,
16071615
div.removeChild(div.lastChild);
16081616
div.removeAttribute('data-loaded');
16091617

1618+
this.annotationLayer = null;
1619+
16101620
delete this.canvas;
16111621

16121622
this.loadingIconDiv = document.createElement('div');
@@ -1628,7 +1638,9 @@ var PageView = function pageView(container, id, scale,
16281638
enumerable: true
16291639
});
16301640

1631-
function setupAnnotations(annotationsDiv, pdfPage, viewport) {
1641+
var self = this;
1642+
1643+
function setupAnnotations(pageDiv, pdfPage, viewport) {
16321644

16331645
function bindLink(link, dest) {
16341646
link.href = PDFView.getDestinationHash(dest);
@@ -1687,6 +1699,12 @@ var PageView = function pageView(container, id, scale,
16871699
}
16881700

16891701
pdfPage.getAnnotations().then(function(annotationsData) {
1702+
if (self.annotationLayer) {
1703+
// If an annotationLayer already exists, delete it to avoid creating
1704+
// duplicate annotations when rapidly re-zooming the document.
1705+
pageDiv.removeChild(self.annotationLayer);
1706+
self.annotationLayer = null;
1707+
}
16901708
viewport = viewport.clone({ dontFlip: true });
16911709
for (var i = 0; i < annotationsData.length; i++) {
16921710
var data = annotationsData[i];
@@ -1725,7 +1743,13 @@ var PageView = function pageView(container, id, scale,
17251743
}
17261744
}
17271745

1728-
annotationsDiv.appendChild(element);
1746+
if (!self.annotationLayer) {
1747+
var annotationLayerDiv = document.createElement('div');
1748+
annotationLayerDiv.className = 'annotationLayer';
1749+
pageDiv.appendChild(annotationLayerDiv);
1750+
self.annotationLayer = annotationLayerDiv;
1751+
}
1752+
self.annotationLayer.appendChild(element);
17291753
}
17301754
});
17311755
}
@@ -2554,8 +2578,7 @@ window.addEventListener('DOMMouseScroll', function(evt) {
25542578

25552579
var ticks = evt.detail;
25562580
var direction = (ticks > 0) ? 'zoomOut' : 'zoomIn';
2557-
for (var i = 0, length = Math.abs(ticks); i < length; i++)
2558-
PDFView[direction]();
2581+
PDFView[direction](Math.abs(ticks));
25592582
} else if (PDFView.isPresentationMode) {
25602583
var FIREFOX_DELTA_FACTOR = -40;
25612584
PDFView.mouseScroll(evt.detail * FIREFOX_DELTA_FACTOR);

0 commit comments

Comments
 (0)