CSS - how to create circle pie canvas like this?

前端 未结 5 918
醉酒成梦
醉酒成梦 2021-01-05 20:03

I really like this element,

\"this

but how to create it? I am not sure what\'s the correct de

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 20:09

    This effect can be achieved by layering a couple arc()s:

    // bright blue full circle
    d.beginPath();
    d.arc(50, 50, 50, 0, 2 * Math.PI, false);
    d.fillStyle = "#aaeeff";
    d.fill();
    
    // dark blue percentage circle
    d.beginPath();
    d.moveTo(50, 50);
    d.arc(50, 50, 50, -0.5 * Math.PI, 0.78 * 2 * Math.PI - 0.5 * Math.PI, false);
    d.fillStyle = "#00aaff";
    d.fill();
    
    // white inner filler
    d.beginPath();
    d.moveTo(50, 50);
    d.arc(50, 50, 25, 0, 2 * Math.PI, false);
    d.fillStyle = "#ffffff";
    d.fill();
    

    and finally rendering the text:

    d.moveTo(50, 50);
    d.fillStyle = "#606060";
    d.font = "12pt sans-serif";
    d.fillText("78%", 36, 56);
    

    Fiddle: http://jsfiddle.net/j6NVg/

提交回复
热议问题