简体   繁体   English

在toDataURL之前缩放图像 - html2canvas

[英]Scale image before toDataURL - html2canvas

Before you tell me this is a duplicate question, know that I've searched through every single similar question and none of the answers in any of them are working for me. 在你告诉我这是一个重复的问题之前,请知道我已经搜索过一个相似的问题, 其中任何答案都没有为我工作。

Im using html2canvas to grab a snapshot of a div, and what I need to do is scale it up to 750x1050 before saving it to a png via canvas.toDataURL() . 我使用html2canvas来获取div的快照,我需要做的是将其扩展到750x1050,然后通过canvas.toDataURL()将其保存到png。

The closest I got was with the following code. 我得到的最接近的是以下代码。

html2canvas(document.getElementById('div_id'), {
   onrendered: function(canvas) {

      var extra_canvas = document.createElement("canvas");

        extra_canvas.setAttribute('width', 750);
        extra_canvas.setAttribute('height', 1050);

        var ctx = extra_canvas.getContext('2d');
        ctx.drawImage(canvas, 0, 0, 750, 1050);
        var dataURL = extra_canvas.toDataURL();

        window.open(dataURL);
   }
});

The image was sized properly but the text within the image was extremely poor quality, as if it resized it after becoming a png. 图像大小合适,但图像中的文字质量极差,就像它成为png 调整大小一样。

Is it that I'm doing something wrong or can you just not scale up this way? 是我做错了什么还是你不能这样放大?

Any and every suggestion/work-around will be greatly appreciated! 任何建议/解决方案将不胜感激!

对于其他任何想知道如何从html获得高分辨率打印内容的人: PhantomJSwkhtmltopdf / wkhtmltoimage是更好地处理这些事情的绝佳选择。

I had bit similar problem and this is what I ended up doing 我有点类似的问题,这就是我最终做的

html2canvas($('#div_id'), {width: 750, height: 1050}).then(
    function(canvas) {
       window.open(canvas.toDataURL("image/png"));
    }
)

Now this still lead to blurry images (especially with text), but that was because my default zoom on browser was set to 110% those causing the window.devicePixelRatio to be 1.1000... I sorted that out by simply showing warning for user (worked for the purpose I need it), but apparently there is a better way to solve it https://stackoverflow.com/a/22819006/460586 现在这仍然会导致图像模糊(尤其是文本),但这是因为我在浏览器上的默认缩放设置为110%,导致window.devicePixelRatio为1.1000 ...我通过简单地向用户显示警告来对其进行排序(工作的目的我需要它),但显然有一个更好的方法来解决它https://stackoverflow.com/a/22819006/460586

Even my images were coming pixelized and sometimes cramped when there was lot of content to fit within a pre-set width and height. 甚至我的图像都是像素化的,当有很多内容适合预先设定的宽度和高度时,有时会变得狭窄。 After hours of searching, found a good solution from this post . 经过搜索数小时,发现从这个很好的解决 It takes care of maintaining resolution to good extent even on zooming and no visible pixelization. 即使在缩放和没有可见像素化的情况下,它也能很好地保持分辨率。

html2canvas(document.getElementById('div_id'), {
onrendered: function(canvas) {
        var ctx = canvas.getContext('2d');
        ctx.webkitImageSmoothingEnabled = false;
        ctx.mozImageSmoothingEnabled = false;
        ctx.imageSmoothingEnabled = false;
        var myImage = canvas.toDataURL("image/jpeg,1.0");  
       }
 }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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