html5 linear,html - Draw arc will linear gradient html5 canvas - Stack Overflow

I couldn't see the example that the OP posted (broken link?), but I had a very similar question and none of the answers here helped me.

So, this is what I wanted to do:

f3d29e5c6f0c96cd1ca6cbebc22196d0.png

And this is how I did it:

var center = { "x": 115, "y": 115 };

var radius = 100;

var quadrants = [

{

"angleStart": Math.PI * -0.5,

"angleEnd": 0,

"x1": center.x,

"y1": center.y - radius,

"x2": center.x + radius,

"y2": center.y,

"colorStops": [

{ "stop": 0, "color": "blue" },

{ "stop": 1, "color": "green" }

]

},

{

"angleStart": 0,

"angleEnd": Math.PI * 0.5,

"x1": center.x + radius,

"y1": center.y,

"x2": center.x,

"y2": center.y + radius,

"colorStops": [

{ "stop": 0, "color": "green" },

{ "stop": 1, "color": "yellow" }

]

},

{

"angleStart": Math.PI * 0.5,

"angleEnd": Math.PI,

"x1": center.x,

"y1": center.y + radius,

"x2": center.x - radius,

"y2": center.y,

"colorStops": [

{ "stop": 0, "color": "yellow" },

{ "stop": 1, "color": "red" }

]

},

{

"angleStart": Math.PI,

"angleEnd": Math.PI * 1.5,

"x1": center.x - radius,

"y1": center.y,

"x2": center.x,

"y2": center.y - radius,

"colorStops": [

{ "stop": 0, "color": "red" },

{ "stop": 1, "color": "black" }

]

}

];

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

// Draw arc quadrants.

for (var i = 0; i < quadrants.length; ++i) {

var quad = quadrants[i];

var grad = ctx.createLinearGradient(quad.x1, quad.y1, quad.x2, quad.y2);

// Color stops.

for (var j = 0; j < quad.colorStops.length; ++j) {

var cs = quad.colorStops[j];

grad.addColorStop(cs.stop, cs.color);

}

// Draw arc.

ctx.beginPath();

ctx.arc(center.x, center.y, radius, quad.angleStart, quad.angleEnd);

ctx.strokeStyle = grad;

ctx.lineWidth = 30;

ctx.stroke();

}

Your browser does not support the HTML5 canvas tag.

Note: The canvas tag is not supported in Internet

Explorer 8 and earlier versions.

And below is my draft idea before implementing it:

Linear gradient distribution on arc:

***********

*** | ***

** | **

** | **

* _ | *

** /| | \ **

** / | _\| **

* | *

* 4th Quadrant | 1st Quadrant *

* | *

*-------------------|-------------------*

* | *

* 3rd Quadrant | 2nd Quadrant *

* _ | *

** |\ | / **

** \ | |/_ **

* | *

** | **

** | **

*** | ***

***********

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值