收藏
回答

如何将一个离屏 canvas 绘制到canvas上?

这样做会报错,Failed to execute 'drawImage' on 'CanvasRenderingContext2D'

求解答

回答关注问题邀请回答
收藏

2 个回答

  • 本末
    本末
    2023-03-29
    Page({
      onReady: function () {
        wx.createSelectorQuery().select("#myCanvas").fields({
          node: true,
          size: true
        }).exec((res) => {
          const canvas = res[0].node;
          this.canvas = canvas
          this.ctx = canvas.getContext("2d")
        });
        this.drawCamera()
      },
      drawCamera: function () {
        const camera = wx.createCameraContext()
        this.count = 0
        const cameraListener = camera.onCameraFrame((frame) => {
          if (this.count < 10) console.log(frame)
          this.count++
          this.canvas.width = frame.width
          this.canvas.height = frame.height
          let data = new Uint8Array(frame.data);
          let clamped = new Uint8ClampedArray(data);
          let imageData = this.canvas.createImageData(clamped, frame.width, frame.height);
          this.ctx.putImageData(imageData, 0, 0, 0, 0, frame.width, frame.height);
        })
        cameraListener.start()
      }
    })
    
    2023-03-29
    有用
    回复
  • 彭世瑜
    彭世瑜
    2021-12-24

    怎么没人回答?

    2021-12-24
    有用
    回复
登录 后发表内容