The shadowColor
property sets or gets the color used for shadows.
To create a shadowEffect, there are four properties of the Canvas context that need to be manipulated:
To create a red shadow that is 5 pixels to the right and 5 pixels down from your text, with a blur of 2 pixels, you would set the properties like this:
context.shadowColor = "#FF0000";
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
context.shadowBlur = 2;
shadowColor |
Yes | Yes | Yes | Yes | Yes |
context.shadowColor=color;
Value | Description |
---|---|
color | The CSS color value for shadows. Default value is #000000 |
The following code uses two different shadow colors.
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width='500px' height='200px'/>
<script type='text/javascript'>
var ctx = document.getElementById("canvas").getContext("2d");
ctx.shadowBlur = 25;<!-- www . ja va2s . c o m-->
ctx.fillStyle = "yellow";
ctx.shadowColor = "black";
ctx.fillRect(20, 20, 110, 80);
ctx.shadowColor = "blue";
ctx.fillRect(140, 20, 110, 80);
</script>
</body>
</html>
The following code adds shadows to a text.
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width='500px' height='200px'/>
<script type='text/javascript'>
var ctx = document.getElementById("canvas").getContext("2d");
<!--from w ww. j a va2 s. com-->
ctx.font = "bold 20px sans-serif";
ctx.shadowColor = "rgb(190, 190, 190)";
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 5;
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.fillText("java2s.com...", 5, 20);
</script>
</body>
</html>
The code above is rendered as follows: