简体   繁体   English

使用html2canvas设置Png的质量

[英]Set Quality of Png with html2canvas

        html2canvas($("#Element"), {
  onrendered: function(canvasq) {
    var w=window.open();
    w.document.write("<h3 style='text-align:center;'>"+ReportTitle+"</h3>");
    w.document.write("<img width='100%' height:'90%' src='"+canvasq.toDataURL()+"' />");
    w.print();
  }
});

I wanna increase quality of canvasq.toDataURL(). 我想提高canvasq.toDataURL()的质量。 Is there any Method ? 有什么方法吗? Because Returned data low quality. 因为返回数据质量低。

Short answer: 简短回答:
Set your image size to 1/3 of it's original size. 将图像大小设置为原始大小的1/3。

Long answer: 答案很长:
Print quality of images on your website is 72dpi. 您网站上的图像打印质量为72dpi。

When you try print your page, all texts are rendered as high quality vectors (normally ~300dpi, depending on your print settings). 当您尝试打印页面时,所有文本都会呈现为高质量的矢量(通常为~300dpi,具体取决于您的打印设置)。

So if you need to print out a high-quality image, you'll need to scale it down at least to a third of it's original size. 因此,如果您需要打印出高质量的图像,则需要将其缩小至少至原始尺寸的三分之一。

An when you're using html2canvas library to produce the image, you'll have to set all the CSS sizes to 300% before doing the render. 当您使用html2canvas库生成图像时,必须在进行渲染之前将所有CSS尺寸设置为300%。

So consider this: 所以考虑一下:

var ReportTitle = 'Hello world!';  // For demonstration

var bigCanvas = $("<div>").appendTo('body');  // This will be the 3x sized canvas we're going to render
var scaledElement = $("#Element").clone()
.css({
  'transform': 'scale(3,3)',
  'transform-origin': '0 0'
})
.appendTo(bigCanvas);

var oldWidth = scaledElement.width();
var oldHeight = scaledElement.height();

var newWidth = oldWidth * 3;
var newHeight = oldHeight * 3;

bigCanvas.css({
  'width': newWidth,
  'height': newHeight
})

html2canvas(bigCanvas, {
  onrendered: function(canvasq) {
    var w=window.open();
    w.document.write("<h3 style='text-align:center;'>"+ReportTitle+"</h3>");
    w.document.write("<img width='"+oldWidth+"' height='"+oldHeight+"' src='"+canvasq.toDataURL()+"' />");
    w.print();
    bigCanvas.remove() 
  }
});

Working JSBin link 工作中的JSBin链接

the approach with css scale is correct. css量表的方法是正确的。

first we need to scale up element, then in "html2canvas" callback scale it down. 首先我们需要扩展元素,然后在“html2canvas”中回调缩小它。

var ELEMENT = jQuery("#ELEMENT");

//get element width and height
var w = ELEMENT.width();
var h = ELEMENT.height();

//scale up your element
ELEMENT.css({
'transform': 'scale(3)',
'-ms-transform': 'scale(3)',
'-webkit-transform': 'scale(3)' });

//run html2canvas
html2canvas(ELEMENT, {
onrendered: function(canvas) {

//scale back your element
ELEMENT.css({
'transform': '',
'-ms-transform': '',
'-webkit-transform': '' });

//your actions to canvas
var win = window.open();
win.document.write('<h3 style="text-align:center;">ttl</h3>');
win.document.write('<img width="100%" height:"90%" src="'+canvas.toDataURL()+'"/>');
win.print();


},
//set proper canvas width and height
width: w*3,
height: h*3
});

you can use scale options in html2canvas. 您可以在html2canvas中使用缩放选项。

In the latest release, v1.0.0-alpha.1, you can use the scale option to increase the resolution (scale: 2 will double the resolution from the default 96dpi). 在最新版本v1.0.0-alpha.1中,您可以使用scale选项来提高分辨率(scale:2将使分辨率从默认的96dpi翻倍)。

// Create a canvas with double-resolution.
html2canvas(element, {
    scale: 2,
    onrendered: myRenderFunction
});

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

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