Html5 繪製旋轉的太極圖

來源:互聯網
上載者:User

標籤:interval   ide   http   asc   順時針   fill   tin   src   技術   

採用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

 

Html5 繪製旋轉的太極圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.