Canvas繪製多變形很是簡單,只要懂得Canvas路徑 + 簡單的初中數學知識
便可完成bash
function drawPolygonPath(sideNum, radius, originX, originY, ctx){
ctx.beginPath();
const unitAngle = Math.PI * 2 / sideNum; //計算單元角度
let angle = 0; //初始角度
let xLength, yLength;
// ctx.moveTo(originX, originY);
for(let i = 0; i < sideNum; i++){ //遍歷計算點,並lineTo()繪製路徑
xLength = radius * Math.cos(angle);
yLength = radius * Math.sin(angle);
ctx.lineTo(originX + xLength, originY - yLength);//繪製路徑
angle += unitAngle;
}
ctx.closePath();//閉合路徑,也可在for循環中多一次循環lineTo()至起點
}
複製代碼