如何在React JS中使用html2canvas将react组件转换为图像?

发布于 2025-01-17 03:08:05 字数 1579 浏览 4 评论 0原文

你好,朋友们,我正在使用 React JS 中的图像比较器应用程序...我尝试在比较后下载图像,但不能。

我的应用程序 My App

我的功能

import * as html2canvas from "html2canvas";
import { saveAs as save } from "file-saver";

//Convert component to Canvas
function canvasToBlob(canvas) {
  return new Promise((resolve, reject) => {
    try {
      canvas.toBlob((blob) => {
        if (!blob) {
          return reject(new Error("Could not convert canvas to blob"));
        }
        return resolve(blob);
      });
    } catch (e) {
      return reject(e);
    }
  });
}

//Convert canvas to base64
function blobToBase64(blob) {
  return new Promise((resolve, reject) => {
    try {
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = function () {
        if (typeof reader.result === "string") {
          return resolve(reader.result);
        } else {
          return reject(new Error("Could not convert blob to base64"));
        }
      };
    } catch (e) {
      return reject(e);
    }
  });
}

//Export base64 to image 
export async function exportDiv(querySelector, fileName) {
  const el = document.querySelector(querySelector);
  if (!el) {
    return "";
  }
  const canvas = await html2canvas(el, {
    backgroundColor: null,
  });
  const blob = await canvasToBlob(canvas);
  const base64 = await blobToBase64(blob);
  save(base64, fileName);
}

这是当我尝试将组件导出到图像时出现的错误 这是我尝试将组件导出到图像时遇到的错误

如果有人有解决方案,请不要犹豫。 .. 提前致谢

Hello friends I'm on an image comparator application in React JS... I try to download the image after comparison but I can't.

My App
My App

My functions

import * as html2canvas from "html2canvas";
import { saveAs as save } from "file-saver";

//Convert component to Canvas
function canvasToBlob(canvas) {
  return new Promise((resolve, reject) => {
    try {
      canvas.toBlob((blob) => {
        if (!blob) {
          return reject(new Error("Could not convert canvas to blob"));
        }
        return resolve(blob);
      });
    } catch (e) {
      return reject(e);
    }
  });
}

//Convert canvas to base64
function blobToBase64(blob) {
  return new Promise((resolve, reject) => {
    try {
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = function () {
        if (typeof reader.result === "string") {
          return resolve(reader.result);
        } else {
          return reject(new Error("Could not convert blob to base64"));
        }
      };
    } catch (e) {
      return reject(e);
    }
  });
}

//Export base64 to image 
export async function exportDiv(querySelector, fileName) {
  const el = document.querySelector(querySelector);
  if (!el) {
    return "";
  }
  const canvas = await html2canvas(el, {
    backgroundColor: null,
  });
  const blob = await canvasToBlob(canvas);
  const base64 = await blobToBase64(blob);
  save(base64, fileName);
}

Here is the error I get when I try to export my component to image
Here is the error I get when I try to export my component to image

If anyone has a solution please don't hesitate... Thanks in advance

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

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

发布评论

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

评论(1

单调的奢华 2025-01-24 03:08:05

尝试对 html2canvas 使用不同的导入并确保您使用的是版本 1.4.1:

import html2canvas from "html2canvas";

Try with different import for html2canvas and make sure you are using version 1.4.1:

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