Shadow

In this chapter you will learn:

  1. How to draw with shadow

Using Shadows

There are four drawing state properties associated with adding shadows to the shapes and text.

NameDescriptionReturns
shadowBlurSets the degree of blur in the shadownumber
shadowColorSets the color of the shadowstring
shadowOffsetXSets the x-offset for the shadownumber
shadowOffsetYSets the y-offset for the shadownumber

The following code sets shadow color and blur.

<!DOCTYPE HTML><!--   j  a v a 2  s.  co  m-->
<html>
<head>
<style>
canvas {
      border: thin solid black
}

body>* {
      float: left;
}
</style>
</head>
<body>
      <canvas id="canvas" width="500" height="840"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.fillStyle = "lightgrey";
            ctx.strokeStyle = "black";
            ctx.lineWidth = 3;
            ctx.shadowOffsetX = 5;
            ctx.shadowOffsetY = 5;
            ctx.shadowBlur = 5;
            ctx.shadowColor = "red";
            ctx.strokeRect(250, 20, 100, 100);
            ctx.beginPath();
            ctx.arc(420, 70, 50, 0, Math.PI, true);
            ctx.stroke();
            ctx.beginPath();
            ctx.arc(420, 80, 40, 0, Math.PI, false);
            ctx.fill();
            ctx.font = "100px sans-serif";
            ctx.fillText("java2s.com", 10, 100);
            ctx.strokeText("java 2s.com", 10, 100);
      </script>
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to set color to transparency when drawing on canvas
Home » Javascript Tutorial » Canvas
Canvas tag
Canvas Context
Rectangle
Rectangle clear
Drawing State
Drawing state restore
Line width
Line Join Style
Line cap settings
Gradients
Linear Gradient
Radial Gradient
Path
Arcs
Two types of curve
Quadratic Bezier Curves
Cubic Bezier Curves
Clip
Text
Shadow
Transparency
Stoke style