vue 页面生成图片保存
需求:将页面中的元素转成图片,支持保存或下载。要求下载的图片包含页面背景,头像,用户名,文本为“我的邀请码”和个人二维码。
实现:将页面绘制到canvas中,生成base64图片链接,支持移动端的长按保存图片。
技术与插件:vue,qrcodejs2,html2canvas,nutUI
示例:以下将代码分成了三部分,合成便是一个完整的vue单页。
HTML代码:
<template>
<div class="poster">
<div class="hb" ref="imageTest">
<img src="../../assets/images/4399_09180407154.jpg" alt="" class="hb-img">
<p class="hb-name">旋风小土豆</p>
<div class="hb-qc">
<p class="hb-yqm">我的邀请码</p>
<div class="codeImg" id="qrCode"></div>
</div>
</div> <div class="close" @click="handleClose">
<nut-icon type="circle-cross" ></nut-icon>
</div> <div class="save-btn" >
<nut-button
block
shape="circle"
@click="setCanvas"
>
保存图片
</nut-button>
</div> <nut-popup v-model="show">
<img :src="cvImg" alt="" class="cv-img">
</nut-popup>
</div>
</template>
SCRIPT:
<script>
import QRCode from 'qrcodejs2'
import html2canvas from 'html2canvas'
export default {
props: { },
data () {
return {
show: false,
cvImg: ''
}
},
components: {
},
created () {
this.$nextTick(() => {
this.getQC()
})
},
mounted () {
},
watch: {
},
methods: {
handleClose () {
console.log('关闭')
this.$router.go(-1)
},
getQC () {
let qrcode = new QRCode('qrCode', {
width: 150,
height: 150, // 高度
text: `${location.origin}${location.pathname}`, // 要生成二维码的链接(或当前页面地址)
colorDark: '#000',
colorLight: '#fff',
correctLevel: QRCode.CorrectLevel.H
})
console.log(qrcode, `${location.origin}${location.pathname}`)
}, setCanvas () {
const canvas = document.createElement('canvas')
// 获取要生成图片的 DOM 元素
let canvasDom = this.$refs.imageTest
// 获取指定的宽高
const width = parseInt(window.getComputedStyle(canvasDom).width)
const height = parseInt(window.getComputedStyle(canvasDom).height)
// 宽高扩大 2 倍 处理图片模糊
canvas.width = width * 2
canvas.height = height * 2
canvas.style.width = width / 2 + 'px'
canvas.style.height = height / 2 + 'px'
const context = canvas.getContext('2d')
context.scale(1, 1)
const options = {
backgroundColor: null,
canvas: canvas,
useCORS: true
}
html2canvas(canvasDom, options).then(canvas => {
// 生成图片地址
this.imgUrl = canvas.toDataURL('image/png')
this.cvImg = this.imgUrl
this.show = true
})
} }
}
</script>
CSS代码:
<style lang="scss" scoped>
.hb{
@include wh(100%, 100vh);
background: url(../../assets/images/c_bg.jpg) no-repeat;
background-size: 100%; .hb-img{
@include wh(80px, 80px);
margin-top: 100px;
border-radius: 10px;
} .hb-name{
color: #FFFFFF;
margin: 10px 0;
font-size: 18px;
} .hb-qc{
@include wh(80%, 260px);
margin: 20px auto;
background-color: rgba(255,255,255, 0.7);
border-radius: 20px;
text-align: center; .hb-yqm{
padding: 10px 20px;
text-align: left;
font-size: 14px;
color: #D37D42;
} .codeImg{
display: flex;
justify-content: center;
margin-top: 20px;
}
} .hb-desc{
@include wh(100%, auto);
}
}
.cv-img{
@include wh(100%, 80vh);
}
.close{
position: absolute;
top: 40px;
left: 40px;
@include wh(40px, 40px);
}
.save-btn{
position: absolute;
bottom: 50px;
@include wh(100%, auto);
}
/deep/ .nut-button{
background: linear-gradient(315deg, rgba(50,213,236, 0.5) 0%, rgba(40,205,231, 0.5) 100%);
color: #2D2D2D;
border: 1px solid rgba(50,213,236, 1);
}
/deep/ .nut-button.block{
@include wh(80%, 40px);
}
/deep/ .popup-box{
animation-duration: 0.3s;
width: 82%;
position: fixed;
max-height: 100%;
z-index: 2002;
text-align: center;
display: flex;
}
</style>
结果成图:

点击保存图片:

下载后的图片:

