HTML5 Game - Create drop shadow effect

Introduction

If you change the shadowBlur, shadowOffsetX, and shadowOffsetY properties youll be able to create a different drop shadow effect:

Demo

ResultView the demo in separate window

<!DOCTYPE html>

<html>
  <head>
    <title>Pushing canvas further</title>
    <meta charset="utf-8">
    //from  www  .j  a  v a  2 s.  co m
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    
    <script type="text/javascript">
      $(document).ready(function() {  
        let canvas = $("#myCanvas");
        let context = canvas.get(0).getContext("2d");
        
        // 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);
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
      <!-- Insert fallback content here -->
    </canvas>
  </body>
</html>

The code above sets the blur to 0 and created a razor-sharp shadow.

Related Topic