IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    img使用canvas转为base64跨域问题

    Ginkgo发表于 2024-03-15 16:06:00
    love 0

    在使用 canvas 将 img 的 src 地址转换为 base64 调用 canvas.toDataURL() 时,遇到跨域问题并报错如下

    Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. at img.onload

    67571-rse1if4af1b.png

    解决方案:

    在 onload 之前添加 crossOrigin 参数

     var img = new Image();
     img.src = src;
     img.crossOrigin = '';
     img.onload = function () {
       // todo
     }

    具体参考大佬方案



沪ICP备19023445号-2号
友情链接