Canvas How to - Set canvas width and height








Question

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

Answer


<!--from  w  w  w .j  a va  2s  .com-->


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
    canvas = $('canvas');
    context = canvas[0].getContext('2d');
    canvas.width(200);
    canvas.height(200);
    context.fillRect(50,50,50,50);
});//]]>  
</script>
</head>
<body>
  <canvas></canvas>
</body>
</html>

The code above is rendered as follows: