We would like to know how to output data URL for png image from canvas.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from w w w .j a v a 2s . c o m-->
var canvas = document.getElementById('canvas');
canvas.width = 200;
canvas.height = 150;
var context = canvas.getContext('2d');
context.rect(20,20,50,50);
context.stroke();
var image = new Image();
image.onload = function(){
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 0);
}
image.src = canvas.toDataURL("image/png");
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
The code above is rendered as follows: