javascript - 未捕获类型错误 : Failed to execute 'drawImage' on Vue. js

标签 javascript image vue.js canvas base64

我有一个 vue.js 应用程序,它可以拍摄照片并将其发送到照片过滤器页面。在此过滤器页面中,我有一个按钮可以发布过滤后的照片。我可以过滤拍摄的照片,但无法发布。我的错误在这里;

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'

我的代码在这里;

async sendPhoto () {
      const canvas = document.createElement('canvas')
      canvas.width = document
        .getElementById('image')
        .getAttribute('data-original-width')
      canvas.height = document
        .getElementById('image')
        .getAttribute('data-original-height')
      const ctx = canvas.getContext('2d')
      ctx.filter = this.filters.filter

      var img = document.createElement('img')
      img.src = this.image
      img.onload = function () {
        ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height)
        const anchor = document.createElement('a')
        anchor.href = canvas.toDataURL()
        anchor.sendPhoto = this.img.src
        anchor.click()
      }
      console.log('canvas.toDataURL()', canvas.toDataURL())
      if (canvas.toDataURL() !== null && canvas.toDataURL() !== undefined) {
        const resBody = await this.$phoneAPI.imgYukle(canvas.toDataURL()) // burda filterli fotoyu tekrardan uploadlamak gerekiyo
        console.log('resBody', JSON.stringify(resBody))
        // await this.instagrampostStory({ message: canvas.toDataURL() })
        this.message = ''
      }
    },
    defaultImage () {
      return DefaultImage
    }
  }
}

感谢您的帮助!

最佳答案

根据drawImage documentation ,第一个参数采用 HTMLImageElement

所以,而不是

img.onload = function () {
  ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height) <-- change this line
  const anchor = document.createElement('a')
  anchor.href = canvas.toDataURL()
  anchor.sendPhoto = this.img.src
  anchor.click()
}

应该是

img.onload = function () {
  ctx.drawImage(img, 0, 0, canvas.width, canvas.height)  <-- to this line
  const anchor = document.createElement('a')
  anchor.href = canvas.toDataURL()
  anchor.sendPhoto = this.img.src
  anchor.click()
}

关于javascript - 未捕获类型错误 : Failed to execute 'drawImage' on Vue. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566581/

相关文章:

javascript - Vee-validate 仅验证某些字段

webpack - 使用 Webpack 2 延迟加载 Vue 组件

javascript - jQuery:我的下拉菜单的 getPos

javascript - XMP 对象需要 setProperty 语法

c++ - 使用不同比例因子缩放单个图像

actionscript-3 - 在 AS3 (Action Script 3.0) 中查找(加载)图像大小

javascript - 使用 v-on :change to pass specific object element to a function using Vue and Vuetify

javascript - 如何在html中设置视频源?

javascript - 正则表达式包含括号内的文本

iOS Cordova 将本地文件设置为 img src