canvas截取图片

canvasDrawImage (i) {
      const itemCanvas = this.canvasArr[i]  //canvas对象
      const itemWitdh = itemCanvas.width
      const itemHeight = itemCanvas.height
      const itemBase64 = itemCanvas.toDataURL('image/png')
      const context = itemCanvas.getContext('2d')
      const img = new Image()
      img.src = itemBase64
      img.crossOrigin = "anonymous";
      img.onload = () => {
        context.drawImage(img, 0, 0, itemWitdh, itemHeight)
        const clipCanvas = document.createElement('canvas')
        clipCanvas.width = itemWitdh
        clipCanvas.height = itemHeight - (150 + 50)
        const h = itemHeight - 200
        const data = context.getImageData(0, 150, itemWitdh, clipCanvas.height)
        const clipContext = clipCanvas.getContext('2d')
        clipContext.putImageData(data, 0, 0)
        this.base64 = clipCanvas.toDataURL('image/png', 1)
      }

  

posted @ 2020-12-28 13:06  Ricardo_front  阅读(1285)  评论(0编辑  收藏  举报