Canvas How to - Add shadow to circle








Question

We would like to know how to add shadow to circle.

Answer


<!--from  w  w w  . j  a v  a2s . c  o  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(function() {
    draw(document.getElementById('canvas').getContext('2d'));
    function draw(ctx) {
        ctx.textBaseline = "top";
        ctx.font = "12pt Arial";
        ctx.fillStyle = "red";
        ctx.strokeStyle = "blue";
        text = setShadow(ctx, "red", 0, 0, 50);
        ctx.arc(60, 60, 30, 0, Math.PI*2, false);
        ctx.fill();
    }
    function setShadow(ctx, color, ox, oy, blur) {
      ctx.shadowColor = color;
      ctx.shadowOffsetX = ox;
      ctx.shadowOffsetY = oy;
      ctx.shadowBlur = blur;
    }
});
});//]]>  
</script>
</head>
<body>
  <canvas id=canvas>
</body>
</html>

The code above is rendered as follows: