javascript - Canvas 中圆弧的不同 fillStyle 颜色

标签 javascript colors html5-canvas fill geometric-arc

我想这个问题的解决方案非常简单,如果这是非常明显的,请提前道歉,但我似乎无法弄清楚如何为两个不同的弧设置两个不同的 fillStyles ...我只是想能够绘制不同颜色的圆圈。下面我介绍了我通常如何在 Canvas 中使用其他形状/绘图方法来完成此操作,但由于某种原因,对于弧线,它将两个弧线设置为最后一个 fillStyle。

ctx.fillStyle = "#c82124"; //red
ctx.arc(15,15,15,0,Math.PI*2,true);
ctx.fill();

ctx.fillStyle = "#3370d4"; //blue
ctx.arc(580,15,15,0,Math.PI*2,true);
ctx.fill();

最佳答案

我认为您缺少开始和结束路径语句。尝试以下操作(它在 jsfiddle 中对我有用, see here )

ctx.fillStyle = "#c82124"; //red
ctx.beginPath();
ctx.arc(15,15,15,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();

ctx.fillStyle = "#3370d4"; //blue
ctx.beginPath();
ctx.arc(580,15,15,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();

关于javascript - Canvas 中圆弧的不同 fillStyle 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549725/

相关文章:

javascript - 使用 Canvas 缩放图像而不使其模糊

javascript - 在 HTML5 Canvas 中旋转 JS 对象时 Y 轴出现问题

javascript - 成功注入(inject)后在指令 View 中显示常量

JavaScript 错误无效量词

JavaScript 回调函数和 Google Feed API

java - 2D 按钮数组 ActionListener,颜色变化?

matlab - 加快样条曲线的着色段?

C# - 使用 RGB 值在颜色对话框中设置自定义颜色

javascript - 如何在 paper.js 中沿着贝塞尔曲线路径对对象进行动画处理?

javascript - "Message send"使用 PHP 和 JS 的确认弹出窗口