
本文详解 Canvas arc() 方法绘制扇形(即圆心角区域)的原理与实践,重点解决“只渲染外弧、不填充扇形”这一高频问题,涵盖 beginPath() 时机、路径闭合逻辑、坐标系理解及完整可运行示例。
本文详解 canvas `arc()` 方法绘制扇形(即圆心角区域)的原理与实践,重点解决“只渲染外弧、不填充扇形”这一高频问题,涵盖 `beginpath()` 时机、路径闭合逻辑、坐标系理解及完整可运行示例。
在 HTML5 Canvas 中,ctx.arc() 本身仅定义一段圆弧路径,而非一个封闭图形。当你调用 ctx.fill() 却只看到一条细弧线(或“外层窄条”),本质原因是:该弧线未与圆心构成闭合区域——Canvas 不会自动连接弧的两端到圆心;它严格遵循路径(path)模型:只有明确闭合的路径才能被正确填充。
你遇到的“只画出外层细 sliver”现象,正是典型路径缺失导致的填充失效。原始代码中:
ctx.moveTo(startingLocation.x, startingLocation.y); ctx.beginPath(); // ❌ 错误:beginPath() 清空了前面的 moveTo! ctx.arc(...); ctx.fill();
此处 beginPath() 被错误地置于 moveTo() 之后,导致 moveTo() 设定的起点被丢弃。而 arc() 在无当前子路径时,会自动将弧的起始点设为新路径起点(即 (x + r·cos(θ), y + r·sin(θ))),但不会连接到圆心。因此 fill() 实际填充的是一个退化为线段的“零面积”路径(或浏览器按默认规则填充极小区域),视觉上就是一条细弧。
✅ 正确做法是:先 beginPath(),再 moveTo(圆心),接着 lineTo(弧起点) → arc() → lineTo(圆心),形成三角扇形闭环。
✅ 正确绘制扇形的四步路径法
以下为修复后、可直接运行的核心逻辑(已适配你的 FOV 可视化需求):
// ✅ 步骤1:开启新路径(清除历史残留) ctx.beginPath(); // ✅ 步骤2:移动到圆心(扇形顶点) ctx.moveTo(startingLocation.x, startingLocation.y); // ✅ 步骤3:画第一条半径线 → 弧起点 const x1 = startingLocation.x + r * Math.cos(theta); const y1 = startingLocation.y + r * Math.sin(theta); ctx.lineTo(x1, y1); // ✅ 步骤4:画圆弧(从 theta 到 theta2,顺时针) ctx.arc(startingLocation.x, startingLocation.y, r, theta, theta2, false); // ✅ 步骤5:画第二条半径线 → 回到圆心(闭合扇形) ctx.lineTo(startingLocation.x, startingLocation.y); // ✅ 步骤6:填充(此时路径已封闭!) ctx.fillStyle = 'rgba(74, 144, 226, 0.3)'; ctx.fill();
? 关键说明:
arc()的false表示顺时针绘图,符合常规 FOV 角度递增方向(如从 18.4° 到 48.4°);lineTo(startingLocation.x, startingLocation.y)是闭合扇形的必要操作,不可省略;closePath()在此场景下非必需(因已显式lineTo回圆心),且若误用可能引入冗余线段。
? 角度单位与坐标系须知
-
角度必须为弧度:
theta = angleInDegrees * Math.PI / 180—— 这是最高频错误源(如传30会被解释为 30 弧度 ≈ 1719°,远超预期); -
Canvas 坐标系 Y 轴向下:
Math.sin()对应垂直偏移为正方向(与数学笛卡尔坐标系相反),但Math.atan2(dy, dx)已天然适配此约定; -
圆心坐标需在画布内:你的
startingLocation = {x:0, y:0}位于左上角,确保r=100时x+r·cos(θ) ,否则弧线部分被裁剪。
? 完整可运行示例(精简版)
<canvas id="canvas" width="400" height="400" style="border:1px solid #ccc;"></canvas><script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const startingLocation = { x: 200, y: 200 }; // 居中圆心,避免越界
const fovAngle = 30;
const r = 120;
// 计算起始角(以正东为0°,逆时针为正,但Canvas中顺时针更直观)
let angle = 18.43494882292201; // 示例:指向右下
let theta = (angle * Math.PI) / 180;
let theta2 = ((angle + fovAngle) * Math.PI) / 180;
// 绘制扇形区域
ctx.beginPath();
ctx.moveTo(startingLocation.x, startingLocation.y); // 圆心
ctx.lineTo(
startingLocation.x + r * Math.cos(theta),
startingLocation.y + r * Math.sin(theta)
);
ctx.arc(startingLocation.x, startingLocation.y, r, theta, theta2, false);
ctx.lineTo(startingLocation.x, startingLocation.y); // 闭合回圆心
ctx.fillStyle = 'rgba(106, 198, 86, 0.4)';
ctx.fill();
// 可选:描边增强视觉
ctx.strokeStyle = '#6ac656';
ctx.lineWidth = 1.5;
ctx.stroke();
// 绘制参考线(起始/结束射线)
ctx.beginPath();
ctx.moveTo(startingLocation.x, startingLocation.y);
ctx.lineTo(
startingLocation.x + r * Math.cos(theta),
startingLocation.y + r * Math.sin(theta)
);
ctx.moveTo(startingLocation.x, startingLocation.y);
ctx.lineTo(
startingLocation.x + r * Math.cos(theta2),
startingLocation.y + r * Math.sin(theta2)
);
ctx.strokeStyle = '#6ac656';
ctx.stroke();
</script>
⚠️ 注意事项总结
-
永远在
moveTo()前调用beginPath():这是 Canvas 状态管理的铁律,避免路径污染; -
扇形 ≠
arc()单独调用:arc()仅生成弧线路径,扇形需手动补两条半径线并闭合; -
closePath()不等于“闭合填充区域”:它只是画一条直线回起点,但起点未必是圆心——显式lineTo(圆心)更可控; -
透明度慎用
globalAlpha:建议直接用rgba()设置fillStyle,避免影响后续绘制; -
调试技巧:临时添加
ctx.stroke()查看路径轮廓,确认是否真正闭合。
掌握这一路径构造逻辑,你不仅能正确绘制 FOV 扇形,还可延伸实现饼图、雷达图、环形进度条等复杂图形——Canvas 的强大,正在于对路径的精确掌控。










