Skip to content

🎥 Make videos programmatically with React

scaffold text box

JonnyBurgerbc7c90ba1baa
diff --git a/packages/docs/docs/rounded-text-box/create-rounded-text-box.mdx b/packages/docs/docs/rounded-text-box/create-rounded-text-box.mdx
new file mode 100644
index 0000000000..5d1df85f99
--- /dev/null
+++ b/packages/docs/docs/rounded-text-box/create-rounded-text-box.mdx
@@ -0,0 +1,71 @@
+---
+image: /generated/articles-docs-rounded-text-box-create-rounded-text-box.png
+title: 'createRoundedTextBox()'
+crumb: '@remotion/rounded-text-box'
+---
+
+_Part of the [`@remotion/rounded-text-box`](/docs/rounded-text-box) package._
+
+## API
+
+_object_ <TsType type="CreateRoundedTextBoxProps" source="@remotion/rounded-text-box" />
+
+Accepts an object with the following properties:
+
+### `textMeasurements`
+
+_array_ <TsType type="Dimensions[]" source="@remotion/layout-utils" />
+
+An array of text measurements, each containing the `width` and `height` of a line of text.
+Should be obtained using the [`measureText()`](/docs/layout-utils/measure-text) function.
+
+### `textAlign`
+
+_string_ <TsType type="TextAlign" source="@remotion/rounded-text-box" />
+
+The alignment of the text.
+Can be `'left'`, `'center'`, or `'right'`.
+
+### `horizontalPadding`
+
+_number_
+
+The horizontal padding of the text box.
+
+### `borderRadius`
+
+_number_
+
+The border radius of the text box.
+
+## Return value
+
+_object_ <TsType type="CreateRoundedTextBoxResult" source="@remotion/rounded-text-box" />
+
+An object containing the following properties:
+
+### `d`
+
+_string_
+
+The SVG path.
+
+### `boundingBox`
+
+_object_ <TsType type="BoundingBox" source="@remotion/paths" />
+
+The bounding box of the text box. See [`getBoundingBox()`](/docs/paths/get-bounding-box) for the shape.
+
+### `instructions`
+
+_array_ <TsType type="ReducedInstruction[]" source="@remotion/paths" />
+
+The SVG path instructions as an array, for use with the [`@remotion/paths`](/docs/paths) package.
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/rounded-text-box/src/create-rounded-text-box.ts)
+- [`@remotion/rounded-text-box`](/docs/rounded-text-box)
+- [`@remotion/paths`](/docs/paths)
+- [`measureText()`](/docs/layout-utils/measure-text)
+- [`getBoundingBox()`](/docs/paths/get-bounding-box)
diff --git a/packages/docs/docs/rounded-text-box/index.mdx b/packages/docs/docs/rounded-text-box/index.mdx
new file mode 100644
index 0000000000..df6f57e0d4
--- /dev/null
+++ b/packages/docs/docs/rounded-text-box/index.mdx
@@ -0,0 +1,53 @@
+---
+image: /generated/articles-docs-rounded-text-box-index.png
+title: '@remotion/rounded-text-box'
+---
+
+_Available from v4.0.360_
+
+Create a TikTok-like multiline text box SVG path with rounded corners.
+
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+import {TableOfContents} from './table-of-contents';
+
+<Tabs
+defaultValue="npm"
+values={[
+{ label: 'npm', value: 'npm', },
+{ label: 'pnpm', value: 'pnpm', },
+{ label: 'yarn', value: 'yarn', },
+]
+}>
+<TabItem value="npm">
+
+```bash
+npm i @remotion/rounded-text-box
+```
+
+ </TabItem>
+
+ <TabItem value="pnpm">
+
+```bash
+pnpm i @remotion/rounded-text-box
+```
+
+ </TabItem>
+
+ <TabItem value="yarn">
+
+```bash
+yarn add @remotion/rounded-text-box
+```
+
+ </TabItem>
+</Tabs>
+
+## Functions
+
+<TableOfContents />
+
+## License
+
+MIT
diff --git a/packages/docs/docs/rounded-text-box/table-of-contents.tsx b/packages/docs/docs/rounded-text-box/table-of-contents.tsx
new file mode 100644
index 0000000000..cc033bc3c7
--- /dev/null
+++ b/packages/docs/docs/rounded-text-box/table-of-contents.tsx
@@ -0,0 +1,16 @@
+import React from 'react';
+import {Grid} from '../../components/TableOfContents/Grid';
+import {TOCItem} from '../../components/TableOfContents/TOCItem';
+
+export const TableOfContents: React.FC = () => {
+ return (
+ <div>
+ <Grid>
+ <TOCItem link="/docs/rounded-text-box/create-rounded-text-box">
+ <strong>createRoundedTextBox()</strong>
+ <div>Create a rounded SVG text box</div>
+ </TOCItem>
+ </Grid>
+ </div>
+ );
+};
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 8f43804563..a70f0ae491 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -582,6 +582,15 @@ const sidebars: SidebarsConfig = {
},
items: ['rive/index', 'rive/remotionrivecanvas'],
},
+ {
+ type: 'category',
+ label: '@remotion/rounded-text-box',
+ link: {
+ type: 'doc',
+ id: 'rounded-text-box/index',
+ },
+ items: ['rounded-text-box/create-rounded-text-box'],
+ },
{
type: 'category',
label: '@remotion/shapes',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index c13e6169bf..0b3d01ad48 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -4836,6 +4836,24 @@ export const articles = [
noAi: false,
slug: 'rive/remotionrivecanvas',
},
+ {
+ id: 'rounded-text-box/create-rounded-text-box',
+ title: 'createRoundedTextBox()',
+ relativePath: 'docs/rounded-text-box/create-rounded-text-box.mdx',
+ compId: 'articles-docs-rounded-text-box-create-rounded-text-box',
+ crumb: '@remotion/rounded-text-box',
+ noAi: false,
+ slug: 'rounded-text-box/create-rounded-text-box',
+ },
+ {
+ id: 'rounded-text-box/index',
+ title: '@remotion/rounded-text-box',
+ relativePath: 'docs/rounded-text-box/index.mdx',
+ compId: 'articles-docs-rounded-text-box-index',
+ crumb: null,
+ noAi: false,
+ slug: 'rounded-text-box/index',
+ },
{
id: 'scaling',
title: 'Output scaling',
diff --git a/packages/docs/static/generated/articles-docs-rounded-text-box-create-rounded-text-box.png b/packages/docs/static/generated/articles-docs-rounded-text-box-create-rounded-text-box.png
new file mode 100644
index 0000000000..54fa0f53c5
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-rounded-text-box-create-rounded-text-box.png differ
diff --git a/packages/docs/static/generated/articles-docs-rounded-text-box-index.png b/packages/docs/static/generated/articles-docs-rounded-text-box-index.png
new file mode 100644
index 0000000000..2a87eb5dd4
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-rounded-text-box-index.png differ
diff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
index 43fda7e60a..f99d4dc2fc 100644
--- a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
@@ -4,7 +4,6 @@
*/
import {measureText} from '@remotion/layout-utils';
-import {getBoundingBox} from '@remotion/paths';
import {createRoundedTextBox} from '@remotion/rounded-text-box';
interface TikTokTextBoxProps {
@@ -67,13 +66,12 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
}),
);
const horizontalPadding = 30;
- const svg = createRoundedTextBox({
+ const {d, boundingBox} = createRoundedTextBox({
textMeasurements: roundings,
textAlign: align,
horizontalPadding,
- cornerRadius: 20,
+ borderRadius: 20,
});
- const boundingBox = getBoundingBox(svg);
return (
<div
@@ -102,7 +100,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
overflow: 'visible',
}}
>
- <path fill="white" d={svg} />
+ <path fill="white" d={d} />
</svg>
{lines.map((line, i) => (
diff --git a/packages/example/src/Title/FitTextOnNLines.tsx b/packages/example/src/Title/FitTextOnNLines.tsx
index 2c7cf5f2e8..c98f725c40 100644
--- a/packages/example/src/Title/FitTextOnNLines.tsx
+++ b/packages/example/src/Title/FitTextOnNLines.tsx
@@ -1,5 +1,4 @@
import {fitTextOnNLines, measureText} from '@remotion/layout-utils';
-import {getBoundingBox} from '@remotion/paths';
import {createRoundedTextBox} from '@remotion/rounded-text-box';
import React from 'react';
import {AbsoluteFill} from 'remotion';
@@ -32,9 +31,9 @@ export const FitTextOnNLines: React.FC<
maxFontSize,
});
- const roundings = lines.map((line) =>
+ const textMeasurements = lines.map((text) =>
measureText({
- text: line,
+ text,
fontFamily,
fontSize,
additionalStyles: {
@@ -47,13 +46,12 @@ export const FitTextOnNLines: React.FC<
validateFontIsLoaded: true,
}),
);
- const svg = createRoundedTextBox({
- textMeasurements: roundings,
- textAlign: textAlign,
+ const {d, boundingBox} = createRoundedTextBox({
+ textMeasurements,
+ textAlign,
horizontalPadding,
- cornerRadius,
+ borderRadius: cornerRadius,
});
- const boundingBox = getBoundingBox(svg);
const lineStyle = React.useMemo<React.CSSProperties>(
() => ({
@@ -85,7 +83,7 @@ export const FitTextOnNLines: React.FC<
overflow: 'visible',
}}
>
- <path fill="white" d={svg} />
+ <path fill="white" d={d} />
</svg>
<div style={{position: 'relative'}}>
{lines.map((line, i) => (
diff --git a/packages/rounded-text-box/src/create-rounded-text-box.ts b/packages/rounded-text-box/src/create-rounded-text-box.ts
index bffa570981..e67266170d 100644
--- a/packages/rounded-text-box/src/create-rounded-text-box.ts
+++ b/packages/rounded-text-box/src/create-rounded-text-box.ts
@@ -1,5 +1,9 @@
import type {Dimensions} from '@remotion/layout-utils';
-import type {Instruction} from '@remotion/paths';
+import type {
+ BoundingBox,
+ Instruction,
+ ReducedInstruction,
+} from '@remotion/paths';
import {
PathInternals,
reduceInstructions,
@@ -16,14 +20,20 @@ export type CreateRoundedTextBoxProps = {
textMeasurements: Dimensions[];
textAlign: TextAlign;
horizontalPadding: number;
- cornerRadius: number;
+ borderRadius: number;
+};
+
+export type CreateRoundedTextBoxResult = {
+ d: string;
+ boundingBox: BoundingBox;
+ instructions: ReducedInstruction[];
};
export const createRoundedTextBox = ({
textMeasurements,
textAlign,
horizontalPadding,
- cornerRadius: unclampedMaxCornerRadius,
+ borderRadius: unclampedMaxCornerRadius,
}: CreateRoundedTextBoxProps) => {
const instructions: Instruction[] = [];
@@ -254,7 +264,11 @@ export const createRoundedTextBox = ({
const reduced = reduceInstructions(instructions);
- PathInternals.getBoundingBoxFromInstructions(reduced);
+ const boundingBox = PathInternals.getBoundingBoxFromInstructions(reduced);
- return serializeInstructions(reduced);
+ return {
+ d: serializeInstructions(reduced),
+ boundingBox,
+ instructions: reduced,
+ };
};
diff --git a/packages/rounded-text-box/src/index.ts b/packages/rounded-text-box/src/index.ts
index 867f1dc3df..ba7e664c9f 100644
--- a/packages/rounded-text-box/src/index.ts
+++ b/packages/rounded-text-box/src/index.ts
@@ -1,5 +1,6 @@
export {
createRoundedTextBox,
type CreateRoundedTextBoxProps,
+ type CreateRoundedTextBoxResult,
type TextAlign,
} from './create-rounded-text-box';