Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use HTML5 Canvas to capture part of a web page?

I know that you can capture images from an HTML5 video stream using canvas and display them on the page. What I am interested in is can you use the canvas object to create an overlay on top of a web page and then capture a PNG snapshot of that page or part of it.

I would like to enhance our website reviewing tools by adding screen capture (within the browser page) which could then be submitted to a remote server.

YouTrack does this with a Java applet but is it possible with modern HTML5 techniques?

Any other suggested solutions to this problem would also be appreciated.

Thanks

like image 270
Richard Avatar asked Jun 24 '11 14:06

Richard


People also ask

What can you do with HTML5 Canvas?

HTML5 element <canvas> gives you an easy and powerful way to draw graphics using JavaScript. It can be used to draw graphs, make photo compositions or do simple (and not so simple) animations.

Which HTML element is used to draw the graphics on a Web page?

<canvas> is an HTML element which can be used to draw graphics via scripting (usually JavaScript).

Is canvas part of HTML5?

The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images. It is a low level, procedural model that updates a bitmap. HTML5 Canvas also helps in making 2D games.

How do I capture a canvas image?

We can capture the canvas by using the toDataURL method that comes with the canvas element object. Then we can write the following JavaScript to draw some content and capture it as an image file: const canvas = document.


2 Answers

You can "emulate" the screen view by reading the DOM and creating a canvas image with javascript from it. Have a look at http://hertzen.com/experiments/jsfeedback/ and the underlying html2canvas script to get an idea how you can do it with purely Javascript (no plugins or server interaction necessary).

like image 126
Niklas Avatar answered Oct 02 '22 11:10

Niklas


This gets asked a lot. The short answer is that it can't be done for security reasons.

The longer answers include mention of the drawWindow function that only FireFox has, which only works locally (again, for security reasons). In the future it may work once the user gives permission, like in the case of the Java applets of today. In the future, it might even be part of the spec and not a one-off thing done by Mozilla.

If you're feeling crazy, you could attempt to make an entire HTML renderer, take the DOM tree for the page, and attempt to render it in Canvas. This is a fool's errand.

like image 34
Simon Sarris Avatar answered Oct 02 '22 11:10

Simon Sarris