🎥 Make videos programmatically with React
`@remotion/paths`: Add centerPath (#8838)
Jonny Burger2a721377c8e4
diff --git a/packages/brand/src/HomepageAssets/NpmInitVideo/NpmInitVideo.tsx b/packages/brand/src/HomepageAssets/NpmInitVideo/NpmInitVideo.tsxindex 7b09eb1288..6af976b356 100644--- a/packages/brand/src/HomepageAssets/NpmInitVideo/NpmInitVideo.tsx+++ b/packages/brand/src/HomepageAssets/NpmInitVideo/NpmInitVideo.tsx@@ -1,4 +1,4 @@-import {parsePath} from '@remotion/paths';+import {centerPath, parsePath} from '@remotion/paths';import {makeCircle, makeRect} from '@remotion/shapes';import React from 'react';import {@@ -8,7 +8,6 @@ import {useCurrentFrame,useVideoConfig,} from 'remotion';-import {centerPath} from '../center';import {BLUE} from '../colors';import {makeElement, transformElement} from '../element';import {Faces} from '../Faces';diff --git a/packages/brand/src/HomepageAssets/RenderProgress/make-button.ts b/packages/brand/src/HomepageAssets/RenderProgress/make-button.tsindex 07d0a42a0e..78c8f195f0 100644--- a/packages/brand/src/HomepageAssets/RenderProgress/make-button.ts+++ b/packages/brand/src/HomepageAssets/RenderProgress/make-button.ts@@ -1,4 +1,5 @@import {+ centerPath,getBoundingBox,parsePath,resetPath,@@ -8,7 +9,6 @@ import {import {makeRect} from '@remotion/shapes';import {Font} from 'opentype.js';import {interpolate, spring} from 'remotion';-import {centerPath} from '../center';import {makeElement, ThreeDElement, transformElement} from '../element';import {turnInto3D} from '../fix-z';import {getText} from '../get-char';diff --git a/packages/brand/src/HomepageAssets/Studio.tsx b/packages/brand/src/HomepageAssets/Studio.tsxindex f35ac82383..bd90f31d35 100644--- a/packages/brand/src/HomepageAssets/Studio.tsx+++ b/packages/brand/src/HomepageAssets/Studio.tsx@@ -1,4 +1,10 @@-import {getBoundingBox, parsePath, resetPath, scalePath} from '@remotion/paths';+import {+ centerPath,+ getBoundingBox,+ parsePath,+ resetPath,+ scalePath,+} from '@remotion/paths';import {makeRect, makeTriangle} from '@remotion/shapes';import React from 'react';import {@@ -8,7 +14,6 @@ import {useCurrentFrame,useVideoConfig,} from 'remotion';-import {centerPath} from './center';import {BLUE, GREEN} from './colors';import {makeElement, transformElement} from './element';import {Faces} from './Faces';diff --git a/packages/brand/src/HomepageAssets/center.ts b/packages/brand/src/HomepageAssets/center.tsdeleted file mode 100644index a609ee8502..0000000000--- a/packages/brand/src/HomepageAssets/center.ts+++ /dev/null@@ -1,14 +0,0 @@-import {getBoundingBox, translatePath} from '@remotion/paths';--export const centerPath = (path: string) => {- const bBox = getBoundingBox(path);- const buttonWidth = bBox.x2 - bBox.x1;- const buttonHeight = bBox.y2 - bBox.y1;-- const centeredButton = translatePath(- path,- -buttonWidth / 2,- -buttonHeight / 2,- );- return centeredButton;-};diff --git a/packages/docs/docs/paths/center-path.mdx b/packages/docs/docs/paths/center-path.mdxnew file mode 100644index 0000000000..1235f83c32--- /dev/null+++ b/packages/docs/docs/paths/center-path.mdx@@ -0,0 +1,46 @@+---+image: /generated/articles-docs-paths-center-path.png+title: centerPath()+crumb: "@remotion/paths"+---++_Part of the [`@remotion/paths`](/docs/paths) package. Available from v4.0.486_++Translates an SVG path so that the center of the [bounding box](/docs/paths/get-bounding-box) is at a target point.++By default, the path is centered around `0, 0`.++```tsx twoslash title="center-path.ts"+import { centerPath } from "@remotion/paths";++const newPath = centerPath("M 10 10 L 20 20"); // M -5 -5 L 5 5+```++## Arguments++### path++The original SVG path.++### target?++The point that the center of the bounding box should be translated to. Default: `{x: 0, y: 0}`.++```tsx twoslash title="center-path-target.ts"+import { centerPath } from "@remotion/paths";++const newPath = centerPath("M 10 10 L 20 20", { x: 100, y: 50 }); // M 95 45 L 105 55+```++## Return value++Returns a new `string` containing the translated path.++This function will throw if the SVG path is invalid.++## See also++- [`resetPath()`](/docs/paths/reset-path)+- [`translatePath()`](/docs/paths/translate-path)+- [`getBoundingBox()`](/docs/paths/get-bounding-box)+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/center-path.ts)diff --git a/packages/docs/docs/paths/table-of-contents.tsx b/packages/docs/docs/paths/table-of-contents.tsxindex 10efcb510e..714658d9e9 100644--- a/packages/docs/docs/paths/table-of-contents.tsx+++ b/packages/docs/docs/paths/table-of-contents.tsx@@ -41,6 +41,12 @@ export const TableOfContents: React.FC = () => {<strong>evolvePath()</strong><div>Animate an SVG path</div></TOCItem>+ <TOCItem link="/docs/paths/center-path">+ <strong>centerPath()</strong>+ <div>+ Translates an SVG path to center it around a target point+ </div>+ </TOCItem><TOCItem link="/docs/paths/translate-path"><strong>translatePath()</strong><div>Translates the position of an path against X/Y coordinates</div>diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 8df4478319..41e554a6ec 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -569,6 +569,7 @@ const sidebars: SidebarsConfig = {'paths/interpolate-path','paths/evolve-path','paths/reset-path',+ 'paths/center-path','paths/get-subpaths','paths/translate-path','paths/warp-path',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex a48f70adfb..7ad4c1764c 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -4789,6 +4789,15 @@ export const articles = [noAi: false,slug: 'passing-props',},+ {+ id: 'paths/center-path',+ title: 'centerPath()',+ relativePath: 'docs/paths/center-path.mdx',+ compId: 'articles-docs-paths-center-path',+ crumb: '@remotion/paths',+ noAi: false,+ slug: 'paths/center-path',+ },{id: 'paths/cut-path',title: 'cutPath()',diff --git a/packages/docs/static/generated/articles-docs-paths-center-path.png b/packages/docs/static/generated/articles-docs-paths-center-path.pngnew file mode 100644index 0000000000..311662c8a4Binary files /dev/null and b/packages/docs/static/generated/articles-docs-paths-center-path.png differdiff --git a/packages/paths/src/center-path.ts b/packages/paths/src/center-path.tsnew file mode 100644index 0000000000..9bb81b8013--- /dev/null+++ b/packages/paths/src/center-path.ts@@ -0,0 +1,22 @@+import {getBoundingBox} from './get-bounding-box';+import {translatePath} from './translate-path';++export type CenterPathTarget = {+ x: number;+ y: number;+};++/*+ * @description Translates an SVG path so that the center of the bounding box is at the specified point. Defaults to 0, 0.+ * @see [Documentation](https://www.remotion.dev/docs/paths/center-path)+ */+export const centerPath = (+ d: string,+ target: CenterPathTarget = {x: 0, y: 0},+) => {+ const box = getBoundingBox(d);+ const dx = target.x - (box.x1 + box.x2) / 2;+ const dy = target.y - (box.y1 + box.y2) / 2;++ return translatePath(d, dx, dy);+};diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.tsindex ffaf6d58ef..315a0b53cb 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 {centerPath, type CenterPathTarget} from './center-path';export {cutPath} from './cut-path';export {evolvePath} from './evolve-path';export {extendViewBox} from './extend-viewbox';diff --git a/packages/paths/src/test/center-path.test.ts b/packages/paths/src/test/center-path.test.tsnew file mode 100644index 0000000000..442aca85b7--- /dev/null+++ b/packages/paths/src/test/center-path.test.ts@@ -0,0 +1,27 @@+import {expect, test} from 'bun:test';+import {centerPath} from '../center-path';+import {getBoundingBox} from '../get-bounding-box';++test('centerPath() centers a path around 0, 0', () => {+ expect(centerPath('M 10 10 L 20 20')).toBe('M -5 -5 L 5 5');+});++test('centerPath() accounts for bounding boxes that do not start at 0, 0', () => {+ const centered = centerPath('M 100 50 L 150 75');+ const box = getBoundingBox(centered);++ expect((box.x1 + box.x2) / 2).toBe(0);+ expect((box.y1 + box.y2) / 2).toBe(0);+});++test('centerPath() centers a path around a target point', () => {+ expect(centerPath('M 10 10 L 20 20', {x: 100, y: 50})).toBe(+ 'M 95 45 L 105 55',+ );+});++test('centerPath() with arcs', () => {+ expect(centerPath('M 35,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0')).toBe(+ 'M -25 0 a 25 25 0 1 1 50 0 a 25 25 0 1 1 -50 0',+ );+});