3 回答

TA贡献1942条经验 获得超3个赞
要绘制一个近似的圆并获取该圆的点,可以在圆形图案线中绘制一系列线段,以便:
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
/* Center and radius of circle */
const centerX = 50;
const centerY = 50;
const radius = 25;
/* Array containing your points */
const points = [];
/* Configure line rendering and start drawing a path */
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
ctx.beginPath();
/* Iterate each side and calculate the position of it's starting point */
for (let i = 0, sides = 50; i < sides; i ++) {
const angle = (i / sides) * 2 * Math.PI;
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
points.push({ x, y });
ctx.lineTo(x, y);
}
/* Complete path and render as stroke */
ctx.closePath();
ctx.stroke();
console.log(points);
<canvas id="canvas" width="200" height="200"></canvas>
添加回答
举报