Back to Remotion

createRoundedTextBox()

packages/docs/docs/rounded-text-box/create-rounded-text-box.mdx

4.0.4683.9 KB
Original Source

Part of the @remotion/rounded-text-box package.

Creates a SVG path for rendering a text box with rounded corners, as pioneered by TikTok and Instagram.

<Demo type="rounded-text-box" />

The above example combines fitTextOnNLines() and measureText() to get the text measurements, and then creates a rounded text box.
View the source code here.

Usage Example

tsx
import {fitTextOnNLines, measureText} from '@remotion/layout-utils';
import {createRoundedTextBox} from '@remotion/rounded-text-box';

const maxLines = 3;
const fontFamily = 'Figtree';
const fontWeight = '700';
const horizontalPadding = 20;
const borderRadius = 20;
const fontSize = 36;
const lineHeight = 1.5;
const textAlign = 'center';

const lines = ['Hello World', 'This is a test', 'This is another test'];

const textMeasurements = lines.map((t) =>
  measureText({
    text: t,
    fontFamily,
    fontSize,
    additionalStyles: {
      lineHeight,
    },
    fontVariantNumeric: 'normal',
    fontWeight,
    letterSpacing: 'normal',
    textTransform: 'none',
    validateFontIsLoaded: true,
  }),
);

const {d, boundingBox} = createRoundedTextBox({
  textMeasurements,
  textAlign,
  horizontalPadding,
  borderRadius,
});

const markup = (
  <div
    style={{
      width: boundingBox.width,
      height: boundingBox.height,
    }}
  >
    <svg
      viewBox={boundingBox.viewBox}
      style={{
        position: 'absolute',
        width: boundingBox.width,
        height: boundingBox.height,
        overflow: 'visible',
      }}
    >
      <path fill="white" d={d} />
    </svg>
    <div style={{position: 'relative'}}>
      {lines.map((line, i) => (
        <div
          key={i}
          style={{
            fontSize,
            fontWeight,
            fontFamily,
            lineHeight,
            textAlign,
            paddingLeft: horizontalPadding,
            paddingRight: horizontalPadding,
            color: 'black',
          }}
        >
          {line}
        </div>
      ))}
    </div>
  </div>
);

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() 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() for the shape.

instructions

array <TsType type="ReducedInstruction[]" source="@remotion/paths" />

The SVG path instructions as an array, for use with the @remotion/paths package.

See also