vue实现合成背景图并点击下载

实现效果图如下:(合并背景和二维码)
在这里插入图片描述
重要插件下载
npm i qrcodejs2 -S
npm i html2canvas -S
在这里插入图片描述

代码如下:

<template>
  <div class="shareBox">
      <van-nav-bar
        title="下载图片"
        left-arrow
        @click-left="goBack"
        :fixed="true"
      >
      <template #left>
        <img src="@/assets/imgs/home/left.png" alt />
      </template>
    </van-nav-bar>
    <div class="sharePage" @click="createPoster">
      <div
        ref="posterHtml"
        id="posterHtml"
        :style="{ backgroundImage: 'url(' + posterHtmlBg + ')' }"
      >
        <!-- 二维码 -->
        <div class="qrcode">
          <div
            id="qrcodeImg"
            ref="qrcodeImg"
            style="width: 140px; height: 140px;"
          ></div>
          
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userApi } from "@/api/public.js";
import QRCode from "qrcodejs2";
import html2canvas from "html2canvas";
export default {
  data() {
    return {
      posterHtmlBg: require("../../assets/imgs/share/share.png"), // 背景图
      posterImg: "", // 最终生成的海报图片
      invite_code: "",
      h5_url: "",
    };
  },
  created() {
    this.initUser();
  },
  methods: {
    initUser() {
      userApi().then(res => {
        console.log(res);
        if (res.code == 200) {
          this.invite_code = res.data.invite_code;
          this.h5_url = res.data.h5_url;
          var url = this.h5_url + "?invite_code=" + this.invite_code;
          this.$nextTick(() => {
            this.createQrcode(url);
          });
        }
      });
    },
    goBack(){
      this.$router.go(-1)
    },
    createQrcode(text) {
      // 生成二维码
      this.$nextTick(() => {
        let qrcodeImgEl = this.$refs.qrcodeImg;
        console.log(this);
        qrcodeImgEl.innerHTML = "";
        let qrcode = new QRCode(qrcodeImgEl, {
          width: 140,
          height: 140,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
        qrcode.makeCode(text);
      });
    },
    createPoster() {
      // 生成海报
      this.$nextTick(() => {
        let vm = this;
        let domObj = vm.$refs.posterHtml;
        console.log(domObj);
        html2canvas(domObj, {
          useCORS: true,
          allowTaint: false,
          logging: false,
          letterRendering: true,
          onclone(doc) {
            console.log("doc", doc);
            let e = doc.querySelector("#posterHtml");
            e.style.display = "block";
          },
        }).then(function (canvas) {
          // 在微信里,可长按保存或转发
          vm.posterImg = canvas.toDataURL("image/png");

          let a = document.createElement("a");

          a.href =  vm.posterImg;

          a.setAttribute("download", "code-download");

          a.click();
          console.log(vm.posterImg);
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
body,
	html,
	#app {
	 	background: #ffffff !important;
	}
	.sharePage ,.shareBox{
		  width: 100%;
		  height: 100%;
	}
	#posterHtml {
		  height: 100%;
		  width: 100%;
		  background-size: cover;
		  background-repeat: no-repeat;
	}
	.qrcode {
		  width: 160px;
		  height: 160px;
		  position: fixed;
		  top: 50%;
		  left: 50%;
		  transform: translate(-50%, -50%);
		  display: flex;
		  align-items: center;
		  justify-content:center;
		  background:#E8F0FF; 
	 
	}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃葡萄不吐葡萄皮嘻嘻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值