Skip to content

🎥 Make videos programmatically with React

simplify svg instructions helper

Jonny Burger71fff2b3248f
diff --git a/packages/paths/src/helpers/iterate.ts b/packages/paths/src/helpers/iterate.ts
index 5f6431d4ab..e4359d25a6 100644
--- a/packages/paths/src/helpers/iterate.ts
+++ b/packages/paths/src/helpers/iterate.ts
@@ -10,10 +10,14 @@ export const iterateOverSegments = <T extends ReducesAbsoluteInstruction>({
prevSegment: AbsoluteInstruction | null;
x: number;
y: number;
+ initialX: number;
+ initialY: number;
}) => T[];
}): T[] => {
let x = 0;
let y = 0;
+ let initialX = 0;
+ let initialY = 0;
const newSegments = segments.map((s, i) => {
const newSeg = iterate({
@@ -21,9 +25,14 @@ export const iterateOverSegments = <T extends ReducesAbsoluteInstruction>({
x,
y,
prevSegment: segments[i - 1] ?? null,
+ initialX,
+ initialY,
});
switch (s.type) {
case 'M':
+ initialX = s.x;
+ initialY = s.y;
+ // fallthrough
case 'A':
case 'C':
case 'Q':
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 f58f78aa3e..33836260b6 100644
--- a/packages/paths/src/helpers/remove-a-s-t-curves.ts
+++ b/packages/paths/src/helpers/remove-a-s-t-curves.ts
@@ -129,7 +129,7 @@ function arcToCircle({
}
// Requires path to be normalized
-export const removeATSInstructions = (
+export const removeATSHVInstructions = (
segments: AbsoluteInstruction[]
): ReducesAbsoluteInstruction[] => {
let prevControlX = 0;
@@ -140,6 +140,14 @@ export const removeATSInstructions = (
return iterateOverSegments<ReducesAbsoluteInstruction>({
segments,
iterate: ({segment, prevSegment, x, y}) => {
+ if (segment.type === 'H') {
+ return [{type: 'L', x: segment.x, y}];
+ }
+
+ if (segment.type === 'V') {
+ return [{type: 'L', x, y: segment.y}];
+ }
+
if (segment.type === 'A') {
const nextX = segment.x;
const nextY = segment.y;
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
index f6f5c09684..35831d39b5 100644
--- a/packages/paths/src/helpers/types.ts
+++ b/packages/paths/src/helpers/types.ts
@@ -37,14 +37,6 @@ export type ReducesAbsoluteInstruction =
x: number;
y: number;
}
- | {
- type: 'H';
- x: number;
- }
- | {
- type: 'V';
- y: number;
- }
| {
type: 'C';
cp1x: number;
@@ -88,6 +80,14 @@ export type AbsoluteInstruction =
type: 'T';
x: number;
y: number;
+ }
+ | {
+ type: 'H';
+ x: number;
+ }
+ | {
+ type: 'V';
+ y: number;
};
export type Instruction =
diff --git a/packages/paths/src/simplify-instructions.ts b/packages/paths/src/simplify-instructions.ts
new file mode 100644
index 0000000000..338a0c586e
--- /dev/null
+++ b/packages/paths/src/simplify-instructions.ts
@@ -0,0 +1,12 @@
+import {removeATSHVInstructions} from './helpers/remove-a-s-t-curves';
+import type {AbsoluteInstruction, Instruction} from './helpers/types';
+import {normalizePath} from './normalize-path';
+import {parsePath} from './parse-path';
+import {serializeInstructions} from './serialize-instructions';
+
+export const simplifyInstructions = (instruction: Instruction[]) => {
+ const serialized = serializeInstructions(instruction);
+ const simplified = normalizePath(serialized);
+ const parsed = parsePath(simplified) as AbsoluteInstruction[];
+ return removeATSHVInstructions(parsed);
+};