🎥 Make videos programmatically with React
splitParts()
Jonny Burger5cb2621eca75
diff --git a/packages/docs/components/TableOfContents/paths.tsx b/packages/docs/components/TableOfContents/paths.tsxindex c00481f51b..e4c635e950 100644--- a/packages/docs/components/TableOfContents/paths.tsx+++ b/packages/docs/components/TableOfContents/paths.tsx@@ -11,7 +11,13 @@ export const TableOfContents: React.FC = () => {<div>Obtain length of an SVG path</div></TOCItem><TOCItem link="/docs/paths/get-parts">- <strong>getParts()</strong>+ <strong+ style={{+ textDecoration: "line-through",+ }}+ >+ getParts()+ </strong><div>Split SVG path into its parts</div></TOCItem><TOCItem link="/docs/paths/get-tangent-at-length">@@ -45,6 +51,10 @@ export const TableOfContents: React.FC = () => {<strong>extendViewBox()</strong><div>Widen an SVG viewBox in all directions</div></TOCItem>+ <TOCItem link="/docs/paths/split-parts">+ <strong>splitParts()</strong>+ <div>Split SVG path into its parts</div>+ </TOCItem></Grid></div>);diff --git a/packages/docs/docs/paths/get-parts.md b/packages/docs/docs/paths/get-parts.mdindex b97c61de24..a21573e330 100644--- a/packages/docs/docs/paths/get-parts.md+++ b/packages/docs/docs/paths/get-parts.md@@ -4,6 +4,10 @@ title: getParts()_Part of the [`@remotion/paths`](/docs/paths) package._+:::warning+**Deprecated** in favor of [`splitParts()`](/docs/paths/split-parts).+:::+Takes an SVG path and returns an array of parts of the path.Example of a path that has two straight lines:diff --git a/packages/docs/docs/paths/split-parts.md b/packages/docs/docs/paths/split-parts.mdnew file mode 100644index 0000000000..cfd0104978--- /dev/null+++ b/packages/docs/docs/paths/split-parts.md@@ -0,0 +1,41 @@+---+title: splitParts()+---++_Part of the [`@remotion/paths`](/docs/paths) package. Available from v3.3.6_++Takes an SVG path and returns an array of parts of the path.++Example of a path that has two straight lines:++```tsx twoslash+import { splitParts } from "@remotion/paths";++const parts = splitParts(`+ M 0 0 L 100 0+ M 0 100 L 200 100+`);+```++An array is returned containing two parts.++```tsx twoslash+import { splitParts } from "@remotion/paths";++const parts = splitParts(`+ M 0 0 L 100 0+ M 0 100 L 200 100+`);++// ---cut---++console.log(parts[0]); // "M 0 0 L 100 0"+console.log(parts[1]); // "M 0 100 L 200 100"+```++Paths containing relative `m` elements will be converted into `M` elements.++## See also++- [`@remotion/paths`](/docs/paths)+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/split-parts.ts)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex c2188cd832..68b80cc3e3 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -256,6 +256,7 @@ module.exports = {"paths/interpolate-path","paths/evolve-path","paths/extend-viewbox",+ "paths/split-parts",],},{diff --git a/packages/paths/src/get-parts.ts b/packages/paths/src/get-parts.tsindex 78356dc6fe..4b8cb4478c 100644--- a/packages/paths/src/get-parts.ts+++ b/packages/paths/src/get-parts.ts@@ -5,6 +5,7 @@ import type {Part} from './helpers/types';* Splits a valid SVG path into it's parts.* @param {string} path A valid SVG path* @link https://remotion.dev/docs/paths/get-parts+ * @deprecated In favor of splitParts()*/export const getParts = (path: string): Part[] => {const parts = [];diff --git a/packages/paths/src/helpers/construct.ts b/packages/paths/src/helpers/construct.tsindex 25b6351391..923d2dc371 100644--- a/packages/paths/src/helpers/construct.ts+++ b/packages/paths/src/helpers/construct.ts@@ -3,7 +3,8 @@import {makeArc} from './arc';import {makeBezier} from './bezier';import {makeLinearPosition} from './linear';-import parse from './parse';+import type {Instruction} from './parse';+import {parsePath} from './parse';import type {Point, PointArray, Properties} from './types';export const construct = (string: string) => {@@ -11,66 +12,81 @@ export const construct = (string: string) => {const partial_lengths: number[] = [];const functions: (null | Properties)[] = [];let initial_point: null | Point = null;- const parsed = parse(string);+ const parsed = parsePath(string);let cur: PointArray = [0, 0];let prev_point: PointArray = [0, 0];let curve: ReturnType<typeof makeBezier> | undefined;let ringStart: PointArray = [0, 0];++ const segments: Instruction[][] = [];+for (let i = 0; i < parsed.length; i++) {+ const instruction = parsed[i];++ if (instruction[0].toLowerCase() !== 'm' && segments.length > 0) {+ segments[segments.length - 1].push(instruction);+ }+// moveTo- if (parsed[i][0] === 'M') {- cur = [parsed[i][1], parsed[i][2]];+ if (instruction[0] === 'M') {+ cur = [instruction[1], instruction[2]];ringStart = [cur[0], cur[1]];+ segments.push([instruction]);functions.push(null);if (i === 0) {- initial_point = {x: parsed[i][1], y: parsed[i][2]};+ initial_point = {x: instruction[1], y: instruction[2]};}- } else if (parsed[i][0] === 'm') {- cur = [parsed[i][1] + cur[0], parsed[i][2] + cur[1]];+ } else if (instruction[0] === 'm') {+ cur = [instruction[1] + cur[0], instruction[2] + cur[1]];ringStart = [cur[0], cur[1]];+ segments.push([['M', cur[0], cur[1]]]);functions.push(null);// lineTo- } else if (parsed[i][0] === 'L') {+ } else if (instruction[0] === 'L') {length += Math.sqrt(- (cur[0] - parsed[i][1]) ** 2 + (cur[1] - parsed[i][2]) ** 2+ (cur[0] - instruction[1]) ** 2 + (cur[1] - instruction[2]) ** 2);functions.push(- makeLinearPosition(cur[0], parsed[i][1], cur[1], parsed[i][2])+ makeLinearPosition(cur[0], instruction[1], cur[1], instruction[2]));- cur = [parsed[i][1], parsed[i][2]];- } else if (parsed[i][0] === 'l') {- length += Math.sqrt(parsed[i][1] ** 2 + parsed[i][2] ** 2);+ cur = [instruction[1], instruction[2]];+ } else if (instruction[0] === 'l') {+ length += Math.sqrt(instruction[1] ** 2 + instruction[2] ** 2);functions.push(makeLinearPosition(cur[0],- parsed[i][1] + cur[0],+ instruction[1] + cur[0],cur[1],- parsed[i][2] + cur[1]+ instruction[2] + cur[1]));- cur = [parsed[i][1] + cur[0], parsed[i][2] + cur[1]];- } else if (parsed[i][0] === 'H') {- length += Math.abs(cur[0] - parsed[i][1]);- functions.push(makeLinearPosition(cur[0], parsed[i][1], cur[1], cur[1]));- cur[0] = parsed[i][1];- } else if (parsed[i][0] === 'h') {- length += Math.abs(parsed[i][1]);+ cur = [instruction[1] + cur[0], instruction[2] + cur[1]];+ } else if (instruction[0] === 'H') {+ length += Math.abs(cur[0] - instruction[1]);+ functions.push(+ makeLinearPosition(cur[0], instruction[1], cur[1], cur[1])+ );+ cur[0] = instruction[1];+ } else if (instruction[0] === 'h') {+ length += Math.abs(instruction[1]);+ functions.push(+ makeLinearPosition(cur[0], cur[0] + instruction[1], cur[1], cur[1])+ );+ cur[0] = instruction[1] + cur[0];+ } else if (instruction[0] === 'V') {+ length += Math.abs(cur[1] - instruction[1]);functions.push(- makeLinearPosition(cur[0], cur[0] + parsed[i][1], cur[1], cur[1])+ makeLinearPosition(cur[0], cur[0], cur[1], instruction[1]));- cur[0] = parsed[i][1] + cur[0];- } else if (parsed[i][0] === 'V') {- length += Math.abs(cur[1] - parsed[i][1]);- functions.push(makeLinearPosition(cur[0], cur[0], cur[1], parsed[i][1]));- cur[1] = parsed[i][1];- } else if (parsed[i][0] === 'v') {- length += Math.abs(parsed[i][1]);+ cur[1] = instruction[1];+ } else if (instruction[0] === 'v') {+ length += Math.abs(instruction[1]);functions.push(- makeLinearPosition(cur[0], cur[0], cur[1], cur[1] + parsed[i][1])+ makeLinearPosition(cur[0], cur[0], cur[1], cur[1] + instruction[1]));- cur[1] = parsed[i][1] + cur[1];+ cur[1] = instruction[1] + cur[1];// Close path- } else if (parsed[i][0] === 'z' || parsed[i][0] === 'Z') {+ } else if (instruction[0] === 'z' || instruction[0] === 'Z') {length += Math.sqrt((ringStart[0] - cur[0]) ** 2 + (ringStart[1] - cur[1]) ** 2);@@ -79,39 +95,39 @@ export const construct = (string: string) => {);cur = [ringStart[0], ringStart[1]];// Cubic Bezier curves- } else if (parsed[i][0] === 'C') {+ } else if (instruction[0] === 'C') {curve = makeBezier({ax: cur[0],ay: cur[1],- bx: parsed[i][1],- by: parsed[i][2],- cx: parsed[i][3],- cy: parsed[i][4],- dx: parsed[i][5],- dy: parsed[i][6],+ bx: instruction[1],+ by: instruction[2],+ cx: instruction[3],+ cy: instruction[4],+ dx: instruction[5],+ dy: instruction[6],});length += curve.getTotalLength();- cur = [parsed[i][5], parsed[i][6]];+ cur = [instruction[5], instruction[6]];functions.push(curve);- } else if (parsed[i][0] === 'c') {+ } else if (instruction[0] === 'c') {curve = makeBezier({ax: cur[0],ay: cur[1],- bx: cur[0] + parsed[i][1],- by: cur[1] + parsed[i][2],- cx: cur[0] + parsed[i][3],- cy: cur[1] + parsed[i][4],- dx: cur[0] + parsed[i][5],- dy: cur[1] + parsed[i][6],+ bx: cur[0] + instruction[1],+ by: cur[1] + instruction[2],+ cx: cur[0] + instruction[3],+ cy: cur[1] + instruction[4],+ dx: cur[0] + instruction[5],+ dy: cur[1] + instruction[6],});if (curve.getTotalLength() > 0) {length += curve.getTotalLength();functions.push(curve);- cur = [parsed[i][5] + cur[0], parsed[i][6] + cur[1]];+ cur = [instruction[5] + cur[0], instruction[6] + cur[1]];} else {functions.push(makeLinearPosition(cur[0], cur[0], cur[1], cur[1]));}- } else if (parsed[i][0] === 'S') {+ } else if (instruction[0] === 'S') {if (i > 0 && ['C', 'c', 'S', 's'].indexOf(parsed[i - 1][0]) > -1) {if (curve) {const c = curve.getC();@@ -120,10 +136,10 @@ export const construct = (string: string) => {ay: cur[1],bx: 2 * cur[0] - c.x,by: 2 * cur[1] - c.y,- cx: parsed[i][1],- cy: parsed[i][2],- dx: parsed[i][3],- dy: parsed[i][4],+ cx: instruction[1],+ cy: instruction[2],+ dx: instruction[3],+ dy: instruction[4],});}} else {@@ -132,19 +148,19 @@ export const construct = (string: string) => {ay: cur[1],bx: cur[0],by: cur[1],- cx: parsed[i][1],- cy: parsed[i][2],- dx: parsed[i][3],- dy: parsed[i][4],+ cx: instruction[1],+ cy: instruction[2],+ dx: instruction[3],+ dy: instruction[4],});}if (curve) {length += curve.getTotalLength();- cur = [parsed[i][3], parsed[i][4]];+ cur = [instruction[3], instruction[4]];functions.push(curve);}- } else if (parsed[i][0] === 's') {+ } else if (instruction[0] === 's') {// 240 225if (i > 0 && ['C', 'c', 'S', 's'].indexOf(parsed[i - 1][0]) > -1) {if (curve) {@@ -155,10 +171,10 @@ export const construct = (string: string) => {ay: cur[1],bx: cur[0] + d.x - c.x,by: cur[1] + d.y - c.y,- cx: cur[0] + parsed[i][1],- cy: cur[1] + parsed[i][2],- dx: cur[0] + parsed[i][3],- dy: cur[1] + parsed[i][4],+ cx: cur[0] + instruction[1],+ cy: cur[1] + instruction[2],+ dx: cur[0] + instruction[3],+ dy: cur[1] + instruction[4],});}} else {@@ -167,27 +183,27 @@ export const construct = (string: string) => {ay: cur[1],bx: cur[0],by: cur[1],- cx: cur[0] + parsed[i][1],- cy: cur[1] + parsed[i][2],- dx: cur[0] + parsed[i][3],- dy: cur[1] + parsed[i][4],+ cx: cur[0] + instruction[1],+ cy: cur[1] + instruction[2],+ dx: cur[0] + instruction[3],+ dy: cur[1] + instruction[4],});}if (curve) {length += curve.getTotalLength();- cur = [parsed[i][3] + cur[0], parsed[i][4] + cur[1]];+ cur = [instruction[3] + cur[0], instruction[4] + cur[1]];functions.push(curve);}}// Quadratic Bezier curves- else if (parsed[i][0] === 'Q') {- if (cur[0] === parsed[i][1] && cur[1] === parsed[i][2]) {+ else if (instruction[0] === 'Q') {+ if (cur[0] === instruction[1] && cur[1] === instruction[2]) {const linearCurve = makeLinearPosition(- parsed[i][1],- parsed[i][3],- parsed[i][2],- parsed[i][4]+ instruction[1],+ instruction[3],+ instruction[2],+ instruction[4]);length += linearCurve.getTotalLength();functions.push(linearCurve);@@ -195,10 +211,10 @@ export const construct = (string: string) => {curve = makeBezier({ax: cur[0],ay: cur[1],- bx: parsed[i][1],- by: parsed[i][2],- cx: parsed[i][3],- cy: parsed[i][4],+ bx: instruction[1],+ by: instruction[2],+ cx: instruction[3],+ cy: instruction[4],dx: null,dy: null,});@@ -206,15 +222,15 @@ export const construct = (string: string) => {functions.push(curve);}- cur = [parsed[i][3], parsed[i][4]];- prev_point = [parsed[i][1], parsed[i][2]];- } else if (parsed[i][0] === 'q') {- if (parsed[i][1] === 0 && parsed[i][2] === 0) {+ cur = [instruction[3], instruction[4]];+ prev_point = [instruction[1], instruction[2]];+ } else if (instruction[0] === 'q') {+ if (instruction[1] === 0 && instruction[2] === 0) {const linearCurve = makeLinearPosition(- cur[0] + parsed[i][1],- cur[0] + parsed[i][3],- cur[1] + parsed[i][2],- cur[1] + parsed[i][4]+ cur[0] + instruction[1],+ cur[0] + instruction[3],+ cur[1] + instruction[2],+ cur[1] + instruction[4]);length += linearCurve.getTotalLength();functions.push(linearCurve);@@ -222,10 +238,10 @@ export const construct = (string: string) => {curve = makeBezier({ax: cur[0],ay: cur[1],- bx: cur[0] + parsed[i][1],- by: cur[1] + parsed[i][2],- cx: cur[0] + parsed[i][3],- cy: cur[1] + parsed[i][4],+ bx: cur[0] + instruction[1],+ by: cur[1] + instruction[2],+ cx: cur[0] + instruction[3],+ cy: cur[1] + instruction[4],dx: null,dy: null,});@@ -233,17 +249,17 @@ export const construct = (string: string) => {functions.push(curve);}- prev_point = [cur[0] + parsed[i][1], cur[1] + parsed[i][2]];- cur = [parsed[i][3] + cur[0], parsed[i][4] + cur[1]];- } else if (parsed[i][0] === 'T') {+ prev_point = [cur[0] + instruction[1], cur[1] + instruction[2]];+ cur = [instruction[3] + cur[0], instruction[4] + cur[1]];+ } else if (instruction[0] === 'T') {if (i > 0 && ['Q', 'q', 'T', 't'].indexOf(parsed[i - 1][0]) > -1) {curve = makeBezier({ax: cur[0],ay: cur[1],bx: 2 * cur[0] - prev_point[0],by: 2 * cur[1] - prev_point[1],- cx: parsed[i][1],- cy: parsed[i][2],+ cx: instruction[1],+ cy: instruction[2],dx: null,dy: null,});@@ -252,25 +268,25 @@ export const construct = (string: string) => {} else {const linearCurve = makeLinearPosition(cur[0],- parsed[i][1],+ instruction[1],cur[1],- parsed[i][2]+ instruction[2]);functions.push(linearCurve);length += linearCurve.getTotalLength();}prev_point = [2 * cur[0] - prev_point[0], 2 * cur[1] - prev_point[1]];- cur = [parsed[i][1], parsed[i][2]];- } else if (parsed[i][0] === 't') {+ cur = [instruction[1], instruction[2]];+ } else if (instruction[0] === 't') {if (i > 0 && ['Q', 'q', 'T', 't'].indexOf(parsed[i - 1][0]) > -1) {curve = makeBezier({ax: cur[0],ay: cur[1],bx: 2 * cur[0] - prev_point[0],by: 2 * cur[1] - prev_point[1],- cx: cur[0] + parsed[i][1],- cy: cur[1] + parsed[i][2],+ cx: cur[0] + instruction[1],+ cy: cur[1] + instruction[2],dx: null,dy: null,});@@ -279,52 +295,58 @@ export const construct = (string: string) => {} else {const linearCurve = makeLinearPosition(cur[0],- cur[0] + parsed[i][1],+ cur[0] + instruction[1],cur[1],- cur[1] + parsed[i][2]+ cur[1] + instruction[2]);length += linearCurve.getTotalLength();functions.push(linearCurve);}prev_point = [2 * cur[0] - prev_point[0], 2 * cur[1] - prev_point[1]];- cur = [parsed[i][1] + cur[0], parsed[i][2] + cur[1]];- } else if (parsed[i][0] === 'A') {+ cur = [instruction[1] + cur[0], instruction[2] + cur[1]];+ } else if (instruction[0] === 'A') {const arcCurve = makeArc({x0: cur[0],y0: cur[1],- rx: parsed[i][1],- ry: parsed[i][2],- xAxisRotate: parsed[i][3],- LargeArcFlag: parsed[i][4] === 1,- SweepFlag: parsed[i][5] === 1,- x1: parsed[i][6],- y1: parsed[i][7],+ rx: instruction[1],+ ry: instruction[2],+ xAxisRotate: instruction[3],+ LargeArcFlag: instruction[4] === 1,+ SweepFlag: instruction[5] === 1,+ x1: instruction[6],+ y1: instruction[7],});length += arcCurve.getTotalLength();- cur = [parsed[i][6], parsed[i][7]];+ cur = [instruction[6], instruction[7]];functions.push(arcCurve);- } else if (parsed[i][0] === 'a') {+ } else if (instruction[0] === 'a') {const arcCurve = makeArc({x0: cur[0],y0: cur[1],- rx: parsed[i][1],- ry: parsed[i][2],- xAxisRotate: parsed[i][3],- LargeArcFlag: parsed[i][4] === 1,- SweepFlag: parsed[i][5] === 1,- x1: cur[0] + parsed[i][6],- y1: cur[1] + parsed[i][7],+ rx: instruction[1],+ ry: instruction[2],+ xAxisRotate: instruction[3],+ LargeArcFlag: instruction[4] === 1,+ SweepFlag: instruction[5] === 1,+ x1: cur[0] + instruction[6],+ y1: cur[1] + instruction[7],});length += arcCurve.getTotalLength();- cur = [cur[0] + parsed[i][6], cur[1] + parsed[i][7]];+ cur = [cur[0] + instruction[6], cur[1] + instruction[7]];functions.push(arcCurve);}partial_lengths.push(length);}- return {initial_point, length, partial_lengths, functions};+ return {+ segments,+ initial_point,+ length,+ partial_lengths,+ functions,+ };};diff --git a/packages/paths/src/helpers/parse.ts b/packages/paths/src/helpers/parse.tsindex 8f1c8ec9a4..195d553251 100644--- a/packages/paths/src/helpers/parse.ts+++ b/packages/paths/src/helpers/parse.ts@@ -17,7 +17,9 @@ const length: {[key in pathOrders]: number} = {const segmentRegExp = /([astvzqmhlc])([^astvzqmhlc]*)/gi;const numberRegExp = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;-export default (path: string) => {+export type Instruction = [string, ...number[]];++export const parsePath = (path: string): Instruction[] => {const segments = (path && path.length > 0 ? path : 'M0,0').match(segmentRegExp);diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.tsindex d5a56fb140..65f0ab70ba 100644--- a/packages/paths/src/index.ts+++ b/packages/paths/src/index.ts@@ -8,3 +8,4 @@ export {Part} from './helpers/types';export {interpolatePath} from './interpolate-path';export {normalizePath} from './normalize-path';export {reversePath} from './reverse-path';+export {splitParts} from './split-parts';diff --git a/packages/paths/src/split-parts.ts b/packages/paths/src/split-parts.tsnew file mode 100644index 0000000000..53dc5a9433--- /dev/null+++ b/packages/paths/src/split-parts.ts@@ -0,0 +1,16 @@+import {construct} from './helpers/construct';++/**+ * Splits a valid SVG path into it's parts.+ * @param {string} path A valid SVG path+ * @link https://remotion.dev/docs/paths/split-parts+ */+export const splitParts = (path: string): string[] => {+ const {segments} = construct(path);++ return segments+ .map((seg) => {+ return seg.map((s) => s.join(' '));+ })+ .map((_s) => _s.join(' '));+};diff --git a/packages/paths/src/test/get-parts.test.ts b/packages/paths/src/test/get-parts.test.tsindex 401a5eec81..ea92621d7f 100644--- a/packages/paths/src/test/get-parts.test.ts+++ b/packages/paths/src/test/get-parts.test.ts@@ -4,7 +4,7 @@ import {getParts} from '../get-parts';test('Should be able to get parts of a path', () => {const parts = getParts(`M 0 0 L 100 0- M 0 100 L 100 100+ M 0 100 L 100 100 L 200 200`);expect(parts[0].length).toEqual(100);expect(parts[1].length).toEqual(100);diff --git a/packages/paths/src/test/split-parts.test.ts b/packages/paths/src/test/split-parts.test.tsnew file mode 100644index 0000000000..a81b3097d4--- /dev/null+++ b/packages/paths/src/test/split-parts.test.ts@@ -0,0 +1,14 @@+import {expect, test} from 'vitest';+import {getLength} from '../get-length';+import {splitParts} from '../split-parts';++test('Should be able to get parts of a path', () => {+ const parts = splitParts(`+ M 0 0 L 100 0+ m 0 100 L 100 100 L 100 200+ `);++ expect(parts).toEqual(['M 0 0 L 100 0', 'M 100 100 L 100 100 L 100 200']);++ expect(parts.map((p) => getLength(p))).toEqual([100, 100]);+});