如何提高`html2canvas`屏幕截图的质量?

时间:2016-10-18 06:45:39

标签: javascript html css html2canvas

我使用了以下html2canvas代码来截取并下载它。

html按钮创建

<button class="btn btn-default btn-sm" style="margin:0px 0px -10px 970px; padding:2px 4px 1px 4px" onclick="genScreenshot()"><span class="glyphicon glyphicon-envelope"></span></button>
<a id="test"></a>
<div id="box1"></div>

功能定义

<script type="text/javascript">                         
function genScreenshot() {
html2canvas(document.body, {
  onrendered: function(canvas) {
  $('#box1').html("");

  if (navigator.userAgent.indexOf("MSIE ") > 0 || 
                navigator.userAgent.match(/Trident.*rv\:11\./)) 
       {
    var blob = canvas.msToBlob();
    window.navigator.msSaveBlob(blob,'Test file.png');
       }
  else {
    $('#test').attr('href', canvas.toDataURL("image/png"));
    $('#test').attr('download','screenshot.png');
    $('#test')[0].click();
       }
                                }
                              });
                          }  
</script>

但下载截图的质量并不理想。如何提高质量?

1 个答案:

答案 0 :(得分:0)

怎么样呢?

var $wrapper = $("#yourDiv");
setSize($wrapper, "2000px", "20pt");

html2canvas($wrapper, {
    onrendered: function (canvas) {
        var a = document.createElement('a');
        a.href = canvas.toDataURL("image/jpg");
        a.download = 'filename.jpg';
        a.click();

        setSize($wrapper, "1000px", "10pt");
    }
});

function setSize(dv, width, fontsize) {
    dv[0].style.width = width;
    dv[0].style.fontSize = fontsize;
}

这会将div和字体的大小调整为更大的大小,然后再缩小。