Html5 繪制旋轉的太極圖


采用Html5+JavaScript在Canvas中繪制旋轉的太極圖,如下圖所示:

具體思路和繪制邏輯,在上圖中已有說明,代碼如下:

 1 <script type="text/javascript">
 2 
 3        //只畫邊框線,無填充
 4         function bigCircle(ctx,x, y, r, st, end, w,oc) {
 5             ctx.lineWidth = w;
 6             ctx.beginPath();
 7             ctx.arc(x, y, r, st, end, oc);
 8             ctx.closePath();
 9             ctx.stroke();
10         }
11         //有填充,畫小圓,x,y表示大圓的圓心,r表示大圓的半徑,w表示線寬,oc表示方向,l表示上下,d表示度數
12         function smallCircle(ctx, x, y, r, st, end, w, oc, l,d) {
13             var Angle = d * Math.PI / 180; //偏移角用弧度表示
14             ctx.lineWidth = w; 
15             ctx.beginPath();
16             if (l) {
17                 ctx.fillStyle = "black"; 
18                 ctx.arc(x + (r / 2) * Math.sin(Angle), y - (r / 2) * Math.cos(Angle), r/10, st, end, oc);
19             } else {
20                 ctx.fillStyle = "red";
21                 ctx.arc(x - (r / 2) * Math.sin(Angle), y + (r / 2) * Math.cos(Angle), r/10, st, end, oc);
22             }
23             ctx.closePath();
24             ctx.stroke();
25             ctx.fill();
26         }
27 
28         //此函數是畫帶S形曲線的圓,l表示左右,true表示左,順時針,false表示右,逆時針
29         //d表示度數
30         function halfCircle(ctx, x, y, r, w, l,d) {
31             ctx.lineWidth = w;
32             if (l) {
33                 ctx.fillStyle = "black";
34             } else {
35                 ctx.fillStyle = "red";
36             }
37             ctx.beginPath();
38             var Angle = d * Math.PI / 180;//偏移角用弧度表示
39 
40             ctx.arc(x + (r / 2) * Math.sin(Angle), y - (r / 2) * Math.cos(Angle), r / 2, Math.PI / 2 + Angle, Math.PI * 3 / 2 + Angle, true);
41             ctx.arc(x - (r / 2) * Math.sin(Angle), y + (r / 2) * Math.cos(Angle), r / 2, Math.PI*3 / 2 + Angle, Math.PI / 2 + Angle, true);
42             ctx.arc(x, y, r, Math.PI / 2 + Angle, Math.PI * 3 / 2 + Angle, l); //順時針,逆時針通過參數判斷
43             ctx.closePath();
44             ctx.stroke();
45             ctx.fill();
46         }
47 
48         var num = 0;//表示旋轉的度數
49         function drawTaichi() {
50             var c = document.getElementById("myCanvas");
51             var ctx = c.getContext("2d");
52             var cX = 200;
53             var cY = 200;
54             var radius = 150;
55             ctx.clearRect(0,0,c.width,c.height);
56             //繪制s線 左
57             halfCircle(ctx, cX, cY, radius, 1, true, num);
58             //
59             halfCircle(ctx, cX, cY, radius, 1, false, num);
60             //繪制小圓,上
61             smallCircle(ctx, cX, cY, radius, 0, Math.PI * 2, 1, true, true, num);
62             //繪制小圓,下
63             smallCircle(ctx, cX, cY, radius, 0, Math.PI * 2, 1, true, false, num);
64             //繪制外圓
65             bigCircle(ctx, cX, cY, radius, 0, Math.PI * 2, 2, true);
66             ctx.save();
67             num++;
68             num = num % 360;//只有360°,所以大於360,就重新開始
69         }
70 
71         window.onload = function () {
72             setInterval(drawTaichi, 200);
73         }
74 
75     </script>
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM