書名: 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:

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
andshadowOffsetY
value
推薦閱讀
- 玩轉Scratch少兒趣味編程
- Monkey Game Development:Beginner's Guide
- C#程序設計實訓指導書
- Learning Bayesian Models with R
- TypeScript實戰指南
- The Complete Coding Interview Guide in Java
- 用戶體驗增長:數字化·智能化·綠色化
- Java:High-Performance Apps with Java 9
- Visual Basic程序設計習題與上機實踐
- C語言程序設計簡明教程:Qt實戰
- Java圖像處理:基于OpenCV與JVM
- Kotlin Programming By Example
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Android高級開發實戰:UI、NDK與安全
- HTML5 WebSocket權威指南