Skip to content

🎥 Make videos programmatically with React

add some docs

Jonny Burgerb1aa980957c9
diff --git a/packages/docs/docs/paths/get-length.md b/packages/docs/docs/paths/get-length.md
new file mode 100644
index 0000000000..523004a569
--- /dev/null
+++ b/packages/docs/docs/paths/get-length.md
@@ -0,0 +1,24 @@
+---
+title: getLength()
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+Gets the length of an SVG path. The length must
+
+A number is returned if the path is valid:
+
+```tsx twoslash
+import { getLength } from "@remotion/paths";
+
+const length = getLength("M 0 0 L 100 0");
+console.log(length); // 100
+```
+
+The function will throw if the path is invalid:
+
+```tsx twoslash
+import { getLength } from "@remotion/paths";
+// ---cut---
+getLength("remotion"); // Error: Malformed path data: ...
+```
diff --git a/packages/docs/docs/paths/get-parts.md b/packages/docs/docs/paths/get-parts.md
new file mode 100644
index 0000000000..706a0570cc
--- /dev/null
+++ b/packages/docs/docs/paths/get-parts.md
@@ -0,0 +1,117 @@
+---
+title: getParts()
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+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 { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+```
+
+An array is returned containing two parts.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+// ---cut---
+
+console.log(parts[0].length); // 100
+console.log(parts[1].length); // 200
+```
+
+## Properties of a part
+
+### `length`
+
+Returns the length of the part.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+// ---cut---
+console.log(parts[0].length); // 100
+```
+
+### `start`
+
+Returns the `x` and `y` coordinates of the start point of the part.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+console.log(parts[1].length); // 200
+// ---cut---
+console.log(parts[0].start); // { x: 0, y: 0 }
+```
+
+### `end`
+
+Returns the `x` and `y` coordinates of the end point of the part.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+// ---cut---
+console.log(parts[0].end); // { x: 100, y: 0 }
+```
+
+### `getPointAtLength()`
+
+Returns the `x` and `y` coordinates of a point along the line of a part. The input must be between `0` and `length`.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+// ---cut---
+console.log(parts[0].getPointAtLength(50)); // { x: 50, y: 0 }
+```
+
+### `getTangentAtLength()`
+
+Returns `tangentX` and `tangentY` of a point along the line of a part. The input must be between `0` and `length`.
+
+```tsx twoslash
+import { getParts } from "@remotion/paths";
+
+const parts = getParts(`
+ M 0 0 L 100 0
+ M 0 100 L 200 100
+`);
+
+// ---cut---
+console.log(parts[0].getTangentAtLength(50)); // { tangentX: 1, tangentY: 0 }
+```
diff --git a/packages/docs/docs/paths/get-point-at-length.md b/packages/docs/docs/paths/get-point-at-length.md
new file mode 100644
index 0000000000..fd24cbf2f8
--- /dev/null
+++ b/packages/docs/docs/paths/get-point-at-length.md
@@ -0,0 +1,25 @@
+---
+title: getPointAtLength()
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+Gets the coordinates of a point which is on an SVG path.
+The first argument is an SVG path, the second one is the at which length the point should be sampled. It must be between `0` and the return value of [`getLength()`](/docs/paths/get-length).
+
+A number is returned if the path is valid:
+
+```tsx twoslash
+import { getPointAtLength } from "@remotion/paths";
+
+const point = getPointAtLength("M 0 0 L 100 0", 50);
+console.log(length); // { x: 50, y: 0 }
+```
+
+The function will throw if the path is invalid:
+
+```tsx twoslash
+import { getPointAtLength } from "@remotion/paths";
+// ---cut---
+getPointAtLength("remotion", 50); // Error: Malformed path data: ...
+```
diff --git a/packages/docs/docs/paths/index.md b/packages/docs/docs/paths/index.md
new file mode 100644
index 0000000000..1a319b2873
--- /dev/null
+++ b/packages/docs/docs/paths/index.md
@@ -0,0 +1,48 @@
+---
+title: "@remotion/paths"
+---
+
+A package providing utility functions for dealing with SVG paths. This package consists of code from [`svg-path-properties`](https://www.npmjs.com/package/svg-path-properties) and [`svg-path-reverse`](https://github.com/Pomax/svg-path-reverse#readme) with the following improvements:
+
+- Functional style APIs
+- First class Typescript types
+- Documentation with examples
+- ESM import style
+
+No dependencies are needed, meaning this package can be used independently of Remotion. Its license is MIT.
+
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
+<Tabs
+defaultValue="npm"
+values={[
+{ label: 'npm', value: 'npm', },
+{ label: 'pnpm', value: 'pnpm', },
+{ label: 'yarn', value: 'yarn', },
+]
+}>
+<TabItem value="npm">
+
+```bash
+npm i @remotion/paths
+```
+
+ </TabItem>
+
+ <TabItem value="pnpm">
+
+```bash
+pnpm i @remotion/paths
+```
+
+ </TabItem>
+
+ <TabItem value="yarn">
+
+```bash
+yarn add @remotion/paths
+```
+
+ </TabItem>
+</Tabs>
diff --git a/packages/docs/package.json b/packages/docs/package.json
index b3c905a264..60fa008fa7 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -30,6 +30,7 @@
"@remotion/lottie": "3.2.9",
"@remotion/media-utils": "3.2.9",
"@remotion/player": "3.2.9",
+ "@remotion/paths": "3.2.9",
"@remotion/preload": "3.2.9",
"@remotion/renderer": "3.2.9",
"@remotion/skia": "3.2.9",
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 97b9c4af77..7d0edf4ba9 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -275,6 +275,19 @@ module.exports = {
"renderer/make-cancel-signal",
],
},
+ {
+ type: "category",
+ label: "API - @remotion/paths",
+ link: {
+ type: "doc",
+ id: "paths/index",
+ },
+ items: [
+ "paths/get-length",
+ "paths/get-parts",
+ "paths/get-point-at-length",
+ ],
+ },
{
type: "category",
label: "Troubleshooting",
diff --git a/packages/paths/.npmignore b/packages/paths/.npmignore
index 6fdeec6a7f..2489b53382 100644
--- a/packages/paths/.npmignore
+++ b/packages/paths/.npmignore
@@ -3,6 +3,5 @@ src
.eslintrc
tsconfig.tsbuildinfo
.prettierrc
-jest.config.js
*.tgz
.turbo
diff --git a/packages/paths/package.json b/packages/paths/package.json
index d226919d25..2380c515e5 100644
--- a/packages/paths/package.json
+++ b/packages/paths/package.json
@@ -7,7 +7,8 @@
"scripts": {
"lint": "eslint src --ext ts,tsx",
"watch": "tsc -w",
- "build": "tsc -d"
+ "build": "tsc -d",
+ "test": "vitest --run"
},
"author": "",
"license": "MIT",
@@ -23,11 +24,12 @@
"eslint": "8.13.0",
"prettier": "^2.0.5",
"prettier-plugin-organize-imports": "^2.3.4",
- "typescript": "^4.7.0"
+ "typescript": "^4.7.0",
+ "vitest": "^0.18.0"
},
"keywords": [
"svg",
- "paths",
+ "path",
"utilities"
],
"publishConfig": {
diff --git a/packages/paths/src/get-parts.ts b/packages/paths/src/get-parts.ts
index 3bac5bc1ac..0a94068125 100644
--- a/packages/paths/src/get-parts.ts
+++ b/packages/paths/src/get-parts.ts
@@ -1,17 +1,18 @@
import {construct} from './helpers/construct';
-import type {PartProperties} from './helpers/types';
+import type {Part} from './helpers/types';
-export const getParts = (p: string) => {
+export const getParts = (p: string): Part[] => {
const parts = [];
const constructed = construct(p);
let i = 0;
for (const fn of constructed.functions) {
if (!fn) {
+ i++;
continue;
}
- const properties: PartProperties = {
+ const properties: Part = {
start: fn.getPointAtLength(0),
end: fn.getPointAtLength(
constructed.partial_lengths[i] - constructed.partial_lengths[i - 1]
@@ -20,7 +21,6 @@ export const getParts = (p: string) => {
constructed.partial_lengths[i] - constructed.partial_lengths[i - 1],
getPointAtLength: fn.getPointAtLength,
getTangentAtLength: fn.getTangentAtLength,
- getPropertiesAtLength: fn.getPropertiesAtLength,
};
i++;
parts.push(properties);
diff --git a/packages/paths/src/get-properties-at-length.ts b/packages/paths/src/get-properties-at-length.ts
deleted file mode 100644
index 083af446b3..0000000000
--- a/packages/paths/src/get-properties-at-length.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import {construct} from './helpers/construct';
-import {getPartAtLength} from './helpers/get-part-at-length';
-
-export const getPropertiesAtLength = (p: string, fractionLength: number) => {
- const constructed = construct(p);
-
- const fractionPart = getPartAtLength(p, fractionLength);
- const functionAtPart = constructed.functions[fractionPart.i];
- if (functionAtPart) {
- return functionAtPart.getPropertiesAtLength(fractionPart.fraction);
- }
-
- if (constructed.initial_point) {
- return {
- x: constructed.initial_point.x,
- y: constructed.initial_point.y,
- tangentX: 0,
- tangentY: 0,
- };
- }
-
- throw new Error('Wrong function at this part.');
-};
diff --git a/packages/paths/src/get-reversed-path.ts b/packages/paths/src/get-reversed-path.ts
index 9904ca0e5f..7d0370873b 100644
--- a/packages/paths/src/get-reversed-path.ts
+++ b/packages/paths/src/get-reversed-path.ts
@@ -141,7 +141,7 @@ function reverseNormalizedPath(normalized: string) {
* make use of, because it lets you reverse individual
* subpaths in some <path> "d" attribute.
*/
-export const getReversedPath = (_path: string, _subpath?: number) => {
+export const reversePath = (_path: string, _subpath?: number) => {
const subpath = _subpath ?? false;
const path = normalizePath(_path);
let paths = path.replace(/M/g, '|M').split('|');
diff --git a/packages/paths/src/helpers/arc.ts b/packages/paths/src/helpers/arc.ts
index 65f6642d97..5f4beaeee0 100644
--- a/packages/paths/src/helpers/arc.ts
+++ b/packages/paths/src/helpers/arc.ts
@@ -1,6 +1,6 @@
// Copied from: https://github.com/rveciana/svg-path-properties
-import type {Point, PointProperties, Properties} from './types';
+import type {Point, Properties} from './types';
export const makeArc = ({
x0,
@@ -96,15 +96,8 @@ export const makeArc = ({
return {x: xDist / dist, y: yDist / dist};
};
- const getPropertiesAtLength = (fractionLength: number): PointProperties => {
- const tangent = getTangentAtLength(fractionLength);
- const point = getPointAtLength(fractionLength);
- return {x: point.x, y: point.y, tangentX: tangent.x, tangentY: tangent.y};
- };
-
return {
getPointAtLength,
- getPropertiesAtLength,
getTangentAtLength,
getTotalLength,
};
diff --git a/packages/paths/src/helpers/linear.ts b/packages/paths/src/helpers/linear.ts
index 6638ec2ff8..8ce4820390 100644
--- a/packages/paths/src/helpers/linear.ts
+++ b/packages/paths/src/helpers/linear.ts
@@ -26,10 +26,9 @@ export const makeLinearPosition = (
return {x: (x1 - x0) / module, y: (y1 - y0) / module};
};
- const getPropertiesAtLength = (pos: number): PointProperties => {
- const point = getPointAtLength(pos);
+ const getPropertiesAtLength = (): PointProperties => {
const tangent = getTangentAtLength();
- return {x: point.x, y: point.y, tangentX: tangent.x, tangentY: tangent.y};
+ return {tangentX: tangent.x, tangentY: tangent.y};
};
return {
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
index e048565948..0cdd9e9462 100644
--- a/packages/paths/src/helpers/types.ts
+++ b/packages/paths/src/helpers/types.ts
@@ -4,16 +4,14 @@ export interface Properties {
getTotalLength(): number;
getPointAtLength(pos: number): Point;
getTangentAtLength(pos: number): Point;
- getPropertiesAtLength(pos: number): PointProperties;
}
-export interface PartProperties {
+export interface Part {
start: Point;
end: Point;
length: number;
getPointAtLength(pos: number): Point;
getTangentAtLength(pos: number): Point;
- getPropertiesAtLength(pos: number): PointProperties;
}
export interface Point {
x: number;
@@ -22,8 +20,6 @@ export interface Point {
export type PointArray = [number, number];
export interface PointProperties {
- x: number;
- y: number;
tangentX: number;
tangentY: number;
}
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 472da041ac..eb3781c21a 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,7 +1,7 @@
export {getLength} from './get-length';
export {getParts} from './get-parts';
export {getPointAtLength} from './get-point-at-length';
-export {getPropertiesAtLength} from './get-properties-at-length';
-export {getReversedPath} from './get-reversed-path';
+export {reversePath} from './get-reversed-path';
export {getTangentAtLength} from './get-tangent-at-length';
+export {Part} from './helpers/types';
export {normalizePath} from './normalize-path';
diff --git a/packages/paths/src/test/get-length.test.ts b/packages/paths/src/test/get-length.test.ts
new file mode 100644
index 0000000000..6b2701a4ff
--- /dev/null
+++ b/packages/paths/src/test/get-length.test.ts
@@ -0,0 +1,10 @@
+import {expect, test} from 'vitest';
+import {getLength} from '../get-length';
+
+test('Should be able to get length of a path', () => {
+ expect(getLength('M 0 0 L 100 0')).toBe(100);
+});
+
+test('Should throw error if path is invalid', () => {
+ expect(() => getLength('remotion')).toThrow(/Malformed path data/);
+});
diff --git a/packages/paths/src/test/get-parts.test.ts b/packages/paths/src/test/get-parts.test.ts
new file mode 100644
index 0000000000..d4e325c443
--- /dev/null
+++ b/packages/paths/src/test/get-parts.test.ts
@@ -0,0 +1,14 @@
+import {expect, test} from 'vitest';
+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
+ `);
+ expect(parts[0].length).toEqual(100);
+ expect(parts[1].length).toEqual(100);
+ expect(parts[0].getPointAtLength(100).x).toEqual(100);
+
+ console.log(parts[0].getTangentAtLength(50));
+});
diff --git a/packages/paths/src/test/get-point-at-length.test.ts b/packages/paths/src/test/get-point-at-length.test.ts
new file mode 100644
index 0000000000..11a3f3723d
--- /dev/null
+++ b/packages/paths/src/test/get-point-at-length.test.ts
@@ -0,0 +1,8 @@
+import {expect, test} from 'vitest';
+import {getPointAtLength} from '../get-point-at-length';
+
+test('Should be able to get parts of a path', () => {
+ const parts = getPointAtLength('M 0 0 L 100 0', 50);
+
+ expect(parts).toEqual({x: 50, y: 0});
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 22e4cdde15..9cc90c32f6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -256,6 +256,7 @@ importers:
'@remotion/lambda': 3.2.9
'@remotion/lottie': 3.2.9
'@remotion/media-utils': 3.2.9
+ '@remotion/paths': 3.2.9
'@remotion/player': 3.2.9
'@remotion/preload': 3.2.9
'@remotion/renderer': 3.2.9
@@ -300,6 +301,7 @@ importers:
'@remotion/lambda': link:../lambda
'@remotion/lottie': link:../lottie
'@remotion/media-utils': link:../media-utils
+ '@remotion/paths': link:../paths
'@remotion/player': link:../player
'@remotion/preload': link:../preload
'@remotion/renderer': link:../renderer
@@ -561,6 +563,8 @@ importers:
'@remotion/renderer': link:../renderer
aws-policies: 1.0.1
mime-types: 2.1.34
+ react: 18.0.0
+ react-dom: [email protected]
remotion: link:../core
devDependencies:
'@jonny/eslint-config': 3.0.266_6hvhxfirl4y3p6nuemvvvbadcu
@@ -573,8 +577,6 @@ importers:
prettier: 2.4.1
prettier-plugin-organize-imports: 2.3.4_pxx7xhvr2bexxfjjff3nukai7m
- react: 18.0.0
- react-dom: [email protected]
ts-jest: 27.0.5_g2mfyom6zaxqcgedxrl4lakegu
ts-node: 10.8.0_ysvagj6uoj2jcpx4fli7niyicq
typescript: 4.7.2
@@ -624,6 +626,8 @@ importers:
remotion: 3.2.9
typescript: ^4.7.0
dependencies:
+ react: 18.0.0
+ react-dom: [email protected]
remotion: link:../core
devDependencies:
'@jonny/eslint-config': 3.0.266_6hvhxfirl4y3p6nuemvvvbadcu
@@ -632,8 +636,6 @@ importers:
eslint: 8.13.0
prettier: 2.4.1
prettier-plugin-organize-imports: 2.3.4_pxx7xhvr2bexxfjjff3nukai7m
- react: 18.0.0
- react-dom: [email protected]
typescript: 4.7.2
packages/paths:
@@ -644,6 +646,7 @@ importers:
prettier: ^2.0.5
prettier-plugin-organize-imports: ^2.3.4
typescript: ^4.7.0
+ vitest: ^0.18.0
devDependencies:
'@jonny/eslint-config': 3.0.266_6hvhxfirl4y3p6nuemvvvbadcu
'@types/node': 16.10.2
@@ -651,6 +654,7 @@ importers:
prettier: 2.6.2
prettier-plugin-organize-imports: 2.3.4_lbsuufegg2dnc4wh6qgf26kgdy
typescript: 4.7.2
+ vitest: 0.18.0
packages/player:
specifiers:
diff --git a/vercel.json b/vercel.json
index 84215d63bc..375a96fd6e 100644
--- a/vercel.json
+++ b/vercel.json
@@ -39,6 +39,11 @@
"source": "/lottie",
"destination": "https://remotion.dev/docs/lottie",
"permanent": false
+ },
+ {
+ "source": "/paths",
+ "destination": "https://remotion.dev/docs/paths",
+ "permanent": false
}
]
}