1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>canvas繪製八卦圖</title> 6 </head> 7 8 <body> 9 <canvas id="canvas" width="600" height="500"></canvas> 10 <script> 11 var canvas=document.getElementById("canvas"); 12 var context=canvas.getContext('2d'); 13 14 //1.先繪製一個完整的空心圓 15 context.beginPath(); 16 context.arc(250,250,200,0,Math.PI*2); 17 context.stroke(); 18 19 //2.繪製半黑半白 默認爲黑色 20 context.beginPath(); 21 context.arc(250,250,200,Math.PI*3/2,Math.PI/2,true); 22 context.fill(); 23 24 //3.繪製一黑一白兩個半圓 25 context.fillStyle="white"; 26 context.beginPath(); 27 context.arc(250,150,100,0,Math.PI*2); 28 context.fill(); 29 30 context.fillStyle="black"; 31 context.beginPath(); 32 context.arc(250,350,100,0,Math.PI*2); 33 context.fill(); 34 35 //4.繪製兩個小圓 36 context.fillStyle="black"; 37 context.beginPath(); 38 context.arc(250,150,30,0,Math.PI*2); 39 context.fill(); 40 41 context.fillStyle="white"; 42 context.beginPath(); 43 context.arc(250,350,30,0,Math.PI*2); 44 context.fill(); 45 </script> 46 </body> 47 </html>
效果圖:html