Skip to content

🎥 Make videos programmatically with React

refactor

Jonny Burgerc8aeea7805da
diff --git a/packages/paths/src/get-length.ts b/packages/paths/src/get-length.ts
index 7b2b56bf10..4294f2cb97 100644
--- a/packages/paths/src/get-length.ts
+++ b/packages/paths/src/get-length.ts
@@ -1,5 +1,7 @@
-import {construct} from './construct';
-import type {PartProperties} from './types';
+// Copied from: https://github.com/rveciana/svg-path-properties
+
+import {construct} from './helpers/construct';
+import type {PartProperties} from './helpers/types';
export const getLength = (p: string) => {
const constructucted = construct(p);
diff --git a/packages/paths/src/arc.ts b/packages/paths/src/helpers/arc.ts
similarity index 98%
rename from packages/paths/src/arc.ts
rename to packages/paths/src/helpers/arc.ts
index 9d1061ab65..f834f41b86 100644
--- a/packages/paths/src/arc.ts
+++ b/packages/paths/src/helpers/arc.ts
@@ -1,4 +1,5 @@
-import type {Point, PointProperties, Properties} from './types';
+// Copied from: https://github.com/rveciana/svg-path-properties
+import type {Point, PointProperties, Properties} from '../types';
export const makeArc = ({
x0,
diff --git a/packages/paths/src/bezier-functions.ts b/packages/paths/src/helpers/bezier-functions.ts
similarity index 97%
rename from packages/paths/src/bezier-functions.ts
rename to packages/paths/src/helpers/bezier-functions.ts
index b939d538f5..ac915932a9 100644
--- a/packages/paths/src/bezier-functions.ts
+++ b/packages/paths/src/helpers/bezier-functions.ts
@@ -27,10 +27,6 @@ export const cubicDerivative = (xs: number[], ys: number[], t: number) => {
export const getCubicArcLength = (xs: number[], ys: number[], t: number) => {
let correctedT: number;
- /* if (xs.length >= tValues.length) {
- throw new Error('too high n bezier');
- } */
-
const n = 20;
const z = t / 2;
diff --git a/packages/paths/src/bezier-values.ts b/packages/paths/src/helpers/bezier-values.ts
similarity index 99%
rename from packages/paths/src/bezier-values.ts
rename to packages/paths/src/helpers/bezier-values.ts
index 9357d157c5..4297c31caf 100644
--- a/packages/paths/src/bezier-values.ts
+++ b/packages/paths/src/helpers/bezier-values.ts
@@ -1,3 +1,5 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
+
/* eslint-disable @typescript-eslint/no-loss-of-precision */
/* eslint-disable no-loss-of-precision */
// Legendre-Gauss abscissae (xi values, defined at i=n as the roots of the nth order Legendre polynomial Pn(x))
diff --git a/packages/paths/src/bezier.ts b/packages/paths/src/helpers/bezier.ts
similarity index 97%
rename from packages/paths/src/bezier.ts
rename to packages/paths/src/helpers/bezier.ts
index ee2e31462f..b49caf0f41 100644
--- a/packages/paths/src/bezier.ts
+++ b/packages/paths/src/helpers/bezier.ts
@@ -1,3 +1,5 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
+
import type {Point} from './types';
import {
diff --git a/packages/paths/src/construct.ts b/packages/paths/src/helpers/construct.ts
similarity index 99%
rename from packages/paths/src/construct.ts
rename to packages/paths/src/helpers/construct.ts
index 08dd77b4b5..25b6351391 100644
--- a/packages/paths/src/construct.ts
+++ b/packages/paths/src/helpers/construct.ts
@@ -1,3 +1,5 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
+
import {makeArc} from './arc';
import {makeBezier} from './bezier';
import {makeLinearPosition} from './linear';
diff --git a/packages/paths/src/linear.ts b/packages/paths/src/helpers/linear.ts
similarity index 94%
rename from packages/paths/src/linear.ts
rename to packages/paths/src/helpers/linear.ts
index 8e0c87403f..6638ec2ff8 100644
--- a/packages/paths/src/linear.ts
+++ b/packages/paths/src/helpers/linear.ts
@@ -1,3 +1,4 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
import type {Point, PointProperties} from './types';
export const makeLinearPosition = (
diff --git a/packages/paths/src/helpers/parse.ts b/packages/paths/src/helpers/parse.ts
new file mode 100644
index 0000000000..8f1c8ec9a4
--- /dev/null
+++ b/packages/paths/src/helpers/parse.ts
@@ -0,0 +1,65 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
+
+import type {pathOrders} from './types';
+
+const length: {[key in pathOrders]: number} = {
+ a: 7,
+ c: 6,
+ h: 1,
+ l: 2,
+ m: 2,
+ q: 4,
+ s: 4,
+ t: 2,
+ v: 1,
+ z: 0,
+};
+const segmentRegExp = /([astvzqmhlc])([^astvzqmhlc]*)/gi;
+const numberRegExp = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;
+
+export default (path: string) => {
+ const segments = (path && path.length > 0 ? path : 'M0,0').match(
+ segmentRegExp
+ );
+ if (!segments) {
+ throw new Error(`No path elements found in string ${path}`);
+ }
+
+ return segments.reduce(
+ (segmentsArray: [string, ...Array<number>][], segmentString: string) => {
+ let command = segmentString.charAt(0);
+ let type: pathOrders = command.toLowerCase() as pathOrders;
+ const args = parseValues(segmentString.substr(1));
+
+ // overloaded moveTo
+ if (type === 'm' && args.length > 2) {
+ segmentsArray.push([command, ...args.splice(0, 2)]);
+ type = 'l';
+ command = command === 'm' ? 'l' : 'L';
+ }
+
+ while (args.length >= 0) {
+ if (args.length === length[type]) {
+ segmentsArray.push([command, ...args.splice(0, length[type])]);
+ break;
+ }
+
+ if (args.length < length[type]) {
+ throw new Error(
+ `Malformed path data: "${command}" must have ${length[type]} elements and has ${args.length}: ${segmentString}`
+ );
+ }
+
+ segmentsArray.push([command, ...args.splice(0, length[type])]);
+ }
+
+ return segmentsArray;
+ },
+ []
+ );
+};
+
+const parseValues = (args: string) => {
+ const numbers = args.match(numberRegExp);
+ return numbers ? numbers.map(Number) : [];
+};
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
new file mode 100644
index 0000000000..e048565948
--- /dev/null
+++ b/packages/paths/src/helpers/types.ts
@@ -0,0 +1,41 @@
+// Copied from: https://github.com/rveciana/svg-path-properties
+
+export interface Properties {
+ getTotalLength(): number;
+ getPointAtLength(pos: number): Point;
+ getTangentAtLength(pos: number): Point;
+ getPropertiesAtLength(pos: number): PointProperties;
+}
+
+export interface PartProperties {
+ start: Point;
+ end: Point;
+ length: number;
+ getPointAtLength(pos: number): Point;
+ getTangentAtLength(pos: number): Point;
+ getPropertiesAtLength(pos: number): PointProperties;
+}
+export interface Point {
+ x: number;
+ y: number;
+}
+export type PointArray = [number, number];
+
+export interface PointProperties {
+ x: number;
+ y: number;
+ tangentX: number;
+ tangentY: number;
+}
+
+export type pathOrders =
+ | 'a'
+ | 'c'
+ | 'h'
+ | 'l'
+ | 'm'
+ | 'q'
+ | 's'
+ | 't'
+ | 'v'
+ | 'z';
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
new file mode 100644
index 0000000000..d40d2b6a91
--- /dev/null
+++ b/packages/paths/src/index.ts
@@ -0,0 +1 @@
+export {getLength} from './get-length';
diff --git a/packages/paths/src/parse.ts b/packages/paths/src/parse.ts
deleted file mode 100644
index 73a93d584d..0000000000
--- a/packages/paths/src/parse.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-import type { pathOrders } from "./types";
-
-const length: { [key in pathOrders]: number } = {
- a: 7,
- c: 6,
- h: 1,
- l: 2,
- m: 2,
- q: 4,
- s: 4,
- t: 2,
- v: 1,
- z: 0
-};
-const segmentRegExp = /([astvzqmhlc])([^astvzqmhlc]*)/gi;
-const numberRegExp = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;
-
-export default (path: string) => {
- const segments = (path && path.length > 0 ? path : "M0,0").match(segmentRegExp);
- if (!segments) {
- throw new Error(`No path elements found in string ${path}`);
- }
-
- return segments.reduce((segmentsArray: [string, ...Array<number>][], segmentString: string) => {
- let command = segmentString.charAt(0);
- let type: pathOrders = command.toLowerCase() as pathOrders;
- const args = parseValues(segmentString.substr(1));
-
- // overloaded moveTo
- if (type === "m" && args.length > 2) {
- segmentsArray.push([command, ...args.splice(0, 2)]);
- type = "l";
- command = command === "m" ? "l" : "L";
- }
-
- while (args.length >= 0) {
- if (args.length === length[type]) {
- segmentsArray.push([command, ...args.splice(0, length[type])]);
- break;
- }
-
- if (args.length < length[type]) {
- throw new Error(
- `Malformed path data: "${command}" must have ${length[type]} elements and has ${args.length}: ${segmentString}`
- );
- }
-
- segmentsArray.push([command, ...args.splice(0, length[type])]);
- }
-
- return segmentsArray;
- }, []);
-};
-
-const parseValues = (args: string) => {
- const numbers = args.match(numberRegExp);
- return numbers ? numbers.map(Number) : [];
-};
diff --git a/packages/paths/src/types.ts b/packages/paths/src/types.ts
deleted file mode 100644
index e6e13bcb97..0000000000
--- a/packages/paths/src/types.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-export interface Properties {
- getTotalLength(): number;
- getPointAtLength(pos: number): Point;
- getTangentAtLength(pos: number): Point;
- getPropertiesAtLength(pos: number): PointProperties;
-}
-
-export interface PartProperties {
- start: Point;
- end: Point;
- length: number;
- getPointAtLength(pos: number): Point;
- getTangentAtLength(pos: number): Point;
- getPropertiesAtLength(pos: number): PointProperties;
-}
-export interface Point {
- x: number;
- y: number;
-}
-export type PointArray = [number, number];
-
-export interface PointProperties {
- x: number;
- y: number;
- tangentX: number;
- tangentY: number;
-}
-
-export type pathOrders =
- | "a"
- | "c"
- | "h"
- | "l"
- | "m"
- | "q"
- | "s"
- | "t"
- | "v"
- | "z";