Skip to content

🎥 Make videos programmatically with React

add evolvePath function

Jonny Burger0e301fa4ce1c
diff --git a/packages/docs/docs/paths/evolve-path.md b/packages/docs/docs/paths/evolve-path.md
new file mode 100644
index 0000000000..9a341143ab
--- /dev/null
+++ b/packages/docs/docs/paths/evolve-path.md
@@ -0,0 +1,27 @@
+---
+title: evolvePath()
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+Animates an SVG path from being invisible to it's full length. The function takes two arguments:
+
+- `progress` is the progress towards full evolution, where `0` means the path being invisible, and `1` meaning the path being fully drawn out.
+ :::note
+ Passing in a value above 1 will result in the start of the path getting devolved. Passing in a value below 0 will result in the path getting evolved from the end.
+ :::
+
+- `path` must be a valid SVG path.
+
+```tsx twoslash
+import { evolvePath } from "@remotion/paths";
+
+const evolution = evolvePath(0.5, "M 0 0 L 100 0");
+console.log(evolution); // { strokeDasharray: '100 100', strokeDashoffset: 50,}
+```
+
+## See also
+
+- [`@remotion/paths`](/docs/paths)
+- [reversePath()](/docs/paths/reverse-path)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/evolve-path.ts)
diff --git a/packages/docs/docs/paths/index.md b/packages/docs/docs/paths/index.md
index f8d59ba071..a1bfe04591 100644
--- a/packages/docs/docs/paths/index.md
+++ b/packages/docs/docs/paths/index.md
@@ -55,7 +55,8 @@ yarn add @remotion/paths
- [`getTangentAtLength()`](/docs/paths/get-tangent-at-length)
- [`reversePath()`](/docs/paths/reverse-path)
- [`normalizePath()`](/docs/paths/normalize-path)
-- [`interpolatePath()`](/docs/paths/interpolate-path
+- [`interpolatePath()`](/docs/paths/interpolate-path)
+- [`evolvePath()`](/docs/paths/evolve-path)
## License
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index e933359113..b894563d5f 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -290,6 +290,7 @@ module.exports = {
"paths/reverse-path",
"paths/normalize-path",
"paths/interpolate-path",
+ "paths/evolve-path",
],
},
{
diff --git a/packages/example/src/Paths/PathEvolve.tsx b/packages/example/src/Paths/PathEvolve.tsx
index 0bf3794b09..5433a2ae8b 100644
--- a/packages/example/src/Paths/PathEvolve.tsx
+++ b/packages/example/src/Paths/PathEvolve.tsx
@@ -1,4 +1,4 @@
-import {getLength} from '@remotion/paths';
+import {evolvePath} from '@remotion/paths';
import React from 'react';
import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';
@@ -18,13 +18,8 @@ const PathEvolve: React.FC = () => {
},
});
- const pathLength1 = getLength(path1);
- const strokeDasharray = `${pathLength1} ${pathLength1}`;
- const strokeDashoffset = pathLength1 - spr * pathLength1;
-
- const pathLength2 = getLength(path2);
- const strokeDasharray2 = `${pathLength2} ${pathLength2}`;
- const strokeDashoffset2 = pathLength2 - spr * pathLength2;
+ const evolution1 = evolvePath(path1, spr);
+ const evolution2 = evolvePath(path2, spr);
return (
<AbsoluteFill>
@@ -42,8 +37,8 @@ const PathEvolve: React.FC = () => {
stroke="url(#paint0_linear_9_2)"
strokeWidth="89.9167"
shapeRendering="crispEdges"
- strokeDasharray={strokeDasharray}
- strokeDashoffset={strokeDashoffset}
+ strokeDasharray={evolution1.strokeDasharray}
+ strokeDashoffset={evolution1.strokeDashoffset}
/>
</g>
<g filter="url(#filter1_d_9_2)">
@@ -52,8 +47,8 @@ const PathEvolve: React.FC = () => {
stroke="url(#paint1_linear_9_2)"
strokeWidth="79.2391"
shapeRendering="crispEdges"
- strokeDasharray={strokeDasharray2}
- strokeDashoffset={strokeDashoffset2}
+ strokeDasharray={evolution2.strokeDasharray}
+ strokeDashoffset={evolution2.strokeDashoffset}
/>
</g>
</g>
diff --git a/packages/paths/README.md b/packages/paths/README.md
index ede0231650..f1b0662336 100644
--- a/packages/paths/README.md
+++ b/packages/paths/README.md
@@ -18,6 +18,7 @@ No dependencies are needed, meaning this package can be used independently of Re
- [`reversePath()`](https://remotion.dev/docs/paths/reverse-path)
- [`normalizePath()`](https://remotion.dev/docs/paths/normalize-path)
- [`interpolatePath()`]https://remotion.dev/docs/paths/interpolate-path
+- [`evolvePath()`]https://remotion.dev/docs/paths/evolve-path
## License
diff --git a/packages/paths/src/evolve-path.ts b/packages/paths/src/evolve-path.ts
new file mode 100644
index 0000000000..454b846b3e
--- /dev/null
+++ b/packages/paths/src/evolve-path.ts
@@ -0,0 +1,15 @@
+import {getLength} from './get-length';
+
+/**
+ * Animates an SVG from being invisible to it's full length.
+ * @param {string} path A valid SVG path
+ * @param {number} progress The first valid SVG path
+ * @link https://remotion.dev/docs/paths/evolve-path
+ */
+export const evolvePath = (progress: number, path: string) => {
+ const length = getLength(path);
+ const strokeDasharray = `${length} ${length}`;
+ const strokeDashoffset = length - progress * length;
+
+ return {strokeDasharray, strokeDashoffset};
+};
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index f9000a24de..bf305fae25 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,3 +1,4 @@
+export {evolvePath} from './evolve-path';
export {getLength} from './get-length';
export {getParts} from './get-parts';
export {getPointAtLength} from './get-point-at-length';
diff --git a/packages/paths/src/test/evolve-path.test.ts b/packages/paths/src/test/evolve-path.test.ts
new file mode 100644
index 0000000000..57e5c857bc
--- /dev/null
+++ b/packages/paths/src/test/evolve-path.test.ts
@@ -0,0 +1,9 @@
+import {expect, test} from 'vitest';
+import {evolvePath} from '../evolve-path';
+
+test('Should be able to interpolate path', () => {
+ expect(evolvePath(0.5, 'M 0 0 L 100 0')).toEqual({
+ strokeDasharray: '100 100',
+ strokeDashoffset: 50,
+ });
+});