🎥 Make videos programmatically with React
@remotion/paths: Promote cutPath to public API with documentation
copilot-swe-agent[bot]45954abe5a64
diff --git a/packages/docs/docs/paths/cut-path.mdx b/packages/docs/docs/paths/cut-path.mdxnew file mode 100644index 0000000000..f53c4cc104--- /dev/null+++ b/packages/docs/docs/paths/cut-path.mdx@@ -0,0 +1,84 @@+---+image: /generated/articles-docs-paths-cut-path.png+title: cutPath()+crumb: "@remotion/paths"+---++_Part of the [`@remotion/paths`](/docs/paths) package._++Cuts an SVG path at a specified length, returning the portion from the start to that length.++```tsx twoslash+import { cutPath } from "@remotion/paths";++const path = "M 0 0 L 100 0 L 100 100";+const cutAtLength = cutPath(path, 50);+console.log(cutAtLength); // "M 0 0 L 50 0"+```++## Arguments++### `d`++_string_++A valid SVG path property. For example:++```+M 0 0 L 100 0 L 100 100 L 0 100 Z+```++### `length`++_number_++The length at which to cut the path. If the length is greater than the total path length, the entire path is returned. If the length is 0, only the initial move command is returned.++## Return value++A string representing the cut path from the start to the specified length.++## Examples++### Cutting a simple line++```tsx twoslash+import { cutPath } from "@remotion/paths";++const simpleLine = "M 0 0 L 100 0";+const halfLine = cutPath(simpleLine, 50);+console.log(halfLine); // "M 0 0 L 50 0"+```++### Cutting a path with curves++```tsx twoslash+import { cutPath } from "@remotion/paths";++const curvePath = "M 0 0 C 50 0 50 50 100 50";+const cutCurve = cutPath(curvePath, 30);+console.log(cutCurve); // "M 0 0 C 30 0 30 15 60 15" (approximate)+```++### Handling edge cases++```tsx twoslash+import { cutPath } from "@remotion/paths";++const path = "M 0 0 L 100 0";++// Length greater than path length+const fullPath = cutPath(path, 200);+console.log(fullPath); // "M 0 0 L 100 0"++// Zero length+const startOnly = cutPath(path, 0);+console.log(startOnly); // "M 0 0"+```++## See also++- [getLength()](/docs/paths/get-length)+- [getPointAtLength()](/docs/paths/get-point-at-length)+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/cut-path.ts)+- [`@remotion/paths`](/docs/paths)\ No newline at end of filediff --git a/packages/docs/docs/paths/table-of-contents.tsx b/packages/docs/docs/paths/table-of-contents.tsxindex 9c5c3b08a9..125583db3f 100644--- a/packages/docs/docs/paths/table-of-contents.tsx+++ b/packages/docs/docs/paths/table-of-contents.tsx@@ -10,6 +10,10 @@ export const TableOfContents: React.FC = () => {<strong>getLength()</strong><div>Obtain length of an SVG path</div></TOCItem>+ <TOCItem link="/docs/paths/cut-path">+ <strong>cutPath()</strong>+ <div>Cut an SVG path at a specified length</div>+ </TOCItem><TOCItem link="/docs/paths/get-point-at-length"><strong>getPointAtLength()</strong><div>Get coordinates at a certain point of an SVG path</div>diff --git a/packages/docs/docs/standalone.mdx b/packages/docs/docs/standalone.mdxindex 24d59a211b..6adfc3e6f9 100644--- a/packages/docs/docs/standalone.mdx+++ b/packages/docs/docs/standalone.mdx@@ -48,6 +48,7 @@ Unbolded APIs are licensed under the MIT license.**@remotion/paths**:- [getLength\(\)](/docs/paths/get-length)+- [cutPath\(\)](/docs/paths/cut-path)- [getPointAtLength\(\)](/docs/paths/get-point-at-length)- [getTangentAtLength\(\)](/docs/paths/get-tangent-at-length)- [getInstructionIndexAtLength\(\)](/docs/paths/get-instruction-index-at-length)diff --git a/packages/paths/README.md b/packages/paths/README.mdindex c151329adf..ebab276c29 100644--- a/packages/paths/README.md+++ b/packages/paths/README.md@@ -14,5 +14,23 @@ When installing a Remotion package, make sure to align the version of all `remotRemove the `^` character from the version number to use the exact version.## Usage-+See the [documentation](https://www.remotion.dev/paths) for more information.++### cutPath()++Cuts an SVG path at a specified length, returning the portion from the start to that length.++```js+import {cutPath} from '@remotion/paths';++const path = 'M 0 0 L 100 0 L 100 100';+const cutAtLength = cutPath(path, 50);+console.log(cutAtLength); // 'M 0 0 L 50 0'+```++**Parameters:**+- `d` (string): The SVG path data string+- `length` (number): The length at which to cut the path++**Returns:** A string representing the cut path from the start to the specified length.diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.tsindex 0fafb857e7..ffaf6d58ef 100644--- a/packages/paths/src/index.ts+++ b/packages/paths/src/index.ts@@ -2,6 +2,7 @@ import {cutPath} from './cut-path';import {debugPath} from './debug-path';import {getBoundingBoxFromInstructions} from './get-bounding-box';+export {cutPath} from './cut-path';export {evolvePath} from './evolve-path';export {extendViewBox} from './extend-viewbox';export {getBoundingBox} from './get-bounding-box';diff --git a/packages/paths/src/test/cut-path.test.ts b/packages/paths/src/test/cut-path.test.tsnew file mode 100644index 0000000000..45599ab239--- /dev/null+++ b/packages/paths/src/test/cut-path.test.ts@@ -0,0 +1,25 @@+import {expect, test} from 'bun:test';+import {cutPath} from '../cut-path';++test('Should be able to cut a simple path', () => {+ const result = cutPath('M 0 0 L 100 0', 50);+ expect(result).toBe('M 0 0 L 50 0');+});++test('Should return full path if length is greater than path length', () => {+ const result = cutPath('M 0 0 L 100 0', 200);+ expect(result).toBe('M 0 0 L 100 0');+});++test('Should handle zero length', () => {+ const result = cutPath('M 0 0 L 100 0', 0);+ expect(result).toBe('M 0 0');+});++test('Should handle complex path with curves', () => {+ const path = 'M 0 0 C 50 0 50 50 100 50';+ const result = cutPath(path, 50);+ // Result should be truncated curve+ expect(result).toMatch(/^M 0 0 C/);+ expect(result.length).toBeGreaterThan(0);+});\ No newline at end of file