Javascript 将 div 捕获为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13720815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:44:09  来源:igfitidea点击:

Capture a div as an image

javascripthtmljavascript-eventsimage-capture

提问by MarGa

I have some div tags containing charts rendered by javascript. I want to export them in an image format by the press of a button. I don't know if it's possible to capture a div tag as an image using javascript ! so there i'm asking for tips and hints. Thanks in advance.

我有一些包含由 javascript 呈现的图表的 div 标签。我想通过按下按钮以图像格式导出它们。我不知道是否可以使用 javascript 将 div 标签捕获为图像!所以我在寻求提示和提示。提前致谢。

回答by Wutz

You can create an image using the canvas element. If your charts are rendered using HTML elements you need to draw that HTML onto the canvas. Then you can use toDataURL on the canvas to get the B64-encoded image data.

您可以使用 canvas 元素创建图像。如果您的图表是使用 HTML 元素呈现的,您需要将该 HTML 绘制到画布上。然后就可以在画布上使用 toDataURL 来获取 B64 编码的图像数据。

回答by Sugar

  1. JS can't access to displayed in case he can't parse HTML
  2. You can call some utility to make screenshot of area. But it's not safe to use, so it will work if you give access.
  3. This question was already asked here. Probably it has some good things you need
  1. JS无法访问显示以防他无法解析HTML
  2. 您可以调用一些实用程序来制作区域截图。但是使用起来并不安全,因此如果您授予访问权限,它将起作用。
  3. 这个问题已经在这里过了。可能它有一些你需要的好东西

回答by Cerbrus

This is not possible using only JavaScript.

仅使用 JavaScript 是不可能的。

JavaScript has no access on a PC to save files, nor can it convert the DOM to a image format.

JavaScript 无法在 PC 上保存文件,也无法将 DOM 转换为图像格式。

回答by Piotr Stapp

回答by Fr0g

u might try phantomjs, http://phantomjs.org/, although I have'nt used it personally, but it is widely used for screen captures

你可以试试phantomjs,http://phantomjs.org/,虽然我个人没有用过,但它被广泛用于截屏