Skip to content

🎥 Make videos programmatically with React

fix "move" bugs

Jonny Burger3753e86b2294
diff --git a/packages/paths/src/get-bounding-box.ts b/packages/paths/src/get-bounding-box.ts
index 6d258849e2..3a1d7b4f5c 100644
--- a/packages/paths/src/get-bounding-box.ts
+++ b/packages/paths/src/get-bounding-box.ts
@@ -90,7 +90,7 @@ function minmaxC(A: [number, number, number, number]): minMax {
/**
* @description Returns the bounding box of the given path, suitable for calculating the viewBox value that you need to pass to an SVG.
- * @param {string} d
+ * @param {string} d
* @see [Documentation](https://www.remotion.dev/docs/paths/get-bounding-box)
*/
export const getBoundingBox = (d: string): BoundingBox => {
@@ -110,10 +110,35 @@ export const getBoundingBoxFromInstructions = (
let x = 0;
let y = 0;
+ let lastMoveX = 0;
+ let lastMoveY = 0;
for (const seg of instructions) {
switch (seg.type) {
- case 'M':
+ case 'M': {
+ lastMoveX = seg.x;
+ lastMoveY = seg.y;
+ if (minX > seg.x) {
+ minX = seg.x;
+ }
+
+ if (minY > seg.y) {
+ minY = seg.y;
+ }
+
+ if (maxX < seg.x) {
+ maxX = seg.x;
+ }
+
+ if (maxY < seg.y) {
+ maxY = seg.y;
+ }
+
+ x = seg.x;
+ y = seg.y;
+ break;
+ }
+
case 'L': {
if (minX > seg.x) {
minX = seg.x;
@@ -188,6 +213,8 @@ export const getBoundingBoxFromInstructions = (
}
case 'Z':
+ x = lastMoveX;
+ y = lastMoveY;
break;
default:
diff --git a/packages/paths/src/normalize-path.ts b/packages/paths/src/normalize-path.ts
index 183083445e..66843a6af7 100644
--- a/packages/paths/src/normalize-path.ts
+++ b/packages/paths/src/normalize-path.ts
@@ -21,11 +21,19 @@ export const normalizeInstructions = (
let x = 0;
let y = 0;
- const initialX = 0;
- const initialY = 0;
+ let moveX = 0;
+ let moveY = 0;
for (let i = 0; i < instructions.length; i++) {
const instruction = instructions[i];
+ if (instruction.type === 'M') {
+ moveX = instruction.x;
+ moveY = instruction.y;
+ } else if (instruction.type === 'm') {
+ moveX += instruction.dx;
+ moveY += instruction.dy;
+ }
+
if (
instruction.type === 'A' ||
instruction.type === 'C' ||
@@ -145,8 +153,8 @@ export const normalizeInstructions = (
if (instruction.type === 'Z') {
normalized.push(instruction);
- x = initialX;
- y = initialY;
+ x = moveX;
+ y = moveY;
continue;
}