HTML2canvas generates Blurry images

前端 未结 8 1429
庸人自扰
庸人自扰 2020-12-01 06:45

I am using jsPDF and it uses html2canvas to generate an image from some html element and insert on the .pdf file. But there is a problem on html2canvas, it generates blurry

8条回答
  •  有刺的猬
    2020-12-01 07:02

    I had this problem because I was on a retina display. I solved in by using MisterLamb's solution here.

    $(window).load(function () {
    
        var scaleBy = 5;
        var w = 1000;
        var h = 1000;
        var div = document.querySelector('#screen');
        var canvas = document.createElement('canvas');
        canvas.width = w * scaleBy;
        canvas.height = h * scaleBy;
        canvas.style.width = w + 'px';
        canvas.style.height = h + 'px';
        var context = canvas.getContext('2d');
        context.scale(scaleBy, scaleBy);
    
        html2canvas(div, {
            canvas:canvas,
            onrendered: function (canvas) {
                theCanvas = canvas;
                document.body.appendChild(canvas);
    
                Canvas2Image.saveAsPNG(canvas);
                $(body).append(canvas);
            }
        });
    });
    

    HTML and PNG without scaling

    HTML and PNG with scaling

提交回复
热议问题