Skip to content

🎥 Make videos programmatically with React

pie rotation

Jonny Burgerec8065689c50
diff --git a/packages/docs/components/demos/Pie.tsx b/packages/docs/components/demos/Pie.tsx
index 35a96f59cb..fd2ab22650 100644
--- a/packages/docs/components/demos/Pie.tsx
+++ b/packages/docs/components/demos/Pie.tsx
@@ -9,6 +9,7 @@ export const PieDemo: React.FC<{
closePath: boolean;
showStrokeInsteadPreviewOnly: boolean;
counterClockwise: boolean;
+ rotation: number;
}> = ({
radius,
darkMode,
@@ -16,6 +17,7 @@ export const PieDemo: React.FC<{
closePath,
showStrokeInsteadPreviewOnly,
counterClockwise,
+ rotation,
}) => {
return (
<AbsoluteFill
@@ -38,6 +40,7 @@ export const PieDemo: React.FC<{
stroke={darkMode ? "white" : "var(--ifm-link-color)"}
strokeWidth={showStrokeInsteadPreviewOnly ? 5 : 0}
counterClockwise={counterClockwise}
+ rotation={rotation}
/>
</AbsoluteFill>
);
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index 0739d3e445..2a8e8ede56 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -193,6 +193,15 @@ export const pieDemo: DemoType = {
name: "progress",
optional: "no",
},
+ {
+ default: 0,
+ step: 0.01,
+ min: -3.14 * 2,
+ max: 3.14 * 2,
+ type: "numeric",
+ name: "rotation",
+ optional: "no",
+ },
{
default: true,
type: "boolean",
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsx
index f449be11bb..8528e00e94 100644
--- a/packages/docs/components/shapes/shapes-info.tsx
+++ b/packages/docs/components/shapes/shapes-info.tsx
@@ -65,8 +65,12 @@ export const shapeComponents: ShapeComponent[] = [
{
name: "progress",
type: "number",
- description:
- "The percentage of the circle that is filled. 0 means fully empty, 1 means fully filled.",
+ description: (
+ <>
+ The percentage of the circle that is filled. <code>0</code> means
+ fully empty, <code>1</code> means fully filled.
+ </>
+ ),
},
{
name: "counterClockwise",
@@ -74,6 +78,16 @@ export const shapeComponents: ShapeComponent[] = [
description:
"If set, the circle gets filled counterclockwise instead of clockwise. Default false.",
},
+ {
+ name: "rotation",
+ type: "boolean",
+ description: (
+ <>
+ Add rotation to the path. <code>0</code> means no rotation,{" "}
+ <code>Math.PI * 2</code> means 1 full clockwise rotation{" "}
+ </>
+ ),
+ },
],
},
{
diff --git a/packages/shapes/src/components/pie.tsx b/packages/shapes/src/components/pie.tsx
index 73656b5393..a75a9ecced 100644
--- a/packages/shapes/src/components/pie.tsx
+++ b/packages/shapes/src/components/pie.tsx
@@ -11,11 +11,12 @@ export const Pie: React.FC<PieProps> = ({
progress,
closePath,
counterClockwise,
+ rotation,
...props
}) => {
return (
<RenderSvg
- {...makePie({radius, progress, closePath, counterClockwise})}
+ {...makePie({radius, progress, closePath, counterClockwise, rotation})}
{...props}
/>
);
diff --git a/packages/shapes/src/utils/make-pie.ts b/packages/shapes/src/utils/make-pie.ts
index 0471945975..5c50e76f7f 100644
--- a/packages/shapes/src/utils/make-pie.ts
+++ b/packages/shapes/src/utils/make-pie.ts
@@ -6,6 +6,31 @@ export type MakePieProps = {
progress: number;
closePath?: boolean;
counterClockwise?: boolean;
+ rotation?: number;
+};
+
+const getCoord = ({
+ counterClockwise,
+ actualProgress,
+ rotation,
+ radius,
+ coord,
+}: {
+ counterClockwise: boolean;
+ actualProgress: number;
+ rotation: number;
+ radius: number;
+ coord: 'x' | 'y';
+}): number => {
+ const factor = counterClockwise ? -1 : 1;
+
+ return (
+ Math[coord === 'x' ? 'cos' : 'sin'](
+ factor * actualProgress * Math.PI * 2 + Math.PI * 1.5 + rotation
+ ) *
+ radius +
+ radius
+ );
};
export const makePie = ({
@@ -13,18 +38,41 @@ export const makePie = ({
radius,
closePath = true,
counterClockwise = false,
+ rotation = 0,
}: MakePieProps) => {
const actualProgress = Math.min(Math.max(progress, 0), 1);
- const factor = counterClockwise ? -1 : 1;
- const endAngleX =
- Math.cos(factor * actualProgress * Math.PI * 2 + Math.PI * 1.5) * radius +
- radius;
- const endAngleY =
- Math.sin(factor * actualProgress * Math.PI * 2 + Math.PI * 1.5) * radius +
- radius;
+ const endAngleX = getCoord({
+ actualProgress,
+ coord: 'x',
+ counterClockwise,
+ radius,
+ rotation,
+ });
+ const endAngleY = getCoord({
+ actualProgress,
+ coord: 'y',
+ counterClockwise,
+ radius,
+ rotation,
+ });
- const start = {x: radius, y: 0};
+ const start = {
+ x: getCoord({
+ actualProgress: 0,
+ coord: 'x',
+ counterClockwise,
+ radius,
+ rotation,
+ }),
+ y: getCoord({
+ actualProgress: 0,
+ coord: 'y',
+ counterClockwise,
+ radius,
+ rotation,
+ }),
+ };
const end = {x: endAngleX, y: endAngleY};
const instructions: Instruction[] = [
@@ -39,8 +87,26 @@ export const makePie = ({
xAxisRotation: 0,
largeArcFlag: false,
sweepFlag: !counterClockwise,
- x: actualProgress <= 0.5 ? endAngleX : radius,
- y: actualProgress <= 0.5 ? endAngleY : radius * 2,
+ x:
+ actualProgress <= 0.5
+ ? endAngleX
+ : getCoord({
+ actualProgress: 0.5,
+ coord: 'x',
+ counterClockwise,
+ radius,
+ rotation,
+ }),
+ y:
+ actualProgress <= 0.5
+ ? endAngleY
+ : getCoord({
+ actualProgress: 0.5,
+ coord: 'y',
+ counterClockwise,
+ radius,
+ rotation,
+ }),
},
actualProgress > 0.5
? {