是否可以不使用.arc()或任何其他HTML标记制作一个圆圈?

我正在尝试使用HTML画布,当我使用.arc()画一个圆时,我不知道如何获取所有要点。有人知道吗?


绝地无双
浏览 129回答 3
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 ++) {&nbsp;&nbsp;&nbsp; const angle = (i / sides) * 2 * Math.PI;&nbsp; const x = centerX + radius * Math.cos(angle);&nbsp; const y = centerY + radius * Math.sin(angle);&nbsp;&nbsp;&nbsp; points.push({ x, y });&nbsp; ctx.lineTo(x, y);&nbsp; }&nbsp;&nbsp;/* Complete path and render as stroke */ctx.closePath();ctx.stroke();console.log(points);<canvas id="canvas" width="200" height="200"></canvas>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript