关于微信小程序 canvas 2d 的问题,下面这段码,苹果系统可,安卓不行,谁知道问题所在?
发布于 4 年前 作者 chengping 1915 次浏览 来自 问答

onClickConfirm() {

    const {

      dpr,

      imageSrc,

      x,

      y,

      initialScale,

      scale,

      clipWidth,

      clipHeight,

    } = this.data


    const totalScale = scale * initialScale

    const sx = -x / totalScale

    const sy = -y / totalScale

    const canvasWidth = clipWidth / totalScale

    const canvasHeight = clipHeight / totalScale


    this.setData({ canvasWidth, canvasHeight })


    const query = wx.createSelectorQuery()

    query

      .select('.canvas')

      .fields({ node: true, size: true })

      .exec((res) => {

        const canvas = res[0].node

        const context = canvas.getContext('2d')


        canvas.width = res[0].width * dpr

        canvas.height = res[0].height * dpr

        context.scale(dpr, dpr)


        const canvasImg = canvas.createImage()

        canvasImg.src = imageSrc

        canvasImg.onload = () => {

          context.drawImage(

            canvasImg,

            sx,

            sy,

            canvasWidth,

            canvasHeight,

            0,

            0,

            canvasWidth,

            canvasHeight

          )

       

          wx.canvasToTempFilePath({

            fileType: 'jpg',

            canvas,

            success: (res) => {

              // TODO: 这里在实际项目中应该是调用 wx.uploadFile 来上传采集后图片

              const app = getApp()

              app.globalData.avatar = res.tempFilePath


              var pages = getCurrentPages(); // 获取页面栈

              var currPage = pages[pages.length - 1]; // 当前页面

              var prevPage = pages[pages.length - 2]; // 上一个页面

              var index3=this.data.index3;

            prevPage.data.fielddata[index3].img_arr=prevPage.data.fielddata[index3].img_arr.concat(res.tempFilePath)

               prevPage.setData({

                fielddata:prevPage.data.fielddata// 假数据

               })


              wx.navigateBack()

            },

          })}

       // })

      })

  },

1 回复

为的是实现简单的打开图片,然后裁剪,返回

回到顶部