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

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
主站蜘蛛池模板: 大丰市| 改则县| 溧阳市| 遂昌县| 青龙| 云霄县| 祁东县| 旬邑县| 体育| 增城市| 乌兰县| 安丘市| 钦州市| 朝阳区| 临猗县| 启东市| 阜宁县| 青岛市| 井冈山市| 曲阜市| 伊川县| 齐河县| 沂源县| 枞阳县| 南昌县| 长乐市| 桦川县| 尉氏县| 金湖县| 措美县| 方城县| 三江| 石首市| 井陉县| 玉林市| 邮箱| 上林县| 固安县| 正阳县| 海晏县| 明光市|