官术网_书友最值得收藏!

  • Canvas Cookbook
  • Bhushan Purushottam Joshi
  • 171字
  • 2021-07-16 11:03:15

Adding shadows to objects

Here, we will be using our first recipe. Just a few properties are set and we get a different output as shown:

Adding shadows to objects

Notice the difference between this and the output of the first recipe. Here you will notice a shadow for each line.

How to do it...

We use a few shadow-related properties to build this recipe:

<html>
<head>
  <title>Line Shadow</title>
    <script type="text/javascript">
      var can;
      var ctx;
      function init() {
        can = document.getElementById("MyCanvasArea");
        ctx = can.getContext("2d");
        drawLine(30,30,300,30,20,"orange","butt");  //default cap style
        drawLine(30,80,300,80,20,"crimson","round");
        drawLine(30,130,300,130,20,"teal","square");
      }
      function drawLine(xstart,ystart,xend,yend,width,color,cap)
      {
        ctx.beginPath();
        ctx.strokeStyle=color;
        ctx.lineWidth=width;

        //adding shadow
        ctx.shadowOffsetX = 4;
        ctx.shadowOffsetY = 4;
        ctx.shadowBlur    = 7;
        ctx.shadowColor   = "gray";
        //shadow properties set above
        
        ctx.lineCap=cap;
        ctx.moveTo(xstart,ystart);  
        ctx.lineTo(xend,yend);
        ctx.stroke();
        ctx.closePath();
      }
    </script>
</head>
<body onload="init()">
  <br/><br/>
  <center>
  <canvas id="MyCanvasArea" width="320" height="200"  style="border:3px solid brown;">
  </canvas>
  </center>
</body>
</html>

How it works...

The properties related to the shadow mentioned in the previous recipe are used here. Here the shadow is applied to the line rather than the text. Thus, shadows can be applied to objects.

There's more...

Try the following:

  • Change the shadow color
  • Change the blur value for the shadow
  • Change the shadowOffsetX and shadowOffsetY value
主站蜘蛛池模板: 临猗县| 横峰县| 河源市| 南城县| 云南省| 吴桥县| 无为县| 上林县| 阳春市| 内黄县| 阜新市| 黄骅市| 军事| 庆城县| 贞丰县| 武宣县| 温宿县| 娄底市| 广宁县| 淳安县| 舒兰市| 内江市| 莱州市| 辛集市| 龙州县| 天气| 天门市| 嘉荫县| 武鸣县| 静安区| 京山县| 清水县| 息烽县| 屯门区| 虎林市| 章丘市| 德格县| 万全县| 昌江| 辽宁省| 岳阳市|