Skip to content

🎥 Make videos programmatically with React

add interpolatePath and an example

Jonny Burger4ff23125efec
diff --git a/packages/example/package.json b/packages/example/package.json
index f0c8e4be05..2219136c91 100644
--- a/packages/example/package.json
+++ b/packages/example/package.json
@@ -36,6 +36,7 @@
"@remotion/lambda": "3.2.10",
"@remotion/lottie": "3.2.10",
"@remotion/media-utils": "3.2.10",
+ "@remotion/paths": "3.2.10",
"@remotion/renderer": "3.2.10",
"@remotion/three": "3.2.10",
"@types/express": "^4.17.11",
diff --git a/packages/example/src/Paths/PathEvolve.tsx b/packages/example/src/Paths/PathEvolve.tsx
new file mode 100644
index 0000000000..0bf3794b09
--- /dev/null
+++ b/packages/example/src/Paths/PathEvolve.tsx
@@ -0,0 +1,162 @@
+import {getLength} from '@remotion/paths';
+import React from 'react';
+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';
+
+const path1 = 'M70.0708 -74.7021C120.649 196.82 515.72 572.643 1156.38 910.447';
+
+const path2 =
+ 'M835.861 203.393C629.777 453.394 415.916 655.36 237.6 780.052C148.205 842.563 69.6438 884.304 6.78505 903.743C-57.91 923.749 -95.0356 917.089 -114.147 901.335C-133.259 885.58 -146.881 850.408 -139.586 783.084C-132.498 717.671 -106.514 632.589 -62.2091 532.908C26.1669 334.076 183.616 85.6076 389.701 -164.393C595.785 -414.394 809.646 -616.36 987.962 -741.051C1077.36 -803.563 1155.92 -845.303 1218.78 -864.742C1283.47 -884.749 1320.6 -878.089 1339.71 -862.334C1358.82 -846.58 1372.44 -811.407 1365.15 -744.083C1358.06 -678.67 1332.08 -593.589 1287.77 -493.908C1199.4 -295.075 1041.95 -46.6073 835.861 203.393Z';
+
+const PathEvolve: React.FC = () => {
+ const {fps} = useVideoConfig();
+ const frame = useCurrentFrame();
+ const spr = spring({
+ fps,
+ frame,
+ config: {
+ damping: 200,
+ },
+ });
+
+ 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;
+
+ return (
+ <AbsoluteFill>
+ <svg
+ width="1080"
+ height="1080"
+ viewBox="0 0 1080 1080"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <g clipPath="url(#clip0_9_2)">
+ <g filter="url(#filter0_d_9_2)">
+ <path
+ d={path1}
+ stroke="url(#paint0_linear_9_2)"
+ strokeWidth="89.9167"
+ shapeRendering="crispEdges"
+ strokeDasharray={strokeDasharray}
+ strokeDashoffset={strokeDashoffset}
+ />
+ </g>
+ <g filter="url(#filter1_d_9_2)">
+ <path
+ d={path2}
+ stroke="url(#paint1_linear_9_2)"
+ strokeWidth="79.2391"
+ shapeRendering="crispEdges"
+ strokeDasharray={strokeDasharray2}
+ strokeDashoffset={strokeDashoffset2}
+ />
+ </g>
+ </g>
+ <defs>
+ <filter
+ id="filter0_d_9_2"
+ x="23.6248"
+ y="-82.9353"
+ width="1155.97"
+ height="1037.65"
+ filterUnits="userSpaceOnUse"
+ colorInterpolationFilters="sRGB"
+ >
+ <feFlood floodOpacity="0" result="BackgroundImageFix" />
+ <feColorMatrix
+ in="SourceAlpha"
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"
+ result="hardAlpha"
+ />
+ <feOffset dy="2.24792" />
+ <feGaussianBlur stdDeviation="1.12396" />
+ <feComposite in2="hardAlpha" operator="out" />
+ <feColorMatrix
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"
+ />
+ <feBlend
+ mode="normal"
+ in2="BackgroundImageFix"
+ result="effect1_dropShadow_9_2"
+ />
+ <feBlend
+ mode="normal"
+ in="SourceGraphic"
+ in2="effect1_dropShadow_9_2"
+ result="shape"
+ />
+ </filter>
+ <filter
+ id="filter1_d_9_2"
+ x="-258.175"
+ y="-991.508"
+ width="1741.91"
+ height="2026.51"
+ filterUnits="userSpaceOnUse"
+ colorInterpolationFilters="sRGB"
+ >
+ <feFlood floodOpacity="0" result="BackgroundImageFix" />
+ <feColorMatrix
+ in="SourceAlpha"
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"
+ result="hardAlpha"
+ />
+ <feOffset dy="2.24792" />
+ <feGaussianBlur stdDeviation="38.4956" />
+ <feComposite in2="hardAlpha" operator="out" />
+ <feColorMatrix
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"
+ />
+ <feBlend
+ mode="normal"
+ in2="BackgroundImageFix"
+ result="effect1_dropShadow_9_2"
+ />
+ <feBlend
+ mode="normal"
+ in="SourceGraphic"
+ in2="effect1_dropShadow_9_2"
+ result="shape"
+ />
+ </filter>
+ <linearGradient
+ id="paint0_linear_9_2"
+ x1="571.918"
+ y1="-147.714"
+ x2="571.918"
+ y2="942.075"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop stopColor="#0B84F3" />
+ <stop offset="1" stopColor="#0B84F3" stopOpacity="0.52" />
+ </linearGradient>
+ <linearGradient
+ id="paint1_linear_9_2"
+ x1="1294.73"
+ y1="-144.472"
+ x2="-309.635"
+ y2="-195.201"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop stopColor="#0B84F3" />
+ <stop offset="1" stopColor="#0B84F3" stopOpacity="0.46" />
+ </linearGradient>
+ <clipPath id="clip0_9_2">
+ <rect width="1080" height="1080" fill="white" />
+ </clipPath>
+ </defs>
+ </svg>
+ </AbsoluteFill>
+ );
+};
+
+export default PathEvolve;
diff --git a/packages/example/src/Paths/PathMorph.tsx b/packages/example/src/Paths/PathMorph.tsx
new file mode 100644
index 0000000000..0928c5a516
--- /dev/null
+++ b/packages/example/src/Paths/PathMorph.tsx
@@ -0,0 +1,35 @@
+import {interpolatePath} from '@remotion/paths';
+import React from 'react';
+import {spring, useCurrentFrame, useVideoConfig} from 'remotion';
+
+const path1 =
+ 'M94.5001 332C47.0001 233.5 -9.09994 56.1999 146.5 135C341 233.5 291 426 475.5 353C623.1 294.6 649.667 154 644.5 91C734.333 47 900.5 9.79997 846.5 213';
+const path2 =
+ 'M94.1703 715.596C37.5 635.5 -9.42966 439.796 146.17 518.596C340.67 617.096 273 708.5 457.5 635.5C605.1 577.1 556 503.5 704 596.596C872.5 666.5 652 441 867.5 565.5';
+
+const PathMorph: React.FC = () => {
+ const {fps} = useVideoConfig();
+ const frame = useCurrentFrame();
+
+ const progress = spring({
+ fps,
+ frame,
+ config: {damping: 200},
+ });
+
+ const path = interpolatePath(progress, path1, path2);
+
+ return (
+ <svg
+ width="1080"
+ height="1080"
+ viewBox="0 0 1080 1080"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path d={path} stroke="blue" strokeWidth="42" />
+ </svg>
+ );
+};
+
+export default PathMorph;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index ce52411bb9..e54290e87f 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -475,6 +475,24 @@ export const Index: React.FC = () => {
width={576}
/>
</Folder>
+ <Folder name="paths">
+ <Composition
+ id="path-evolve"
+ lazyComponent={() => import('./Paths/PathEvolve')}
+ durationInFrames={500}
+ fps={30}
+ height={1080}
+ width={1080}
+ />
+ <Composition
+ id="path-morph"
+ lazyComponent={() => import('./Paths/PathMorph')}
+ durationInFrames={500}
+ fps={30}
+ height={1080}
+ width={1080}
+ />
+ </Folder>
</>
);
};
diff --git a/packages/paths/src/helpers/split-curve.ts b/packages/paths/src/helpers/split-curve.ts
index 909c14381f..0fb0812cd1 100644
--- a/packages/paths/src/helpers/split-curve.ts
+++ b/packages/paths/src/helpers/split-curve.ts
@@ -1,3 +1,36 @@
+/*
+
+Copied and adapted from https://github.com/pbeshai/d3-interpolate-path:
+Copyright 2016, Peter Beshai
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+* Neither the name of the author nor the names of contributors may be used to
+ endorse or promote products derived from this software without specific prior
+ written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*/
+
/**
* de Casteljau's algorithm for drawing and splitting bezier curves.
* Inspired by https://pomax.github.io/bezierinfo/
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index f8170b6535..f9000a24de 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -3,5 +3,6 @@ export {getParts} from './get-parts';
export {getPointAtLength} from './get-point-at-length';
export {getTangentAtLength} from './get-tangent-at-length';
export {Part} from './helpers/types';
+export {interpolatePath} from './interpolate-path';
export {normalizePath} from './normalize-path';
export {reversePath} from './reverse-path';
diff --git a/packages/paths/src/interpolate-path.ts b/packages/paths/src/interpolate-path.ts
index fa46341ae3..f7341625e0 100644
--- a/packages/paths/src/interpolate-path.ts
+++ b/packages/paths/src/interpolate-path.ts
@@ -1,3 +1,36 @@
+/*
+
+Copied and adapted from https://github.com/pbeshai/d3-interpolate-path:
+Copyright 2016, Peter Beshai
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+* Neither the name of the author nor the names of contributors may be used to
+ endorse or promote products derived from this software without specific prior
+ written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*/
+
import type {Command} from './helpers/split-curve';
import {splitCurve, typeMap} from './helpers/split-curve';
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e896b8985f..f51e6f19a6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -387,6 +387,7 @@ importers:
'@remotion/lambda': 3.2.10
'@remotion/lottie': 3.2.10
'@remotion/media-utils': 3.2.10
+ '@remotion/paths': 3.2.10
'@remotion/renderer': 3.2.10
'@remotion/three': 3.2.10
'@types/babel__core': ^7.1.12
@@ -429,6 +430,7 @@ importers:
'@remotion/lambda': link:../lambda
'@remotion/lottie': link:../lottie
'@remotion/media-utils': link:../media-utils
+ '@remotion/paths': link:../paths
'@remotion/renderer': link:../renderer
'@remotion/three': link:../three
'@types/express': 4.17.13
@@ -15163,7 +15165,7 @@ packages:
dev: false
/is-arrayish/0.2.1:
- resolution: {integrity: sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=}
+ resolution: {integrity: sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==}
/is-arrayish/0.3.2:
resolution: {integrity: sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==}