Skip to content

🎥 Make videos programmatically with React

reducedinstruction is now just a Q instruction

Jonny Burgera53f14586fc4
diff --git a/packages/paths/src/get-bounding-box.ts b/packages/paths/src/get-bounding-box.ts
index e5919311f3..b16b7eb925 100644
--- a/packages/paths/src/get-bounding-box.ts
+++ b/packages/paths/src/get-bounding-box.ts
@@ -1,4 +1,4 @@
-import {removeATSHVInstructions} from './helpers/remove-a-s-t-curves';
+import {removeATSHVQInstructions} from './helpers/remove-a-s-t-curves';
import type {
AbsoluteInstruction,
BoundingBox,
@@ -229,7 +229,7 @@ export const getBoundingBoxFromInstructions = (
*/
export const getBoundingBox = (d: string): BoundingBox => {
const parsed = parsePath(d) as AbsoluteInstruction[];
- const unarced = removeATSHVInstructions(normalizeInstructions(parsed));
+ const unarced = removeATSHVQInstructions(normalizeInstructions(parsed));
return getBoundingBoxFromInstructions(unarced);
};
diff --git a/packages/paths/src/helpers/convert-q-to-c-instruction.ts b/packages/paths/src/helpers/convert-q-to-c-instruction.ts
new file mode 100644
index 0000000000..743a9be787
--- /dev/null
+++ b/packages/paths/src/helpers/convert-q-to-c-instruction.ts
@@ -0,0 +1,22 @@
+import type {CInstruction, Point, QInstruction} from './types';
+
+export const convertQToCInstruction = (
+ instruction: QInstruction,
+ startPoint: Point,
+): CInstruction => {
+ const cp1x = startPoint.x + (2 / 3) * (instruction.cpx - startPoint.x);
+ const cp1y = startPoint.y + (2 / 3) * (instruction.cpy - startPoint.y);
+
+ const cp2x = instruction.x + (2 / 3) * (instruction.cpx - instruction.x);
+ const cp2y = instruction.y + (2 / 3) * (instruction.cpy - instruction.y);
+
+ return {
+ type: 'C',
+ cp1x,
+ cp1y,
+ cp2x,
+ cp2y,
+ x: instruction.x,
+ y: instruction.y,
+ };
+};
diff --git a/packages/paths/src/helpers/iterate.ts b/packages/paths/src/helpers/iterate.ts
index 9f2acda6c3..7b4309c7b4 100644
--- a/packages/paths/src/helpers/iterate.ts
+++ b/packages/paths/src/helpers/iterate.ts
@@ -1,13 +1,19 @@
-import type {AbsoluteInstruction, ReducedInstruction} from './types';
+import type {
+ AbsoluteInstruction,
+ QInstruction,
+ ReducedInstruction,
+} from './types';
-export const iterateOverSegments = <T extends ReducedInstruction>({
+export const iterateOverSegments = <
+ T extends ReducedInstruction | QInstruction,
+>({
segments,
iterate,
}: {
- segments: AbsoluteInstruction[];
+ segments: (AbsoluteInstruction | QInstruction)[];
iterate: (options: {
- segment: AbsoluteInstruction;
- prevSegment: AbsoluteInstruction | null;
+ segment: AbsoluteInstruction | QInstruction;
+ prevSegment: (AbsoluteInstruction | QInstruction) | null;
x: number;
y: number;
initialX: number;
diff --git a/packages/paths/src/helpers/remove-a-s-t-curves.ts b/packages/paths/src/helpers/remove-a-s-t-curves.ts
index d8ed110577..2a4907a2ea 100644
--- a/packages/paths/src/helpers/remove-a-s-t-curves.ts
+++ b/packages/paths/src/helpers/remove-a-s-t-curves.ts
@@ -1,3 +1,4 @@
+import {convertQToCInstruction} from './convert-q-to-c-instruction';
import {iterateOverSegments} from './iterate';
import type {AbsoluteInstruction, ReducedInstruction} from './types';
@@ -234,12 +235,12 @@ function arcToCircle({
}
// Requires path to be normalized
-export const removeATSHVInstructions = (
+export const removeATSHVQInstructions = (
segments: AbsoluteInstruction[],
): ReducedInstruction[] => {
return iterateOverSegments<ReducedInstruction>({
segments,
- iterate: ({segment, prevSegment, x, y, cpX, cpY}) => {
+ iterate: ({segment, prevSegment, x, y, cpX, cpY}): ReducedInstruction[] => {
if (segment.type === 'H') {
return [{type: 'L', x: segment.x, y}];
}
@@ -311,13 +312,16 @@ export const removeATSHVInstructions = (
const newControlY = y - vectorY;
return [
- {
- type: 'Q',
- cpx: newControlX,
- cpy: newControlY,
- x: segment.x,
- y: segment.y,
- },
+ convertQToCInstruction(
+ {
+ type: 'Q',
+ cpx: newControlX,
+ cpy: newControlY,
+ x: segment.x,
+ y: segment.y,
+ },
+ {x, y},
+ ),
];
}
@@ -355,6 +359,10 @@ export const removeATSHVInstructions = (
];
}
+ if (segment.type === 'Q') {
+ return [convertQToCInstruction(segment, {x, y})];
+ }
+
return [segment];
},
});
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
index bd95bac2db..dc4c84dedc 100644
--- a/packages/paths/src/helpers/types.ts
+++ b/packages/paths/src/helpers/types.ts
@@ -68,11 +68,11 @@ export type ReducedInstruction =
| MInstruction
| LInstruction
| CInstruction
- | QInstruction
| ZInstruction;
export type AbsoluteInstruction =
| ReducedInstruction
+ | QInstruction
| {
type: 'A';
rx: number;
diff --git a/packages/paths/src/reduce-instructions.ts b/packages/paths/src/reduce-instructions.ts
index 327f55effe..97cb1a0282 100644
--- a/packages/paths/src/reduce-instructions.ts
+++ b/packages/paths/src/reduce-instructions.ts
@@ -1,4 +1,4 @@
-import {removeATSHVInstructions} from './helpers/remove-a-s-t-curves';
+import {removeATSHVQInstructions} from './helpers/remove-a-s-t-curves';
import type {Instruction, ReducedInstruction} from './helpers/types';
import {normalizeInstructions} from './normalize-path';
@@ -11,5 +11,5 @@ export const reduceInstructions = (
instruction: Instruction[],
): ReducedInstruction[] => {
const simplified = normalizeInstructions(instruction);
- return removeATSHVInstructions(simplified);
+ return removeATSHVQInstructions(simplified);
};