Set Quality of Png with html2canvas

后端 未结 3 1101
生来不讨喜
生来不讨喜 2020-12-18 15:14
        html2canvas($(\"#Element\"), {
  onrendered: function(canvasq) {
    var w=window.open();
    w.document.write(\"

\"+Re

相关标签:
3条回答
  • 2020-12-18 15:17

    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 讨论(0)
  • 2020-12-18 15:27

    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
    });
    
    0 讨论(0)
  • 2020-12-18 15:39

    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

    0 讨论(0)
提交回复
热议问题