[英]html2canvas to convert div to canvas and then to image
I want to convert a div to canvas and then eventually to image.我想将 div 转换为 canvas,然后最终转换为图像。 So I am using the html2canvas library.所以我正在使用 html2canvas 库。 I have tried downloading image, opening image in new window, and appending to a div, but none of them has worked.我已经尝试下载图像,在新的 window 中打开图像,并附加到一个 div,但它们都没有奏效。
$(document).ready(function (e) { $("#final_download").click(function() { html2canvas($("#final_image_div")[0]).then(function (canvas) { console.log(canvas); var myImage = canvas.toDataURL("image/png"); // download the generated image downloadURI(myImage, "final_image.png"); // Open the image in a new window window.open(myImage, "_blank"); // display it in div $("#bottom").append(myImage); }); }); });
<script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.3/html2canvas.min.js"></script> <div id="final_image_div">abcdef</div> <img src="images/download.jpg" id="final_download"> <div class="bottom"></div>
Any idea as to what I am doing wrong here?关于我在这里做错了什么的任何想法?
I tried to run your code and it was just lacking a dowloadURL
function (there is no such thing in Vanilla JS).我试图运行你的代码,它只是缺少一个dowloadURL
function (Vanilla JS 中没有这样的东西)。
I simply added the following code and then everything worked as expected:我只是添加了以下代码,然后一切都按预期工作:
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
link.click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.