我找到了导致圆圈变形的原因。
我曾经stroke-width
设置过圆的“胖”/“帽”,越大越变形。
至少,这些是我的观察结果,从技术上讲,它也可能是由其他原因引起的。
无论如何,为了得到合适的甜甜圈,我最终采用了这种方法:
/**
* Donut circle drawing
*
* @param integer start Percentage to start with
* @param float diameter
* @param float fat How fat should the circle bar be
* @return object
*/
var fatDonutArc = function (start, diameter, fat)
{
var center = diameter / 2;
var outerRadius = center;
var innerRadius = center - fat; // subtract fat
var alpha = 360 / 100 * start;
var a = (90 - alpha) * Math.PI / -180; // -180 starts to draw from 12 o'clock
// calculate outer ring point coordinates
var outerX = center + outerRadius * Math.cos(a);
var outerY = center + outerRadius * Math.sin(a);
// calculate inner ring point coordinates
var innerX = center + innerRadius * Math.cos(a);
var innerY = center + innerRadius * Math.sin(a);
// path cache
var path;
if (start !== 100)
{
path = [
// move to start point of inner ring
[
"M",
center,
center - innerRadius
],
// draw a line to outer ring
[
"L",
center,
center - outerRadius
],
// arc to outer ring end
[
"A",
outerRadius,
outerRadius,
0,
+(alpha > 180),
1,
outerX,
outerY
],
// move to inner ring end
[
"L",
innerX,
innerY
],
// arc to inner ring start
[
"A",
innerRadius,
innerRadius,
0,
+(alpha > 180),
0,
center,
center - innerRadius
]
];
}
else
{
path = [
// move to outer ring start
[
"M",
center,
center - outerRadius
],
// arc around the clock
[
"A",
outerRadius,
outerRadius,
0,
+(alpha > 180),
1,
outerX - .1, // subtract, otherwise the path becomes "reset"
outerY
],
// connect
[
"z"
],
// move to inner circle start
[
"M",
innerX,
innerY
],
// arc around the clock
[
"A",
innerRadius,
innerRadius,
0,
+(alpha > 180),
0,
innerX + .1, // subtract, otherwise the path becomes "reset"
innerY
],
// and connect
[
"z"
]
];
}
return {
path : path
};
};
这是以下内容的混搭:raphael.js - 将饼图转换为圆环图+ http://raphaeljs.com/polar-clock.html
在这里,我设置了一个示例,以查看它的实际效果:http: //jsbin.com/erusos/1
还有一个悬而未决的问题:在 Chrome 中,是 CSS 渲染器,没有完全环绕圆圈,还是 SVG?
享受!