Canvas How to - Get canvas width, height








Question

We would like to know how to get canvas width, height.

Answer


<!--from w ww.  ja  v a2  s . c  om-->

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
    var c = document.getElementById('test');
    alert('w: ' + c.width + ' h: ' + c.height);
}//]]>  
</script>
</head>
<body>
  <canvas id="test"></test>
</body>
</html>

The code above is rendered as follows: