最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set Quality of Png with html2canvas - Stack Overflow

programmeradmin5浏览0评论
        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(). Is there any Method ? Because Returned data low quality.

        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(). Is there any Method ? Because Returned data low quality.

Share Improve this question asked Aug 19, 2013 at 14:15 user2622044user2622044 1853 gold badges4 silver badges14 bronze badges 1
  • 1 I have the same problem here. In Safari html2canvas works very fine! but in Chrome is getting a very low image. Any ideas? – codebear22 Commented Jul 12, 2014 at 0:28
Add a comment  | 

3 Answers 3

Reset to default 7

Short answer:
Set your image size to 1/3 of it's original size.

Long answer:
Print quality of images on your website is 72dpi.

When you try print your page, all texts are rendered as high quality vectors (normally ~300dpi, depending on your print settings).

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.

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

you can use scale options in 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).

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

the approach with css scale is correct.

first we need to scale up element, then in "html2canvas" callback scale it down.

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
});
发布评论

评论列表(0)

  1. 暂无评论