Skip to content

🎥 Make videos programmatically with React

pie component

Jonny Burger0a66f9cae1a7
diff --git a/packages/shapes/src/test/make-pie.test.ts b/packages/shapes/src/test/make-pie.test.ts
new file mode 100644
index 0000000000..1e952cfb61
--- /dev/null
+++ b/packages/shapes/src/test/make-pie.test.ts
@@ -0,0 +1,27 @@
+import {expect, test} from 'vitest';
+import {makePie} from '../utils/make-pie';
+
+test('Test makePie() function', () => {
+ const pie = makePie({progress: 0.5, radius: 100, closePath: true});
+ expect(pie).toEqual({
+ height: 200,
+ width: 200,
+ path: 'M 100 0 A 100 100 0 0 1 100 200 L 100 100 z',
+ instructions: [
+ {type: 'M', x: 100, y: 0},
+ {
+ type: 'A',
+ rx: 100,
+ ry: 100,
+ xAxisRotation: 0,
+ largeArcFlag: false,
+ sweepFlag: true,
+ x: 100,
+ y: 200,
+ },
+ {type: 'L', x: 100, y: 100},
+ {type: 'z'},
+ ],
+ transformOrigin: '100 100',
+ });
+});
diff --git a/packages/shapes/src/test/pie.test.tsx b/packages/shapes/src/test/pie.test.tsx
new file mode 100644
index 0000000000..d15ef0711e
--- /dev/null
+++ b/packages/shapes/src/test/pie.test.tsx
@@ -0,0 +1,12 @@
+import {expect, test} from 'vitest';
+import {Pie} from '../components/pie';
+import {render} from './test-utils';
+
+test('Should be able to make a pie svg', () => {
+ const {container} = render(<Pie progress={0.5} radius={100} />);
+
+ // assert path
+ expect(container.querySelector('path')?.getAttribute('d')).toEqual(
+ 'M 100 0 A 100 100 0 0 1 100 200 L 100 100 z'
+ );
+});
diff --git a/packages/shapes/src/utils/make-pie.ts b/packages/shapes/src/utils/make-pie.ts
index 5c50e76f7f..8f4f085f63 100644
--- a/packages/shapes/src/utils/make-pie.ts
+++ b/packages/shapes/src/utils/make-pie.ts
@@ -24,13 +24,15 @@ const getCoord = ({
}): number => {
const factor = counterClockwise ? -1 : 1;
- return (
+ const val =
Math[coord === 'x' ? 'cos' : 'sin'](
factor * actualProgress * Math.PI * 2 + Math.PI * 1.5 + rotation
) *
radius +
- radius
- );
+ radius;
+
+ const rounded = Math.round(val * 100000) / 100000;
+ return rounded;
};
export const makePie = ({