Canvas How to - Draw shadowed circle








Question

We would like to know how to draw shadowed circle.

Answer


<!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(){<!--from   ww w .  j  a  v  a2 s . c  o  m-->
$(function() {
    draw(document.getElementById('canvas').getContext('2d'));
    function draw(ctx) {
       var radgrad = ctx.createRadialGradient(60,60,0,60,60,60);
       radgrad.addColorStop(0, 'rgba(255,0,0,1)');
       radgrad.addColorStop(0.8, 'rgba(228,0,0,.9)');
       radgrad.addColorStop(1, 'rgba(228,0,0,0)');
       // draw shape
       ctx.fillStyle = radgrad;
       ctx.fillRect(0,0,150,150);
    }
    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" height="300" width="300">
</body>
</html>

The code above is rendered as follows: