vue使用canvas完成二维码与图片的合成与下载(两张图片同理)

1 篇文章 0 订阅
1 篇文章 0 订阅
<el-dialog :title="title" :visible.sync="shopCodeShow" :append-to-body="true" width="20%" height="480">
  <div class="bg-purple-light-down limit-button">
    <el-button type="primary" plain size="small" @click="downLoadShopPic">下载</el-button>
  </div>
  <div style="display: flex;justify-content: center; margin-top:10px;">
    <img ref="shopPicImg" :src="shopPic"  style="display: none;" alt="店铺背景">
    <vue-qr :callback="f_shopPicCode"  :text="payPic" :logoSrc="paylogoSrc" :size="150" :logoScale="0.3" :margin=0 style="display: none; "></vue-qr>
    <canvas id="myCanvasShopPic" ref="myCanvas" width="300" height="450" style="border:1px #eeeeee solid;"></canvas>
  </div>
</el-dialog>
//画布画店铺码
toShopCode (rowData) {
  this.shopNameBgImg = "";
  this.title = '店铺码';
  this.shopCodeShow = true;
  this.unable = false;
  this.shopPicCode = rowData.CommonQRcode;
  this.paylogoSrc = require('../../../assets/aggLogo.png');
  this.shopNameBgImg = rowData.Name;

  this.$nextTick(function () {//等待加载完成
    // 获取Canvas 画图
    let myCanvas = this.$refs.myCanvas
    var ctx = myCanvas.getContext('2d');
    // 在Canvas画布 添加图片
    var img = this.$refs.shopPicImg;
    img.setAttribute("crossorigin","anonymous");
    img.setAttribute("src","url"+'?time=' + new Date().valueOf())
    img.onload = () => {
      ctx.drawImage(img, 0, 0, 300, 450);
    }
  })
},

//店铺二维码回调
f_shopPicCode(dataUrl,id){
  var shopNameBgImg = this.shopNameBgImg
  this.shopPicCodeUrl = dataUrl;
  let myCanvas = this.$refs.myCanvas
  var ctx = myCanvas.getContext('2d')
  ctx.clearRect(0,0,300,450);
  // 在Canvas画布 添加图片
  // var shopPicCodeImg = this.$refs.shopPicCodeImg;
  var shopPicCodeImg = new Image();
  shopPicCodeImg.src = dataUrl;
  // shopPicCodeImg.setAttribute("src",dataUrl)
  shopPicCodeImg.onload = () => {
    ctx.drawImage(shopPicCodeImg, 65, 120, 170, 170);
    ctx.globalCompositeOperation = "destination-over";//层级关系,二维码放在上层
    ctx.font = "14px bold 黑体";
    ctx.fillStyle = "#ffffff";
    ctx.textAlign = "center";
    // 设置垂直对齐方式
    ctx.textBaseline = "middle";
    // 绘制文字
    ctx.fillText(shopNameBgImg, 140, 90);
  }
},
//下载店铺码
downLoadShopPic(){
  var myCanvasShopPic  = document.getElementById("myCanvasShopPic");
  var dataURL = myCanvasShopPic.toDataURL("image/png");
  var saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
  saveLink.href = dataURL;
  saveLink.download = 'downLoad.png';
  saveLink.click();
},

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Silenchen11

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

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

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

打赏作者

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

抵扣说明:

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

余额充值