简体   繁体   中英

Issue in Panning pdf file on canvas which is render with pdf.js?

I am rendering pdf file on canvas with pdf.js. How to Pan pdf on canvas? I am trying out some code for panning but chrome getting crash because of that. Same Code for panning works for image.

Let me know where I am going wrong.

var scale = 1;
 var canvas = document.getElementById('the-canvas');
        var context = canvas.getContext('2d');
function pdfCanvas(scale){

    PDFJS.getDocument('gkhead.pdf').then(function(pdf) {
      // Using promise to fetch the page
      pdf.getPage(1).then(function(page) {
       // var scale = 1.5;
        var viewport = page.getViewport(scale);

        //
        // Prepare canvas using PDF page dimensions
        //

        //canvas.height = viewport.height;
            canvas.height = 800;
        //canvas.width = viewport.width;
            canvas.width = 800;
            context.globalAlpha = 0.5;  


        //
        // Render PDF page into canvas context
        //
        var renderContext = {
          canvasContext: context,
          viewport: viewport
        };


        page.render(renderContext);
        context.restore();  


      });
    });
}

Trying out following code for panning:

var isDown = false;
        var startCoords = [];
        var last = [0, 0];

        canvas.onmousedown = function(e) {
            isDown = true;

            startCoords = [
                e.offsetX - last[0],
                e.offsetY - last[1]
           ];
        };

        canvas.onmouseup   = function(e) {
            isDown = false;

            last = [
                e.offsetX - startCoords[0], // set last coordinates
                e.offsetY - startCoords[1]
            ];
        };

        canvas.onmousemove = function(e)
        {
            if(!isDown) return;

            var x = e.offsetX;
            var y = e.offsetY;
            context.setTransform(1, 0, 0, 1,
                             x - startCoords[0], y - startCoords[1]);
            pdfCanvas(scale)
        }   

Recommend you using the following plugin

http://borbit.github.io/jquery.viewport/

The only thing you have to do is to create an viewport (eg div) and define the width and height that is smaller than your canvas.

您还可以使用“手动工具”,它只是...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM