javascript - 在vuejs中将div保存为图像文件

标签 javascript html vuejs2

我想使用 VueJS 将 div 元素保存为图像文件。我尝试使用 Canvas 。如何将 div 元素转换并保存为图像文件?

最佳答案

您可以使用 html2canvas :

JS:

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

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 需要在您的网站中:

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
Source: https://html2canvas.hertzen.com/documentation

关于javascript - 在vuejs中将div保存为图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50758473/

相关文章:

javascript - 如何将自由绘制的线条作为线条列表获取,其中每条线条都是坐标列表?

javascript - 带有第一个 div 的 jQuery 自定义 Accordion 在初始加载时打开 - 修改箭头图像的问题

android - Weex跨设备本地 Assets 定位

vue.js - Vuetify 在特定网格大小上添加类

javascript - 对 VueJS 生命周期钩子(Hook)的误解

javascript - 如何从 Javascript 中的对象中选择属性

javascript - yui 子选择器

jquery - 较大的脚本文件失败但较小的似乎没问题

jquery - 更改动画 Sprite 表时 HTML5 Canvas 挂起(逐帧视频播放)

javascript - 为 for 循环中的每个项目添加 onclick 处理程序