如何用vue实现网页截图你知道吗
作者:suoh's Blog 时间:2024-04-27 15:51:31
目录
1、安装html2Canvas
2、在需要的vue组件中引入
3、编写一个截图按钮
4、调用函数toImage
总结
1、安装html2Canvas
npm install html2canvas --save
2、在需要的vue组件中引入
import html2canvas from "html2canvas";
3、编写一个截图按钮
<el-button class="button-dalod" size="mini" title="生成图片" @click="toImage()" icon="el-icon-download"></el-button>
4、调用函数toImage
// 页面元素转图片
toImage () {
// 手动创建一个 canvas 标签
const canvas = document.createElement("canvas")
// 获取父标签,意思是这个标签内的 DOM 元素生成图片
// imageTofile是给截图范围内的父级元素自定义的ref名称
let canvasBox = this.$refs.imageTofile
// 获取父级的宽高
const width = parseInt(window.getComputedStyle(canvasBox).width)
const height = parseInt(window.getComputedStyle(canvasBox).height)
// 宽高 * 2 并放大 2 倍 是为了防止图片模糊
canvas.width = width * 2
canvas.height = height * 2
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
const context = canvas.getContext("2d");
context.scale(2, 2);
const options = {
backgroundColor: null,
canvas: canvas,
useCORS: true
}
html2canvas(canvasBox, options).then((canvas) => {
// toDataURL 图片格式转成 base64
let dataURL = canvas.toDataURL("image/png")
console.log(dataURL)
this.downloadImage(dataURL)
})
},
//下载图片
downloadImage(url) {
// 如果是在网页中可以直接创建一个 a 标签直接下载
let a = document.createElement('a')
a.href = url
a.download = '首页截图'
a.click()
},
别忘了给页面所在截图范围内的父级添加ref属性,方便canvas找到父级计算宽高从而截屏
这就是截图出来的效果:
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注asp之家的更多内容!
来源:https://blog.csdn.net/qq_41579104/article/details/121360337
标签:vue,网页,截图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决vue watch数据的方法被调用了两次的问题
2024-05-05 09:10:33
关于Pandas count()与values_count()的用法及区别
2021-09-25 08:28:20
![](https://img.aspxhome.com/file/2023/6/101186_0s.png)
使用Python中Tkinter模块的Treeview 组件显示ini文件操作
2022-05-23 03:45:38
![](https://img.aspxhome.com/file/2023/4/135084_0s.png)
python利用正则表达式搜索单词示例代码
2023-02-21 11:31:03
Mysql+Keepalived实现双主热备方式
2024-01-27 10:23:34
CSS属性behavior的语法及介绍
2010-01-13 12:40:00
Python3中的2to3转换工具使用示例
2022-04-26 17:33:36
Go项目实现优雅关机与平滑重启功能
2023-07-16 07:36:13
用python画了个圣诞树给女朋友
2022-06-03 00:54:44
![](https://img.aspxhome.com/file/2023/5/97355_0s.jpg)
go中的unsafe包及使用详解
2023-10-13 17:07:27
捕捉并保存ASP运行错误的函数代码
2012-11-30 20:24:43
python为QT程序添加图标的方法详解
2021-05-16 15:56:08
![](https://img.aspxhome.com/file/2023/4/83754_0s.png)
使用AngularJS制作一个简单的RSS阅读器的教程
2024-05-03 15:31:35
![](https://img.aspxhome.com/file/2023/5/129715_0s.png)
python 下载文件的多种方法汇总
2023-08-11 16:50:05
![](https://img.aspxhome.com/file/2023/7/105017_0s.gif)
python接口测试对修改密码接口进行压测
2022-05-16 04:26:15
如何设计注册激活邮件
2010-01-12 13:14:00
![](https://img.aspxhome.com/file/UploadPic/20101/12/2010-01-03_163629-89s.png)
python高手之路python处理excel文件(方法汇总)
2021-02-17 10:33:15
![](https://img.aspxhome.com/file/2023/4/101664_0s.png)
asp随机数 随机产生N位由数字和字母组成的密码
2011-03-10 10:47:00
解决Python中的modf()函数取小数部分不准确问题
2023-06-12 06:24:43
![](https://img.aspxhome.com/file/2023/3/135023_0s.png)
谈谈网页设计中的字体应用 (2) serif 和 sans-serif
2009-11-24 13:04:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/202939796-35s.gif)