Skip to content

Commit cbc3be9

Browse files
committed
fix: draw circle
1 parent c2d523d commit cbc3be9

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/skia/SkiaPathRebuilder.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { PathRebuilder } from 'zrender/lib/core/PathProxy';
22
import { SkPath, Skia } from '@shopify/react-native-skia';
3+
import { isAroundZero } from './helper';
34

5+
const PI = Math.PI;
6+
const PI2 = Math.PI * 2;
47
export default class SkiaPathRebuilder implements PathRebuilder {
58
private path: SkPath;
69

@@ -50,24 +53,41 @@ export default class SkiaPathRebuilder implements PathRebuilder {
5053
rotation: number,
5154
startAngle: number,
5255
endAngle: number,
53-
counterclockwise: boolean = false
56+
anticlockwise: boolean = false
5457
) {
55-
const useSmallArc = Math.abs(endAngle - startAngle) <= Math.PI;
58+
let dTheta = endAngle - startAngle;
59+
const clockwise = !anticlockwise;
60+
61+
const dThetaPositive = Math.abs(dTheta);
62+
const isCircle =
63+
isAroundZero(dThetaPositive - PI2) ||
64+
(clockwise ? dTheta >= PI2 : -dTheta >= PI2);
65+
66+
const useSmallArc = Math.abs(endAngle - startAngle) <= PI;
5667

5768
const endX = x + radiusX * Math.cos(endAngle);
5869
const endY = y + radiusY * Math.sin(endAngle);
5970

60-
const xAxisRotateInDegrees = (rotation * 180) / Math.PI;
61-
62-
this.path.arcToRotated(
63-
radiusX,
64-
radiusY,
65-
xAxisRotateInDegrees,
66-
useSmallArc,
67-
counterclockwise,
68-
endX,
69-
endY
70-
);
71+
const xAxisRotateInDegrees = (rotation * 180) / PI;
72+
if (isCircle) {
73+
const ovalRect = {
74+
x: x - radiusX,
75+
y: y - radiusY,
76+
width: radiusX * 2,
77+
height: radiusY * 2,
78+
};
79+
this.path.addOval(ovalRect, anticlockwise);
80+
} else {
81+
this.path.arcToRotated(
82+
radiusX,
83+
radiusY,
84+
xAxisRotateInDegrees,
85+
useSmallArc,
86+
anticlockwise,
87+
endX,
88+
endY
89+
);
90+
}
7191
}
7292

7393
rect(x: number, y: number, width: number, height: number): void {

src/skia/helper.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const EPSILON = 1e-4;
2+
export function isAroundZero(transform: number) {
3+
return transform < EPSILON && transform > -EPSILON;
4+
}

0 commit comments

Comments
 (0)