zoukankan      html  css  js  c++  java
  • Tainted canvases may not be exported的问题解决

    项目里使用到用canvas生成海报,在toDataURL报了这个错误Tainted canvases may not be exported。

    原因就在于使用了跨域的图片,所以说是被污染的画布。
    解决方案如下
    1】为image请求添加跨域

    var image = new Image()
    image.setAttribute("crossOrigin",'anonymous')
    image.src = src
    

    但也许有可能服务器不让你跨域请求图片(也不知道为啥),那么用到方案2
    2】通过把请求的图片转化成base64再进行使用
    代码如下

    function getURLBase64(url) {
      return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest()
        xhr.open('get', url, true)
        xhr.responseType = 'blob'
        xhr.onload = function() {
          if (this.status === 200) {
            var blob = this.response
            var fileReader = new FileReader()
            fileReader.onloadend = function(e) {
              var result = e.target.result
              resolve(result)
            }
            fileReader.readAsDataURL(blob)
          }
        }
        xhr.onerror = function() {
          reject()
        }
        xhr.send()
      })
    }
  • 相关阅读:
    web页面中四种常见必测控件
    python03
    python基础2
    python基础
    如何定位测试用例的作用?
    需求测试的注意事项有哪些?
    性能测试的流程?
    简述bug的生命周期?
    Python字符串
    Python基础语法
  • 原文地址:https://www.cnblogs.com/iroading/p/11011268.html
Copyright © 2011-2022 走看看