使用 html2canvas 制作图片

一. 引入 html2canvas

npm i html2canvas

二. 在页面中引入

import html2canvas from "html2canvas";

三. 在页面中使用

1. HTML

<div class="qr_code" v-if="img">
	<img :src="img" />
</div>
<div id="qianduanwz" v-if="!img">
	 <img class="bgi" src="../../assets/images/save.png" />
	 <img class="er_code" :src="serviceUrl" />
</div>

2. JS

create(){
	this.serviceUrl = localStorage.getItem("serviceUrl");
	this.$nextTick(() => {
      new html2canvas(document.getElementById("qianduanwz"), {
        allowTaint: true,
        useCORS: true,
      }).then((canvas) => {
        this.img = canvas.toDataURL(); // 导出图片
      });
    });
}
data(){
	return {
		serviceUrl :"",  // 二维码图片
		img:"",  //  生成的图片
	}
}

3. CSS

<style lang="less" scoped>
.qr_code {
  width: 2.25rem;
  height: 2.25rem;
  margin: 0.2rem auto 0;
  img {
    display: block;
    width: 100%;
    height: 100%;
  }
}
#qianduanwz {
  width: 2.25rem;
  height: 2.25rem;
  position: relative;
  margin: 0.2rem auto 0;
  .bgi {
    display: block;
    width: 100%;
    height: 100%;
  }
  .er_code {
    width: 1.2rem;
    height: 1.2rem;
    position: absolute;
    top: 0.55rem;
    left: 0.51rem;
  }
}
</style>

四. 效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值