30 lines
1.4 KiB
JavaScript
30 lines
1.4 KiB
JavaScript
export const VIEW_BOX_SIZE = 100;
|
|
export const getCircleStyle = (perimeter, perimeterWithoutGap, offset, percent, rotateDeg, gapDegree, gapPosition, strokeColor, strokeLinecap, strokeWidth, stepSpace = 0) => {
|
|
const offsetDeg = offset / 100 * 360 * ((360 - gapDegree) / 360);
|
|
const positionDeg = gapDegree === 0 ? 0 : {
|
|
bottom: 0,
|
|
top: 180,
|
|
left: 90,
|
|
right: -90
|
|
}[gapPosition];
|
|
let strokeDashoffset = (100 - percent) / 100 * perimeterWithoutGap;
|
|
// Fix percent accuracy when strokeLinecap is round
|
|
// https://github.com/ant-design/ant-design/issues/35009
|
|
if (strokeLinecap === 'round' && percent !== 100) {
|
|
strokeDashoffset += strokeWidth / 2;
|
|
// when percent is small enough (<= 1%), keep smallest value to avoid it's disappearance
|
|
if (strokeDashoffset >= perimeterWithoutGap) {
|
|
strokeDashoffset = perimeterWithoutGap - 0.01;
|
|
}
|
|
}
|
|
const halfSize = VIEW_BOX_SIZE / 2;
|
|
return {
|
|
stroke: typeof strokeColor === 'string' ? strokeColor : undefined,
|
|
strokeDasharray: `${perimeterWithoutGap}px ${perimeter}`,
|
|
strokeDashoffset: strokeDashoffset + stepSpace,
|
|
transform: `rotate(${rotateDeg + offsetDeg + positionDeg}deg)`,
|
|
transformOrigin: `${halfSize}px ${halfSize}px`,
|
|
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s, opacity .3s ease 0s',
|
|
fillOpacity: 0
|
|
};
|
|
}; |