Canvas How to - Set image height and width








Question

We would like to know how to set image height and width.

Answer


<!--from w w w .j  av  a 2s .c  o m-->

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
    var curr_canv = document.getElementById('a').getContext('2d');
    var img = new Image();
    img.width = 525;
    img.height = 400;
    img.src = "http://www.java2s.com/style/download.png";
    img.onload = function() { curr_canv.drawImage(img,0,0); }
});//]]>  
</script>
</head>
<body>
  <canvas id="a" style="width:100%;height:100%;"></canvas>
</body>
</html>

The code above is rendered as follows: