We would like to know how to draw image from img element on Canvas.
<!--from w w w . j a v a 2 s .c om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
imgs = document.getElementsByTagName('img')[0];
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var context = canvas.getContext('2d');
canvas.width = imgs.width / 2;
canvas.height = imgs.height / 2;
var testImage = new Image();
testImage.src = imgs.src;
testImage.onload = function() {
square = 100;
context.drawImage(this,0,0,this.width, this.height,
0,0,canvas.width,canvas.height);
}
}//]]>
</script>
</head>
<body>
<img src="http://www.java2s.com/style/download.png" width="200">
</body>
</html>
The code above is rendered as follows: