Canvas How to - Fill rectangle








Question

We would like to know how to fill rectangle.

Answer


<!-- ww  w .  j a v a 2 s  .c o  m-->
<!DOCTYPE html>
<html>
<body>
<canvas id='canvas'></canvas>
<script type='text/javascript'>//<![CDATA[ 
var ctx = document.getElementById('canvas').getContext('2d');
    var x = 0, y = 0, side = 10;
    ctx.fillRect(x, y, side, side);
//]]>  
</script>
</body>
</html>

The code above is rendered as follows: