Canvas How to - Draw shape on top of another shape








Question

We would like to know how to draw shape on top of another shape.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){<!--from  www . j  a  v  a2 s  . c  o  m-->
    var my_canvas = document.getElementById('canvas'),
    context = my_canvas.getContext("2d");
    context.moveTo(20, 20);
    context.lineTo(100, 20);
    context.fillStyle = "#999";
    context.beginPath();
    context.arc(100,100,75,0,2*Math.PI);
    context.fill();
    context.fillStyle = "orange";
    context.fillRect(20,20,50,50);
    context.font = "24px Helvetica";
    context.fillStyle = "#000";
    context.fillText("Canvas", 50, 130);
}//]]>  
</script>
</head>
<body>
  <canvas id="canvas" width="200" height="200"></canvas>
</body>
</html>

The code above is rendered as follows: