diff options
author | Your Name <you@example.com> | 2017-12-23 20:08:09 +0100 |
---|---|---|
committer | Your Name <you@example.com> | 2017-12-23 20:08:09 +0100 |
commit | 79fa6979aa36b40983827fc5608b8bed85eea833 (patch) | |
tree | edd5ee0a6bbf883036d011248b8243d748e3adaa /ps/in/europe/pdf.viewer.js | |
parent | e1a06ca1c59c6af49671cf6f6ab48bcc95af6ef4 (diff) | |
download | lesoiseaux.io-79fa6979aa36b40983827fc5608b8bed85eea833.tar.gz |
Add existing ps.l.o site
Diffstat (limited to 'ps/in/europe/pdf.viewer.js')
-rw-r--r-- | ps/in/europe/pdf.viewer.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/ps/in/europe/pdf.viewer.js b/ps/in/europe/pdf.viewer.js new file mode 100644 index 0000000..b0cf96e --- /dev/null +++ b/ps/in/europe/pdf.viewer.js @@ -0,0 +1,93 @@ +var url="PUBLIC.v3.pdf" + +//PDFJS.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js'; +PDFJS.workerSrc = 'pdf.worker.js'; + +var pdfDoc = null, + pageNum = 1, + pageRendering = false, + pageNumPending = null, + scale = 1, + canvas = document.getElementById('the-canvas'), + ctx = canvas.getContext('2d'); + +/** + * Get page info from document, resize canvas accordingly, and render page. + * @param num Page number. + */ +function renderPage(num) { + pageRendering = true; + // Using promise to fetch the page + pdfDoc.getPage(num).then(function(page) { + var viewport = page.getViewport(scale); + canvas.height = viewport.height; + canvas.width = viewport.width; + + // Render PDF page into canvas context + var renderContext = { + canvasContext: ctx, + viewport: viewport + }; + var renderTask = page.render(renderContext); + + // Wait for rendering to finish + renderTask.promise.then(function() { + pageRendering = false; + if (pageNumPending !== null) { + // New page rendering is pending + renderPage(pageNumPending); + pageNumPending = null; + } + }); + }); + + // Update page counters + document.getElementById('page_num').textContent = pageNum; +} + +/** + * If another page rendering in progress, waits until the rendering is + * finised. Otherwise, executes rendering immediately. + */ +function queueRenderPage(num) { + if (pageRendering) { + pageNumPending = num; + } else { + renderPage(num); + } +} + +/** + * Displays previous page. + */ +function onPrevPage() { + if (pageNum <= 1) { + return; + } + pageNum--; + queueRenderPage(pageNum); +} +document.getElementById('prev').addEventListener('click', onPrevPage); + +/** + * Displays next page. + */ +function onNextPage() { + if (pageNum >= pdfDoc.numPages) { + return; + } + pageNum++; + queueRenderPage(pageNum); +} +document.getElementById('next').addEventListener('click', onNextPage); + +/** + * Asynchronously downloads PDF. + */ +PDFJS.getDocument(url).then(function(pdfDoc_) { + pdfDoc = pdfDoc_; + document.getElementById('page_count').textContent = pdfDoc.numPages; + + // Initial/first page rendering + renderPage(pageNum); +}); |