vue 页面生成图片保存的更多相关文章
- 使用 html2canvas 点击保存时把当前页面生成图片
style: #box{ background-image:url('./img/pone.png') } body: <div id="box" ...
- 微信小程序中把页面生成图片
这个问题我上网搜了一下,答案有多种,但是真正能用的没有几何.很多答案都是雷同,有的网友也不负责任,直接拿来照抄,自己也不跑一遍看看.哎,不说了,说多了全是泪.希望我们的技术达人在分享的时候,能够真实的 ...
- 将HTML页面自动保存为PDF文件并上传的两种方式(一)-前端(react)方式
一.业务场景 公司的样本检测报告以React页面的形式生成,已调整为A4大小的样式并已实现分页,业务上需要将这个网页生成PDF文件,并上传到服务器,后续会将这个文件发送给客户(这里不考虑). 二.原来 ...
- C# html生成图片保存下载
最近有个需求,需要把内容生成图片,我找到一些资料可以将html页面生成图片并保存下载 下面是简单的实现 1.html页面 @{ Layout = null; } <!DOCTYPE html&g ...
- Vue页面内公共的多类型附件图片上传区域并适用折叠面板
在前端项目中,附件上传是很常用的功能,几乎所有的app相关项目中都会使用到,一般在选择使用某个前端UI框架时,可以查找其内封装好的图片上传组件,但某些情况下可能并不适用于自身的项目需求,本文中实现的附 ...
- iframe嵌套vue页面打开新窗口
iframe嵌套vue页面时目录结构为下图: 此时出口文件指向index.html, 所以只需要用a标签动态拼接href, 并设置属性 target="_blank" ,即可在if ...
- python 全栈开发,Day92(编程式的导航,vue页面布局,marked包的使用)
昨日内容回顾 1. 组件间的传值 1. bus --> 空Vue对象 通过向bus对象抛出自定义事件的方式在组件间传递信息 2. 注意事项: 1. bus.$on()应该在组件mounted(挂 ...
- 微信小程序导出当前画布指定区域的内容并生成图片保存到本地相册(canvas)
最近在学小程序,在把当前画布指定区域的内容导出并生成图片保存到本地这个知识点上踩坑了. 这里用到的方法是: wx.canvasToTempFilePath(),该方法作用是把当前画布指定区域的内容导出 ...
- Vue页面骨架屏(一)
在开发webapp的时候总是会受到首屏加载时间过长的影响,主流的解决方法是在载入完成之前显示loading图效果,而一些大公司会配置一套服务端渲染的架构来解决这个问题.考虑到ssr所要解决的一系列问题 ...
随机推荐
- k8s二进制部署 - dashboard安装
配置资源清单rbac.yaml apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard ...
- kubernetes实战-交付dubbo服务到k8s集群(二)交付jenkins到k8s集群
首先下载jenkins镜像并上传到我们自己的私有仓库:7-200 # docker pull jenkins/jenkins:2.190.3 # docker tag 22b8b9a84dbe har ...
- 牛年 dotnet云原生技术趋势
首先祝大家:新年快乐,牛年大吉,牛年发发发! 2020年的春节,新冠疫情使得全球业务停滞不前,那时候,没有人知道会发生什么,因此会议被取消,合同被搁置,项目被推迟,一切似乎都停止了.但是我们却见证了I ...
- Linux bash script regex auto replace
Linux bash script regex auto replace 自动替换 /assets/css/0.styles.96df394b.css => ./assets/css/0.sty ...
- 20 个使用原生 JavaScript 实现的 Web 项目
20 个使用原生 JavaScript 实现的 Web 项目 20 vanilla JavaScript Web Projects https://github.com/learning-js-by- ...
- Web SQL& IndexedDB
Web SQL& IndexedDB https://developer.mozilla.org/en/docs/Web/API/IndexedDB_API https://mdn.githu ...
- Flutter & APP & UI Components
Flutter & APP & UI Components 下拉刷新或者上拉加载 https://github.com/OpenFlutter/flutter_screenutil h ...
- Array in Depth
Array in Depth Array.concat() & Array.push() https://developer.mozilla.org/en-US/docs/Web/JavaSc ...
- news of javascript
news of javascript https://javascriptweekly.com/ https://javascriptweekly.com/issues https://www.inf ...
- 教你玩转CSS 居中
1.元素居中对齐 要水平居中对齐一个元素(如 <div>), 可以使用 margin: auto;. 设置到元素的宽度将防止它溢出到容器的边缘. 元素通过指定宽度,并将两边的空外边距平均分 ...