I would like to draw a donut in HTML5 canvas. If the background color of the canvas is a solid color, I was able to draw it. But this is a gradient color, I can’t draw it.
I would like to know how to draw a donut when the canvas backgroud color has a gradient color.
Like:

A source
This is my code:
function background(context, coordinate, properties) { var x = coordinate.x //起始点x , y = coordinate.y //起始点 y , w = coordinate.w //宽度(终点-起始点之间的宽度) , h = coordinate.h //高度(终点-起始点之间的高度) , gradientFactor, gradientColor; //渐变因子, 渐变色 context.save(); switch( properties["background-fill-type"] ) { case "solid": context.fillStyle = properties["background-color"]; break; case "gradient": gradientFactor = properties["background-gradient-factor"]; gradientColor = context.createLinearGradient(x, y, x + w, y); gradientColor.addColorStop(gradientFactor, properties["background-first-color"]); gradientColor.addColorStop(1 - gradientFactor, properties["background-second-color"]); context.fillStyle = gradientColor; break; case "image": break; } context.fillRect(x, y, w, h); context.restore(); }
- If the background color of the canvas is solid color:
var context = canvas.getContext("2d") , properties = { "background-fill-type": "solid",
- If the background color of the canvas has a gradient color:
var context = canvas.getContext("2d") , properties = { "background-fill-type": "gradient",
This is an effect chart. (A little crooked, - -!):

source share