使用 HTML5 Canvas 可輕鬆繪製五角星。核心是以 beginPath 建立路徑,再以 moveTo 與 lineTo 依序連接十個頂點,最後 closePath 並填色。
<canvas id="stars" width="333" height="300"> Your browser does not support the canvas element. </canvas>
var ctx = document.getElementById('stars').getContext('2d');
ctx.fillStyle = "#827839";
ctx.shadowColor = "#000000";
ctx.shadowOffsetX = 6;
ctx.shadowOffsetY = 6;
ctx.shadowBlur = 9;
ctx.beginPath();
ctx.moveTo(15, 150);
ctx.lineTo(100, 140);
ctx.lineTo(170, 90);
ctx.lineTo(230, 140);
ctx.lineTo(315, 150);
ctx.lineTo(230, 200);
ctx.lineTo(300, 263);
ctx.lineTo(170, 233);
ctx.lineTo(30, 263);
ctx.lineTo(100, 200);
ctx.closePath();
ctx.fill();fillStyle 決定星星顏色,shadow 相關屬性可加入陰影,讓圖形更有層次。若需要線框效果,可改用 stroke 並設定 lineWidth 與 strokeStyle。