简体   繁体   English

HTML Div转换为带有背景图像Html2canvas的图像

[英]HTML Div convert into image with background image Html2canvas

I am put canvas into one HTML div, After that i am converting this div into image using Html2canvas. 我将画布放入一个HTML div中,然后使用Html2canvas将该div转换为图像。 HTML div successfully converted, but background image is not come. HTML div成功转换,但是背景图像不出现。

I have seen one link but i am not sure. 我已经看到一个链接,但不确定。 Because i have not seen any background image. 因为我还没有看到任何背景图片。 Javascript html2canvas cant get background JavaScript html2canvas无法获取背景

please suggest me any idea. 请给我建议任何想法。

 $(function() { $("#btnSave").click(function() { html2canvas($("#html-to-canvas"), { onrendered: function(canvas) { theCanvas = canvas; document.body.appendChild(canvas); Canvas2Image.saveAsPNG(canvas); $("#canvas-image").append(canvas); } }); }); }); 
  .bg-img { background-image:url('https://d2z4fd79oscvvx.cloudfront.net/0016463_petal_diamond_ring_320.jpeg'); background-repeat: no-repeat; } 
 <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script> <script src="https://raw.github.com/niklasvh/html2canvas/gh-pages/build/html2canvas.js"></script> <script src="https://raw.github.com/niklasvh/html2canvas/v0.34/src/plugins/jquery.plugin.html2canvas.js"></script> <div style="width:450px;height:500px;border:1px solid #333" class=" html-to-canvas bg-img" id="html-to-canvas"> <canvas id="canvas" style="margin-left:100px;margin-top:150px" width="200" height="300"></canvas> </div> <input type="button" id="btnSave" value="Save PNG"/> <div id="canvas-image"></div> 

html2canvas or Canvas2Image does not work with external images. html2canvasCanvas2Image不适用于外部图像。

All the images that the script uses need to reside under the same origin for it to be able to read them. 脚本使用的所有图像都必须位于同一原点,以便能够读取它们。 Ref 参考

Use relative path for the images and Try: 使用图像的相对路径,然后尝试:

Working code: 工作代码:

$(function() {
  $("#btnSave").click(function() {
    html2canvas($("#html-to-canvas"), {
      onrendered: function(canvas) {
        Canvas2Image.saveAsPNG(canvas);
        $("#canvas-image").append(canvas);
      }
    });
  });
});
.bg-img {
  background-image: url('images/0016463_petal_diamond_ring_320.jpeg');
  background-repeat: no-repeat;
}
<div style="width: 450px; height: 500px; border: 1px solid #333" class=" html-to-canvas bg-img" id="html-to-canvas">
  <canvas id="canvas" style="margin-left: 100px; margin-top: 150px" width="200" height="300"></canvas>
</div>
<input type="button" id="btnSave" value="Save PNG" />
<div id="canvas-image"></div>

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

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