Skip to content

🎥 Make videos programmatically with React

`@remotion/shapes`: Fix bounding box of Star and Polygon

Jonny Burgereeb6de5760f8
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 22b50fdd5d..36ec2242c0 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,3 +1,5 @@
+import {getBoundingBoxFromInstructions} from './get-bounding-box';
+
export {evolvePath} from './evolve-path';
export {extendViewBox} from './extend-viewbox';
export {getBoundingBox} from './get-bounding-box';
@@ -23,3 +25,7 @@ export {scalePath} from './scale-path';
export {serializeInstructions} from './serialize-instructions';
export {translatePath} from './translate-path';
export {WarpPathFn, warpPath} from './warp-path';
+
+export const PathInternals = {
+ getBoundingBoxFromInstructions,
+};
diff --git a/packages/shapes/src/utils/make-polygon.ts b/packages/shapes/src/utils/make-polygon.ts
index ca77e15bf7..a04fc59209 100644
--- a/packages/shapes/src/utils/make-polygon.ts
+++ b/packages/shapes/src/utils/make-polygon.ts
@@ -1,5 +1,10 @@
import type {Instruction} from '@remotion/paths';
-import {serializeInstructions} from '@remotion/paths';
+import {
+ PathInternals,
+ reduceInstructions,
+ resetPath,
+ serializeInstructions,
+} from '@remotion/paths';
import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
@@ -68,10 +73,15 @@ export const makePolygon = ({
cornerRadius,
edgeRoundness,
});
+
+ const reduced = reduceInstructions(polygonPathInstructions);
+ const path = resetPath(serializeInstructions(reduced));
+ const boundingBox = PathInternals.getBoundingBoxFromInstructions(reduced);
+
return {
- path: serializeInstructions(polygonPathInstructions),
- width,
- height,
+ path,
+ width: boundingBox.width,
+ height: boundingBox.height,
transformOrigin: `${centerX} ${centerY}`,
instructions: polygonPathInstructions,
};
diff --git a/packages/shapes/src/utils/make-star.ts b/packages/shapes/src/utils/make-star.ts
index 7b8e0bc987..912351512d 100644
--- a/packages/shapes/src/utils/make-star.ts
+++ b/packages/shapes/src/utils/make-star.ts
@@ -1,7 +1,12 @@
// Copied from https://stackblitz.com/edit/svg-star-generator?file=index.js
import type {Instruction} from '@remotion/paths';
-import {resetPath, serializeInstructions} from '@remotion/paths';
+import {
+ PathInternals,
+ reduceInstructions,
+ resetPath,
+ serializeInstructions,
+} from '@remotion/paths';
import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
@@ -86,10 +91,14 @@ export const makeStar = ({
edgeRoundness,
});
+ const reduced = reduceInstructions(starPathInstructions);
+ const path = resetPath(serializeInstructions(reduced));
+ const boundingBox = PathInternals.getBoundingBoxFromInstructions(reduced);
+
return {
- path: resetPath(serializeInstructions(starPathInstructions)),
- width,
- height,
+ path,
+ width: boundingBox.width,
+ height: boundingBox.height,
transformOrigin: `${centerX} ${centerY}`,
instructions: starPathInstructions,
};