Add preDraw() functionality to render the next page in the background

This commit is contained in:
Julian Viereck 2011-12-18 22:36:36 +01:00
parent dba1a7dc6f
commit 5830474ef7

View File

@ -33,6 +33,9 @@ var RenderingQueue = (function RenderingQueueClosure() {
RenderingQueue.prototype = { RenderingQueue.prototype = {
enqueueDraw: function RenderingQueueEnqueueDraw(item) { enqueueDraw: function RenderingQueueEnqueueDraw(item) {
if (!item.drawingRequired())
return; // as no redraw required, no need for queueing.
if ('rendering' in item) if ('rendering' in item)
return; // is already in the queue return; // is already in the queue
@ -366,6 +369,7 @@ var PDFView = {
// when page is painted, using the image as thumbnail base // when page is painted, using the image as thumbnail base
pageView.onAfterDraw = function pdfViewLoadOnAfterDraw() { pageView.onAfterDraw = function pdfViewLoadOnAfterDraw() {
thumbnailView.setImage(pageView.canvas); thumbnailView.setImage(pageView.canvas);
preDraw();
}; };
} }
@ -759,8 +763,12 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
}, 0); }, 0);
}; };
this.drawingRequired = function() {
return !div.hasChildNodes();
};
this.draw = function pageviewDraw(callback) { this.draw = function pageviewDraw(callback) {
if (div.hasChildNodes()) { if (!this.drawingRequired()) {
this.updateStats(); this.updateStats();
callback(); callback();
return; return;
@ -1004,16 +1012,49 @@ window.addEventListener('unload', function webViewerUnload(evt) {
window.scrollTo(0, 0); window.scrollTo(0, 0);
}, true); }, true);
/**
* Render the next not yet visible page already such that it is
* hopefully ready once the user scrolls to it.
*/
function preDraw() {
var pages = PDFView.pages;
var visible = PDFView.getVisiblePages();
var last = visible[visible.length - 1];
// PageView.id is the actual page number, which is + 1 compared
// to the index in `pages`. That means, pages[last.id] is the next
// PageView instance.
if (pages[last.id] && pages[last.id].drawingRequired()) {
renderingQueue.enqueueDraw(pages[last.id]);
return;
}
// If there is nothing to draw on the next page, maybe the user
// is scrolling up, so, let's try to render the next page *before*
// the first visible page
if (pages[visible[0].id - 2]) {
renderingQueue.enqueueDraw(pages[visible[0].id - 2]);
}
}
function updateViewarea() { function updateViewarea() {
var visiblePages = PDFView.getVisiblePages(); var visiblePages = PDFView.getVisiblePages();
var pageToDraw;
for (var i = 0; i < visiblePages.length; i++) { for (var i = 0; i < visiblePages.length; i++) {
var page = visiblePages[i]; var page = visiblePages[i];
renderingQueue.enqueueDraw(PDFView.pages[page.id - 1]); var pageObj = PDFView.pages[page.id - 1];
pageToDraw |= pageObj.drawingRequired();
renderingQueue.enqueueDraw(pageObj);
} }
if (!visiblePages.length) if (!visiblePages.length)
return; return;
// If there is no need to draw a page that is currenlty visible, preDraw the
// next page the user might scroll to.
if (!pageToDraw) {
preDraw();
}
updateViewarea.inProgress = true; // used in "set page" updateViewarea.inProgress = true; // used in "set page"
var currentId = PDFView.page; var currentId = PDFView.page;
var firstPage = visiblePages[0]; var firstPage = visiblePages[0];