Skip to content

🎥 Make videos programmatically with React

sketch out pie component

Jonny Burger4b01e0444210
diff --git a/packages/shapes/src/components/pie.tsx b/packages/shapes/src/components/pie.tsx
new file mode 100644
index 0000000000..ba41d30b6e
--- /dev/null
+++ b/packages/shapes/src/components/pie.tsx
@@ -0,0 +1,11 @@
+import React from 'react';
+import type {MakePieProps} from '../utils/make-pie';
+import {makePie} from '../utils/make-pie';
+import type {AllShapesProps} from './render-svg';
+import {RenderSvg} from './render-svg';
+
+export type PieProps = MakePieProps & AllShapesProps;
+
+export const Pie: React.FC<PieProps> = ({radius, fillAmount, ...props}) => {
+ return <RenderSvg {...makePie({radius, fillAmount})} {...props} />;
+};
diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.ts
index 648e34b905..a4c9f6a86f 100644
--- a/packages/shapes/src/index.ts
+++ b/packages/shapes/src/index.ts
@@ -1,11 +1,13 @@
export {Circle, CircleProps} from './components/circle';
export {Ellipse, EllipseProps} from './components/ellipse';
+export {Pie, PieProps} from './components/pie';
export {Rect, RectProps} from './components/rect';
export {Star, StarProps} from './components/star';
export {Triangle, TriangleProps} from './components/triangle';
export {Instruction} from './utils/instructions';
export {makeCircle, MakeCircleProps} from './utils/make-circle';
export {makeEllipse, MakeEllipseOptions} from './utils/make-ellipse';
+export {makePie, MakePieProps} from './utils/make-pie';
export {makeRect, MakeRectOptions} from './utils/make-rect';
export {makeStar, MakeStarProps} from './utils/make-star';
export {makeTriangle, MakeTriangleProps} from './utils/make-triangle';
diff --git a/packages/shapes/src/utils/make-pie.ts b/packages/shapes/src/utils/make-pie.ts
new file mode 100644
index 0000000000..4f87809ff7
--- /dev/null
+++ b/packages/shapes/src/utils/make-pie.ts
@@ -0,0 +1,52 @@
+import type {Instruction} from './instructions';
+import {serializeInstructions} from './instructions';
+
+export type MakePieProps = {
+ radius: number;
+ fillAmount: number;
+};
+
+export const makePie = ({fillAmount, radius}: MakePieProps) => {
+ const instructions: Instruction[] = [
+ {
+ type: 'M',
+ x: 0,
+ y: radius,
+ },
+ {
+ type: 'a',
+ rx: radius,
+ ry: radius,
+ xAxisRotation: 0,
+ largeArcFlag: true,
+ sweepFlag: false,
+ x: radius * 2,
+ y: 0,
+ },
+ fillAmount > 0.5
+ ? {
+ type: 'a',
+ rx: radius,
+ ry: radius,
+ xAxisRotation: 0,
+ largeArcFlag: true,
+ sweepFlag: false,
+ x: -radius * 2,
+ y: 0,
+ }
+ : null,
+ {
+ type: 'z',
+ },
+ ].filter(Boolean) as Instruction[];
+
+ const path = serializeInstructions(instructions);
+
+ return {
+ height: radius * 2,
+ width: radius * 2,
+ path,
+ instructions,
+ transformOrigin: `${radius} ${radius}`,
+ };
+};