如何解决HTML2Canvas Image Cross Origin问题?

发布于 2025-01-28 21:52:11 字数 1800 浏览 2 评论 0原文

我正在尝试将DOM元素转换为PNG图像。一切正常,除了交叉原点图像,这些图像在图像中显示为空空间。我尝试添加额外的用户道具:是的,但这无效。

这是代码:

 const exportAsPicture = () => {
    var html = document.getElementsByTagName("HTML")[0];
    var body = document.getElementsByTagName("BODY")[0];
    var htmlWidth = html.clientWidth;
    var bodyWidth = body.clientWidth;
    var data = document.getElementById(`${idea.slug}`);
    var newWidth = data.scrollWidth - data.clientWidth;
    if (newWidth > data.clientWidth) {
      htmlWidth += newWidth;
      bodyWidth += newWidth;
    }
    html.style.width = htmlWidth + "px";
    body.style.width = bodyWidth + "px";
    data.style.boxShadow = "none";
    console.log(data);
    html2canvas(data, {
      logging: true,
      letterRendering: 1,
      proxy: "https://notepd.s3.amazonaws.com/",
      useCORS: true,
    })
      .then((canvas) => {
        var image = canvas.toDataURL("image/png", 0.1);
        data.style.boxShadow = "0px 4px 4px rgba(0, 0, 0, 0.08)";

        return image;
      })
      .then((image) => {
        saveAs(image, `NotePD | ${idea.title}.png`);
        html.style.width = null;
        body.style.width = null;
      });
  };

  const saveAs = (blob, fileName) => {
    var elem = window.document.createElement("a");
    elem.href = blob;
    elem.download = fileName;
    elem.style = "display:none;";
    (document.body || document.documentElement).appendChild(elem);
    if (typeof elem.click === "function") {
      elem.click();
    } else {
      elem.target = "_blank";
      elem.dispatchEvent(
        new MouseEvent("click", {
          view: window,
          bubbles: true,
          cancelable: true,
        })
      );
    }
    URL.revokeObjectURL(elem.href);
    elem.remove();
  };

图片来自S3Bucket,相同的原点图像呈现好

I'm attempting to convert a DOM element to a PNG image. Everything works OK except for the cross origin images, which appear as empty spaces in the image. I tried adding extra props of useCors: true, but that doesn't work.

Here's the code:

 const exportAsPicture = () => {
    var html = document.getElementsByTagName("HTML")[0];
    var body = document.getElementsByTagName("BODY")[0];
    var htmlWidth = html.clientWidth;
    var bodyWidth = body.clientWidth;
    var data = document.getElementById(`${idea.slug}`);
    var newWidth = data.scrollWidth - data.clientWidth;
    if (newWidth > data.clientWidth) {
      htmlWidth += newWidth;
      bodyWidth += newWidth;
    }
    html.style.width = htmlWidth + "px";
    body.style.width = bodyWidth + "px";
    data.style.boxShadow = "none";
    console.log(data);
    html2canvas(data, {
      logging: true,
      letterRendering: 1,
      proxy: "https://notepd.s3.amazonaws.com/",
      useCORS: true,
    })
      .then((canvas) => {
        var image = canvas.toDataURL("image/png", 0.1);
        data.style.boxShadow = "0px 4px 4px rgba(0, 0, 0, 0.08)";

        return image;
      })
      .then((image) => {
        saveAs(image, `NotePD | ${idea.title}.png`);
        html.style.width = null;
        body.style.width = null;
      });
  };

  const saveAs = (blob, fileName) => {
    var elem = window.document.createElement("a");
    elem.href = blob;
    elem.download = fileName;
    elem.style = "display:none;";
    (document.body || document.documentElement).appendChild(elem);
    if (typeof elem.click === "function") {
      elem.click();
    } else {
      elem.target = "_blank";
      elem.dispatchEvent(
        new MouseEvent("click", {
          view: window,
          bubbles: true,
          cancelable: true,
        })
      );
    }
    URL.revokeObjectURL(elem.href);
    elem.remove();
  };

The picture is coming from the s3Bucket the same origin images renders well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2025-02-04 21:52:11

只需在图像中添加crossorigin =“匿名”它将解决问题

Just add crossOrigin="anonymous" to your image it will solve the issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文