Capture a div as an image [duplicate]

2019-01-28 23:01发布

This question already has an answer here:

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.

5条回答
爷、活的狠高调
2楼-- · 2019-01-28 23:09

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.

查看更多
The star\"
3楼-- · 2019-01-28 23:09
老娘就宠你
4楼-- · 2019-01-28 23:10

This is not possible using only JavaScript.

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

查看更多
Viruses.
5楼-- · 2019-01-28 23:20
  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
查看更多
Rolldiameter
6楼-- · 2019-01-28 23:22

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

查看更多
登录 后发表回答