Canvas How to - Scale image to full size of canvas








Question

We would like to know how to scale image to full size of canvas.

Answer


<!--from   w  w  w  .j  a  v  a  2 s  .  c o  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        var img = new Image();
        img.onload = function () {
            ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
        }
        img.src = "http://www.java2s.com/style/download.png";
}//]]>  
</script>
</head>
<body>
  <div>
    <canvas id="canvas" width=800 height=500></canvas>
  </div>
</body>
</html>

The code above is rendered as follows: