Capture HTML Canvas as gif/jpg/png/pdf?
Solution 1:
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+'"/>');
Solution 2:
HTML5 provides Canvas.toDataURL(mimetype) which is implemented in Opera, Firefox, and Safari 4 beta. There are a number of security restrictions, however (mostly to do with drawing content from another origin onto the canvas).
So you don't need an additional library.
e.g.
<canvas id=canvas width=200 height=200></canvas>
<script>
window.onload = function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.fillStyle = "green";
context.fillRect(50, 50, 100, 100);
// no argument defaults to image/png; image/jpeg, etc also work on some
// implementations -- image/png is the only one that must be supported per spec.
window.location = canvas.toDataURL("image/png");
}
</script>
Theoretically this should create and then navigate to an image with a green square in the middle of it, but I haven't tested.