uniapp微信H5 dom转换成图片并下载(html2canvas )

由于微信安全防控,我们无法直接生成并下载,但是可以打开图片后长按保存

1、npm依赖

 npm install html2canvas --save

2、引入

javascript 复制代码
	import html2canvas from "html2canvas"

3、方法

javascript 复制代码
exportCardAsImage() {
				const cardElement = this.$refs.card.$el;
				console.log(this.$refs.card);
				html2canvas(cardElement).then((canvas) => {
					const imageData = canvas.toDataURL('image/png');
					console.log("图片", imageData);
					this.pic(imageData)
				});
			},
			pic(src) {
				uni.previewImage({
					urls: Array.isArray(src) ? src : [src],
					current: '',
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				});
			},

4、完整代码

javascript 复制代码
<template>
	<view class="index">
		<view class="top">
			<view class="bg">
				<image src="../../static/electronic_card/bg.png"></image>
			</view>
		</view>
		<view class="main">
			<view class="card" ref="card">
				<image src="../../static/card/bg.png" mode=""></image>
				<view class="content">
					<view class="">
						<view class="logo">
							<image src="../../static/card/logo.png" mode=""></image>
						</view>
						<view class="txt">
							姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:张明磊
							<br>
							班&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;级:1901班
							<br>
							联系方式:18888888888
							<br>
							入学年份:2019
							<br>
							No:HURA258998
						</view>
					</view>
					<view class="img">
						<image src="../../static/card/head.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="downbtn" @click="exportCardAsImage">查看与保存卡片</view>
	</view>
</template>

<script>
	import html2canvas from "html2canvas"

	import utlis from '../../common/utils.js'
	export default {
		mixins: [utlis],
		data() {
			return {

			}
		},
		onLoad() {

		},
		onUnload() {

		},
		methods: {
			exportCardAsImage() {
				const cardElement = this.$refs.card.$el;
				console.log(this.$refs.card);
				html2canvas(cardElement).then((canvas) => {
					const imageData = canvas.toDataURL('image/png');
					console.log("图片", imageData);
					this.pic(imageData)
				});
			},
			pic(src) {
				uni.previewImage({
					urls: Array.isArray(src) ? src : [src],
					current: '',
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				});
			},

		}
	}
</script>

<style scoped lang="scss">
	page {
		background: #F9F9F9;
	}

	.index {
		width: 100vw;
		display: flex;
		flex-direction: column;
		// justify-content: center;
		align-items: center;
	}

	.top {
		width: 100vw;

		.bg {
			position: relative;
			z-index: 1;

			image {
				height: 554rpx;
				width: 100vw;
			}
		}
	}

	.main {
		position: relative;
		z-index: 2;
		margin-top: -100rpx;
		width: 716rpx;
		height: 402rpx;
		background: rgba(175, 0, 5, 0.78);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		overflow: hidden;
		// background: url("../../static/card/bg.png") repeat fixed center;

		.card {
			position: relative;
			z-index: 1;
			padding: 30rpx;

			&>image {
				position: absolute;
				z-index: 1;
				top: 0;
				left: 0;
				width: 716rpx;
				height: 402rpx;
			}

			.content {
				position: relative;
				z-index: 2;
				display: flex;
				justify-content: space-between;

				.logo {
					image {
						width: 296rpx;
						height: 72rpx;
					}
				}

				.txt {
					line-height: 50rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					font-weight: 400;
				}

				.img {
					display: flex;
					// justify-content: center;
					align-items: center;
					margin-right: 88rpx;

					image {
						width: 182rpx;
						height: 230rpx;
					}
				}
			}
		}


	}

	.downbtn {

		margin: 0;
		width: 690rpx;
		height: 104rpx;
		background: #AF0005;
		border-radius: 72rpx 72rpx 72rpx 72rpx;
		opacity: 1;
		color: #FFFFFF;
		font-weight: 500;
		line-height: 104rpx;
		text-align: center;
		margin-top: 76rpx;
	}
</style>
相关推荐
全职计算机毕业设计1 小时前
基于 UniApp 平台的学生闲置物品售卖小程序设计与实现
android·uni-app
涔溪1 小时前
uni-app环境搭建
前端·uni-app
遛马1 小时前
uni-app安装插件
uni-app
清云随笔4 小时前
uniapp|微信小程序 实现输入四位数 空格隔开
uni-app
一只欢喜5 小时前
uniapp使用uview2上传图片功能
前端·uni-app
ggome6 小时前
Uniapp低版本的安卓不能用解决办法
前端·javascript·uni-app
贰叁!14 小时前
uniapp输入车牌号组件
uni-app
她似晚风般温柔78916 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
Jiaberrr17 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app
工业互联网专业19 小时前
毕业设计选题:基于springboot+vue+uniapp的驾校报名小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计