Canvas How to - Move shadows offset








Question

We would like to know how to move shadows offset.

Answer


<!DOCTYPE html>
<!--from  ww  w .  j a  va  2 s.c  o m-->
<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {  
        var canvas = $("#myCanvas");
        var context = canvas.get(0).getContext("2d");
        
        // A basic drop shadow
        context.shadowBlur = 20;
        context.shadowColor = "rgb(0, 0, 0)";
        context.fillRect(50, 50, 100, 100);
        
        // Moving the shadow and changing the colour
        context.shadowBlur = 0;
        context.shadowOffsetX = 10;
        context.shadowOffsetY = 10;
        context.shadowColor = "rgba(100, 100, 100, 0.5)"; // Transparent grey
        context.fillRect(200, 50, 100, 100);
        
        // Shadows on other shapes
        context.shadowColor = "rgb(255, 0, 0)"; // Red
        context.shadowBlur = 50;
        context.shadowOffsetX = 0;
        context.shadowOffsetY = 0;
        context.beginPath();
        context.arc(400, 100, 50, 0, Math.PI*2, false);
        context.closePath();
        context.fill();
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
    </canvas>
  </body>
</html>

The code above is rendered as follows: