小程序 canvas生成分享海报

成品效果图:将二维码和图片拼合到一张图上。各位可以扫下面小程序码体验一下。

位置在小程序首页-右上角-生成海报。

我做了图片切换。

 

<!--pages/test/canvas.wxml-->
<block  wx:if="{{urls}} ===../../../images/qrbg.png">
<image src='{{qrcode}}' class='bgs2' hidden='{{!hidden}}'></image>
<image src='{{bg}}' class='bgs' hidden='{{!hidden}}'></image>
</block>
<!-- 画布大小按需定制 这里我按照背景图的尺寸定的  -->
<canvas canvas-id="shareImg" hidden='{{cvsdis}}'  style="width:750px;height:1136px"></canvas>
<!-- 预览区域  -->
<view hidden='{{hidden}}' class='preview'>
  <image src='{{prurl}}' class='bcimg' bindtouchstart='timestart' bindtouchend='timeend' bindtap='saveImg'></image>
  <button type='primary' size='mini' bindtap='save'>保存到相册</button>
</view>
<view class='share'  bindtap='qie'>更换样式</view>
<button class='share2' type='primary' bindtap='share' disabled='{{cvsdis}}'>生成分享图</button>
/* pages/canvas/canvas.wxss */
page{
  background: #ededed;
}
canvas{
  position: fixed;
  top: 0;
  left: 800rpx;
}
.share{
  position: fixed;
  bottom: 0;
  width: 50%;
  left:0;
  height: 100rpx;
  line-height: 100rpx;
  border-radius: 0;
  background: #fff;
  color: #42b035;
  text-align: center;
}
.share2{
  position: fixed;
  bottom: 0;
  width: 50%;
  right: 0;
  height: 100rpx;
  line-height: 100rpx;
  border-radius: 0
}
.preview {
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.9);
  position: absolute;
  z-index: 2;
}
.preview image{
  width: 80%;
  position: absolute;
  top: 10%;
  left: 10%;
  z-index: 3;
  border: 1px dashed #fff;
}
.bcimg{
  position: absolute;
  width: 70%;
  height: 76%;
  top: 0;
}
.preview button{
  width: 100%;
  position: fixed;
  bottom: 0;
  height: 100rpx;
  line-height: 100rpx;
  left: 0;
  border-radius: 0;
  z-index: 999999;
}
.bgs{
  width: 600rpx;
  height: 1050rpx;
  position: fixed;
  top: 0;
  left: 75rpx;
}
.bgs2{
  width: 160rpx;
  height: 160rpx;
  position: fixed;
  top: 860rpx;
  right: 100rpx;
  z-index: 9999;
}
var app = getApp();
Page({
  data: {
    hidden: true,
    numbers:1,
    i: 0,
    img_url:'接口地址',
    cvsdis:true,
  },
  onShow: function (options) {
    var that = this;
    request.request(
      'mp_qrcode', {
        bg: that.data.numbers,
      },
      function (res) {
        if (res.data.err == 0) {
          that.setData({
            bg: that.data.img_url + res.data.data.bg,
            qrcode: that.data.img_url + res.data.data.qrcode,
            numbers: that.data.numbers + 1,
          })
          let promise1 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
              src: that.data.img_url + res.data.data.qrcode,
              success: function (res) {
                resolve(res);
              }
            })
          });
          let promise2 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
              src: that.data.img_url + res.data.data.bg,
              success: function (res) {
                resolve(res);
              }
            })
          });
          Promise.all([
            promise1, promise2
          ]).then(res => {
            console.log('拼合前看1', res)
            const ctx = wx.createCanvasContext('shareImg')
            //主要就是计算好各个图文的位置
            ctx.drawImage(res[0].path, 521, 930, 198, 172)
            ctx.drawImage(res[1].path, 0, 0, 750, 1136)
            ctx.stroke()
            ctx.draw()
            that.setData({
              cvsdis: false
            })
          })
        }
      }
    )
    
  },

  qie: function () {
    var that = this;
    var i = that.data.i
    that.setData({
      cvsdis: true
    })
    request.request(
      'mp_qrcode', {
        bg: that.data.numbers,
      },
      function (res) {
        console.log(res.data);
        if (res.data.err == 0) {
          that.setData({
            bg: that.data.img_url + res.data.data.bg,
            qrcode: that.data.img_url + res.data.data.qrcode,
            numbers: that.data.numbers + 1,
          })
          let promise1 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
              src: that.data.img_url + res.data.data.qrcode,
              success: function (res) {
                resolve(res);
              }
            })
          });
          let promise2 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
              src: that.data.img_url + res.data.data.bg,
              success: function (res) {
                resolve(res);
              }
            })
          });
          Promise.all([
            promise1, promise2
          ]).then(res => {
            console.log('拼合前看2', res)
            const ctx = wx.createCanvasContext('shareImg')
            //主要就是计算好各个图文的位置
            ctx.drawImage(res[0].path, 521, 930, 198, 172)
            ctx.drawImage(res[1].path, 0, 0, 750, 1136)
            ctx.stroke()
            ctx.draw()
            that.setData({
              cvsdis:false
            })
          })
        }
      }
    )
  },
  /**
   * 生成分享图
  */
  share: function () {
    var that = this
    wx.showLoading({
      title: '努力生成中...'
    })
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 740,
      height: 1334,
      destWidth: 740,
      destHeight: 1334,
      canvasId: 'shareImg',
      success: function (res) {
        that.setData({
          prurl: res.tempFilePath,
          hidden: false
        })
        console.log('生活的'+that.data.prurl);
        wx.hideLoading()
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },

  /**
   * 保存到相册
  */
  timestart: function (e) {
    var _this = this;
    _this.setData({ timestart: e.timeStamp });
  },
  //点击结束的时间
  timeend: function (e) {
    var _this = this;
    _this.setData({ timeend: e.timeStamp });
  },

  //保存图片
  saveImg: function (e) {
    var that = this;
    var times = that.data.timeend - that.data.timestart;
    if (times > 300) {
      console.log("长按");
      console.log(that.data.prur)
      wx.saveImageToPhotosAlbum({
        filePath: that.data.prurl,
        success(res) {
          wx.showModal({
            content: '图片已保存到相册,赶紧晒一下吧~',
            showCancel: false,
            confirmText: '好哒',
            confirmColor: '#72B9C3',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定');
                that.setData({
                  hidden: true
                })
              }
            }
          })
        }
      })
    }
  },


  save: function () {
    var that = this
    //生产环境时 记得这里要加入获取相册授权的代码
    console.log(that.data.prur)
    wx.saveImageToPhotosAlbum({
      filePath: that.data.prurl,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册,赶紧晒一下吧~',
          showCancel: false,
          confirmText: '好哒',
          confirmColor: '#72B9C3',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              that.setData({
                hidden: true
              })
            }
          }
        })
      }
    })
  }
})

需要注意的是:

背景图是一张png图。二维码部分为透明!(下图样式)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值