【问题标题】:Unable to render external images in html2canvas无法在 html2canvas 中呈现外部图像
【发布时间】:2022-01-25 12:29:38
【问题描述】:

JavaScript 新手,请帮我在 html2canvas 脚本中渲染外部图像。我必须使用useCORS 吗?如果是,在哪里?我也尝试过从 css 和 html 标签渲染图像,但它没有反映。

还有控制台错误:Uncaught (in promise) DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

const elm = document.querySelector(".convert");
html2canvas(elm).then(function(canvas) {
  document.querySelector(".result");
  document.body.appendChild(canvas);

  let cvs = document.querySelector("canvas");
  let a = document.querySelector(".a");
  a.href= cvs.toDataURL();
  a.download="example.png";
});

【问题讨论】:

    标签: javascript html2canvas


    【解决方案1】:

    我也遇到了同样的问题

    html2canvas(this.PrintOrderID.nativeElement, {
      scale: 2,
      width: mywindow!.innerHeight,
      height: mywindow!.innerWidth,
      useCORS: true,
      allowTaint : true
    }).then((canvas) => {
      mywindow!.document.body.appendChild(canvas);
    })
    

    【讨论】:

    • 问题标记为 javascript,但您的答案似乎使用了 Dart。用不同的编程语言提供答案是没有帮助的。
    • @Ouroborus,不一定。它可以为完成等效工作提供逻辑指导。显然,语法、内置函数等可能存在问题,必须加以考虑。
    猜你喜欢
    • 2015-08-03
    • 2014-12-20
    • 1970-01-01
    • 2018-02-09
    • 2015-03-19
    • 2018-02-09
    • 2014-10-11
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多