Skip to content

Commit

Permalink
fix: percent accuracy issue when >98% or <2%
Browse files Browse the repository at this point in the history
Reduce half value of storkeWidth when strokeLinecap="round"
  • Loading branch information
afc163 committed May 8, 2022
1 parent 4345937 commit e32bbdf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/examples/fast-progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class App extends React.Component<ProgressProps, any> {
this.restart = this.restart.bind(this);
}

private tm: number;
private tm: NodeJS.Timeout;

componentDidMount() {
this.increase();
Expand Down
31 changes: 20 additions & 11 deletions docs/examples/simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Example extends React.Component<ProgressProps, any> {
constructor(props) {
super(props);
this.state = {
percent: 30,
percent: 96,
color: '#3FC7FA',
};
this.changeState = this.changeState.bind(this);
Expand All @@ -23,19 +23,25 @@ class Example extends React.Component<ProgressProps, any> {
};

changeIncrease() {
let percent = this.state.percent + 10;
if (percent > 100) {
percent = 100;
}
this.setState({ percent });
this.setState(({ percent }) => {
if (percent > 100) {
percent = 100;
}
return {
percent: percent + 1,
};
});
};

changeReduce() {
let percent = this.state.percent - 10;
if (percent < 0) {
percent = 0;
}
this.setState({ percent });
this.setState(({ percent }) => {
if (percent < 0) {
percent = 0;
}
return {
percent: percent - 1,
};
});
};

render() {
Expand Down Expand Up @@ -63,6 +69,9 @@ class Example extends React.Component<ProgressProps, any> {
<div style={circleContainerStyle}>
<Circle percent={percent} strokeWidth={6} strokeLinecap="round" strokeColor={color} />
</div>
<div style={circleContainerStyle}>
<Circle percent={percent} strokeWidth={6} strokeLinecap="butt" strokeColor={color} />
</div>
<div style={circleContainerStyle}>
<Circle percent={percent} strokeWidth={6} strokeLinecap="square" strokeColor={color} />
</div>
Expand Down
32 changes: 28 additions & 4 deletions src/Circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const getCircleStyle = (
strokeColor: string | Record<string, string>,
gapDegree = 0,
gapPosition: GapPositionType,
strokeLinecap: ProgressProps['strokeLinecap'],
strokeWidth,
) => {
const rotateDeg = gapDegree > 0 ? 90 + gapDegree / 2 : -90;
const perimeter = Math.PI * 2 * radius;
Expand All @@ -35,10 +37,21 @@ const getCircleStyle = (
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 disapperance
if (strokeDashoffset >= perimeterWithoutGap) {
strokeDashoffset = perimeterWithoutGap - 0.01;
}
}

return {
stroke: typeof strokeColor === 'string' ? strokeColor : undefined,
strokeDasharray: `${perimeterWithoutGap}px ${perimeter}`,
strokeDashoffset: `${((100 - percent) / 100) * perimeterWithoutGap}px`,
strokeDashoffset,
transform: `rotate(${rotateDeg + offsetDeg + positionDeg}deg)`,
transformOrigin: '50% 50%',
transition:
Expand Down Expand Up @@ -66,7 +79,16 @@ const Circle: React.FC<ProgressProps> = ({
const gradientId = `${mergedId}-gradient`;
const radius = VIEW_BOX_SIZE / 2 - strokeWidth / 2;

const circleStyle = getCircleStyle(radius, 0, 100, trailColor, gapDegree, gapPosition);
const circleStyle = getCircleStyle(
radius,
0,
100,
trailColor,
gapDegree,
gapPosition,
strokeLinecap,
strokeWidth,
);
const percentList = toArray(percent);
const strokeColorList = toArray(strokeColor);
const gradient = strokeColorList.find((color) => color && typeof color === 'object');
Expand All @@ -86,6 +108,8 @@ const Circle: React.FC<ProgressProps> = ({
color,
gapDegree,
gapPosition,
strokeLinecap,
strokeWidth,
);
stackPtg += ptg;
return (
Expand All @@ -96,7 +120,7 @@ const Circle: React.FC<ProgressProps> = ({
cx={VIEW_BOX_SIZE / 2}
cy={VIEW_BOX_SIZE / 2}
stroke={stroke}
strokeLinecap={strokeLinecap === 'round' ? strokeLinecap : undefined}
strokeLinecap={strokeLinecap}
strokeWidth={strokeWidth}
opacity={ptg === 0 ? 0 : 1}
style={circleStyleForStack}
Expand Down Expand Up @@ -132,7 +156,7 @@ const Circle: React.FC<ProgressProps> = ({
cx={VIEW_BOX_SIZE / 2}
cy={VIEW_BOX_SIZE / 2}
stroke={trailColor}
strokeLinecap={strokeLinecap === 'round' ? strokeLinecap : undefined}
strokeLinecap={strokeLinecap}
strokeWidth={trailWidth || strokeWidth}
style={circleStyle}
/>
Expand Down

0 comments on commit e32bbdf

Please sign in to comment.