简体   繁体   English

HTML2Canvas保存画布错误

[英]HTML2Canvas Save Canvas Error

Looking at using HTML2Canvas to save a HTML div, I am looking at saving the div with the id "imagesave" I have seen examples online of the following code working to do this but when I press download image nothing happens, how can I make this work? 在查看使用HTML2Canvas保存HTML div时,我正在查看ID为“ imagesave”的div。我已经在网上看到了以下代码的示例,但是当我按下载图像时,什么也没发生,我该如何做工作?

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>

<button id="save_image_locally">download img</button>

   <div id="imagesave">
     <p>testing</p>
        <p>testing</p>
   </div>

<script>

  $('#save_image_locally').click(function(){
    html2canvas($('#imagesave'), 
    {
      onrendered: function (canvas) {
        var a = document.createElement('a');
        a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
        a.download = 'somefilename.jpg';
        a.click();
      }
    });
  });

</script>
</body>
</html>

specify background color, add CSS like: 指定背景颜色,添加CSS,例如:

#imagesave {
  background:lightblue;
}

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

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