简体   繁体   English

在vuejs中将div另存为图片文件

[英]Save div as image file in vuejs

I want to save div element as an image file using VueJS.我想使用 VueJS 将 div 元素保存为图像文件。 I tried using canvas.我尝试使用画布。 How can I convert and save div element as an image file?如何将 div 元素转换并保存为图像文件?

You can use html2canvas :您可以使用html2canvas

JS : JS:

html2canvas(document.querySelector("#capture")).then(canvas => {
    document.body.appendChild(canvas)
});

HTML : HTML :

<script src="html2canvas.min.js"></script>
<div id="capture" style="padding: 10px; background: #f5da55">
  <h4 style="color: #000; ">Hello world!</h4>
</div>

html2canvas.min.js need to be in your website : html2canvas.min.js需要在您的网站中:

All the images that the script uses need to reside under the same origin for it to be able to read them without the assistance of a proxy.脚本使用的所有图像都需要驻留在同一来源下,以便它能够在没有代理帮助的情况下读取它们。 Similarly, if you have other canvas elements on the page, which have been tainted with cross-origin content, they will become dirty and no longer readable by html2canvas同样,如果页面上有其他画布元素,这些元素已经被跨源内容污染,它们将变脏并且不再被 html2canvas 读取
Source: https://html2canvas.hertzen.com/documentation来源: https : //html2canvas.hertzen.com/documentation

vue.js 有一个包装器,可以在 vue 中更轻松地使用 html2canvas: https : //github.com/mycurelabs/vue-html2canvas

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

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