Capture HTML Canvas as gif/jpg/png/pdf?

Parand picture Parand · May 29, 2009 · Viewed 620.9k times · Source

Is it possible to capture or print what's displayed in an html canvas as an image or pdf?

I'd like to generate an image via canvas, and be able to generate a png from that image.

Answer

donohoe picture donohoe · Aug 18, 2010

Oops. Original answer was specific to a similar question. This has been revised:

var canvas = document.getElementById("mycanvas");
var img    = canvas.toDataURL("image/png");

with the value in IMG you can write it out as a new Image like so:

document.write('<img src="'+img+'"/>');