canvas 繪製立體圓環
分享一個程式碼例項,它實現了繪製立體圓環的效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> canvas { border:2px dotted #ccc; } </style> </head> <body> <canvas id="canvas" width="550" height="450"></canvas> <script type="text/javascript"> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); function drawTwoArcs() { context.beginPath(); context.arc(300, 190, 150, 0, Math.PI * 2, false); context.arc(300, 190, 100, 0, Math.PI * 2, true); context.fill(); } function draw() { context.shadowColor = "rgba(0,0,0,0.8)"; context.shadowOffsetX = 12; context.shadowOffsetY = 12; context.shadowBlur = 15; drawTwoArcs(); } context.fillStyle = "rgba(100,140,230,0.5)"; draw(); </script> </body> </html>
如果單獨繪製任何一個圓環並填充,那麼效果都是完全填充。
但是為什麼這裡能夠實現圓環效果,下面做一下介紹。
fill()方法是有引數的,如果不填寫,那麼預設值是"nonzero",這是一個填充規則。
關於此規則具體參閱canvas非零繞組規則與奇偶規則一章節。
明白這個規則,那麼再利用陰影就可以實現要求了。
關於陰影的使用參閱canvas 陰影設定一章節。
相關文章
- canvas立體效果的圓環Canvas
- canvas繪製圓環效果程式碼例項Canvas
- CSS 繪製圓環CSS
- canvas繪製滑鼠懸浮可以變大的立體小球Canvas
- canvas 繪製矩形Canvas
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- Anroid自定義View-繪製圓環View
- Canvas 繪製 3d 圓柱體Canvas3D
- canvas 繪製線條Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- Canvas 繪製雷達圖Canvas
- canvas繪製動畫的技巧Canvas動畫
- canvas 繪製文字詳解Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製風車效果Canvas
- canvas繪製笑臉表情Canvas
- canvas繪製太陽系Canvas
- canvas繪製矩形框Canvas
- Canvas 繪製風向動畫Canvas動畫
- canvas 繪製圖案是重疊繪製而不是重置Canvas
- canvas繪製圖案是重疊繪製而不是重置Canvas
- Canvas(3)---繪製餅狀圖Canvas
- canvas系列教程之繪製矩形Canvas
- 用canvas繪製流星夜空Canvas
- 使用canvas繪製圓弧動畫Canvas動畫
- canvas繪製不重合的圓Canvas
- canvas繪製sin正弦曲線Canvas
- 流水賬系列之Canvas繪製-02Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas lineWidth繪製原理Canvas
- canvas lineWidth 繪製原理Canvas
- canvas繪製卡通人臉形象效果Canvas
- canvas繪製圖片drawImage學習Canvas