[Pkg-javascript-commits] [pdf.js] 232/246: Debounces scroll events in web viewer.
David Prévot
taffit at moszumanska.debian.org
Sun Sep 7 15:36:44 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository pdf.js.
commit 403a4e2bda3b1a879812d880f10e42665172b983
Author: Fabian Lange <lange.fabian at gmail.com>
Date: Mon Aug 18 21:16:46 2014 +0200
Debounces scroll events in web viewer.
As requested in #5178, this change debounces the scroll events.
The reason for doing so is that browsers can event-storm especially on
scroll, communicating hundreds of subpixel changes.
The main reason for this resulting in poor performance is that on each
scroll event `scrollTop` was queried, which forces layouting.
This change will use `requestAnimationFrame` to make sure the browser can
allocate enough time to other tasks. The delay is however quite small, thus
the reduction in executions is less noticeable. Modern browsers however utilize
`requestAnimationFrame` to smoothen out rendering.
---
web/viewer.js | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/web/viewer.js b/web/viewer.js
index 06dd580..4a4282e 100644
--- a/web/viewer.js
+++ b/web/viewer.js
@@ -274,20 +274,28 @@ var PDFView = {
watchScroll: function pdfViewWatchScroll(viewAreaElement, state, callback) {
state.down = true;
state.lastY = viewAreaElement.scrollTop;
- viewAreaElement.addEventListener('scroll', function webViewerScroll(evt) {
- if (!PDFView.pdfDocument) {
+ state.rAF = null;
+ viewAreaElement.addEventListener('scroll', function debounceScroll(evt) {
+ if (state.rAF) {
return;
}
- var currentY = viewAreaElement.scrollTop;
- var lastY = state.lastY;
- if (currentY > lastY) {
- state.down = true;
- } else if (currentY < lastY) {
- state.down = false;
- }
- // else do nothing and use previous value
- state.lastY = currentY;
- callback();
+ // schedule an invocation of webViewerScrolled for next animation frame.
+ state.rAF = window.requestAnimationFrame(function webViewerScrolled() {
+ state.rAF = null;
+ if (!PDFView.pdfDocument) {
+ return;
+ }
+ var currentY = viewAreaElement.scrollTop;
+ var lastY = state.lastY;
+ if (currentY > lastY) {
+ state.down = true;
+ } else if (currentY < lastY) {
+ state.down = false;
+ }
+ // else do nothing and use previous value
+ state.lastY = currentY;
+ callback();
+ });
}, true);
},
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git
More information about the Pkg-javascript-commits
mailing list