小程序canvan画布,现两张图片合成一张,并保存到本地

微信小程序合成照片 应用canvas把他们绘制到一张图上保存到本地相册然后点击分享自定义一个组件components/canvas.js里// components/canvas/canvas.jsconst app = getApp()Component({ /** * 组件的属性列表 */ properties: { scene: { type: String, value: '' } }, /** * 组件的初始数
摘要由CSDN通过智能技术生成

微信小程序合成照片 应用canvas把他们绘制到一张图上保存到本地相册然后点击分享

自定义一个组件

components/canvas.js里

// components/canvas/canvas.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    scene: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据 
   */
  data: {
    pixelRatio: 2,
    programCode: '',
    count: 0,
    spinning: false,
    loadingFlag: false, // 是否重新加载图片
    image:''
  },
  // 在组件完全初始化完毕、进入页面节点树后
  attached () {
    wx.nextTick(() => {
      this.getFile() // 获取小程序码和头像的临时文件
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getFile () {
      // 请求接口 获取头像和小程序码的临时文件
      if (!this.data.loadingFlag) {
        this.initData()
      }
    },
    writeCanvas () {
      let that = this
      const ctx = wx.createCanvasContext('myCanvas', that)
      let canvasW = that.data.canvasW
      let canvasH = that.data.canvasH
      let bgImgPath = that.data.image //背景图片(在这里换图片)
      let programCode = that.data.erweima // 小程序码(在这里换图片)
      // 画大背景 单位是 px 不是 rpx
      ctx.drawImage(bgImgPath, 0, 0, canvasW, canvasH)
      // 保存上下文
      ctx.save()
      // 恢复画布
      ctx.restore()
      // 画小程序码
      ctx.drawImage(programCode, that.computedPercent(85), that.computedPercent(165), that.computedPercent(117), that.computedPercent(117))
      ctx.draw(true, () => {
        that.setData({
          spinning: false
        })
      })
    },
    // 初始化数据
    initData () {
      let that =
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值