Skip to content

🎥 Make videos programmatically with React

nice

JonnyBurgerf35127eef78c
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 0629d67082..2e3ddd61fa 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -628,11 +628,11 @@ export const Index: React.FC = () => {
<Still
id="FitTextOnNLines"
component={FitTextOnNLines}
- width={1300}
- height={350}
+ width={1600}
+ height={500}
schema={fitTextOnNLinesSchema}
defaultProps={{
- line: 'No matter how much text I am adding, the text always fits on 3 lines and there is corner rounding like on TikTok.',
+ text: 'No matter how much text I am adding, the text always fits on 3 lines and there is corner rounding like on TikTok.',
maxLines: 3,
textAlign: 'right' as const,
}}
diff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
index 45e304da3f..43fda7e60a 100644
--- a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
@@ -49,13 +49,16 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
textColor,
fontSize,
}) => {
+ const lineHeight = 1.5;
const fontWeight = 400;
const roundings = lines.map((line) =>
measureText({
text: line,
fontFamily,
fontSize,
- additionalStyles: {},
+ additionalStyles: {
+ lineHeight,
+ },
fontVariantNumeric: 'normal',
fontWeight,
letterSpacing: 'normal',
@@ -63,7 +66,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
validateFontIsLoaded: true,
}),
);
- const horizontalPadding = 20;
+ const horizontalPadding = 30;
const svg = createRoundedTextBox({
textMeasurements: roundings,
textAlign: align,
@@ -86,7 +89,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
textAlign: align,
fontFamily,
color: textColor ?? 'black',
- lineHeight: 1.5,
+ lineHeight,
fontWeight,
}}
>
diff --git a/packages/example/src/Title/FitTextOnNLines.tsx b/packages/example/src/Title/FitTextOnNLines.tsx
index 7be9ba97a1..ed85197ed2 100644
--- a/packages/example/src/Title/FitTextOnNLines.tsx
+++ b/packages/example/src/Title/FitTextOnNLines.tsx
@@ -1,63 +1,99 @@
-import {fitTextOnNLines} from '@remotion/layout-utils';
+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';
import {z} from 'zod';
-import {
- TIKTOK_TEXT_BOX_HORIZONTAL_PADDING,
- TikTokTextBox,
-} from '../TikTokTextbox/TikTokTextBox';
+import {TIKTOK_TEXT_BOX_HORIZONTAL_PADDING} from '../TikTokTextbox/TikTokTextBox';
-const boxWidth = 1200;
+const boxWidth = 1500;
const fontFamily = 'Proxima Nova';
-const fontWeight = 'bold';
+const fontWeight = 400;
+const horizontalPadding = 30;
+const cornerRadius = 20;
+const lineHeight = 1.5;
+const maxFontSize = 100;
export const fitTextOnNLinesSchema = z.object({
- line: z.string(),
+ text: z.string(),
maxLines: z.number().step(1),
textAlign: z.enum(['left', 'center', 'right']),
});
export const FitTextOnNLines: React.FC<
z.infer<typeof fitTextOnNLinesSchema>
-> = ({line, maxLines, textAlign}) => {
+> = ({text: line, maxLines, textAlign}) => {
const {fontSize, lines} = fitTextOnNLines({
maxLines,
maxBoxWidth: boxWidth - TIKTOK_TEXT_BOX_HORIZONTAL_PADDING * 2,
fontFamily,
text: line,
fontWeight,
- maxFontSize: 100,
+ maxFontSize,
});
+ const roundings = lines.map((line) =>
+ measureText({
+ text: line,
+ fontFamily,
+ fontSize,
+ additionalStyles: {
+ lineHeight,
+ },
+ fontVariantNumeric: 'normal',
+ fontWeight,
+ letterSpacing: 'normal',
+ textTransform: 'none',
+ validateFontIsLoaded: true,
+ }),
+ );
+ const svg = createRoundedTextBox({
+ textMeasurements: roundings,
+ textAlign: textAlign,
+ horizontalPadding,
+ cornerRadius,
+ });
+ const boundingBox = getBoundingBox(svg);
+
+ const lineStyle = React.useMemo<React.CSSProperties>(
+ () => ({
+ fontSize,
+ fontWeight,
+ fontFamily,
+ lineHeight,
+ textAlign,
+ paddingLeft: horizontalPadding,
+ paddingRight: horizontalPadding,
+ }),
+ [fontSize, textAlign],
+ );
+
return (
- <AbsoluteFill
+ <div
style={{
justifyContent: 'center',
alignItems: 'center',
- backgroundColor: '#1F2429',
+ width: boundingBox.width,
+ height: boundingBox.height,
}}
>
- <div
+ <svg
+ viewBox={boundingBox.viewBox}
style={{
- width: boxWidth,
- fontSize,
- overflow: 'auto',
- fontWeight,
- fontFamily,
- display: 'flex',
- flexDirection: 'column',
- alignItems: 'center',
- color: 'black',
+ position: 'absolute',
+ width: boundingBox.width,
+ height: boundingBox.height,
+ overflow: 'visible',
}}
>
- <TikTokTextBox
- textColor="black"
- lines={lines}
- fontFamily={fontFamily}
- textAlign={textAlign}
- fontSize={fontSize}
- />
+ <path fill="white" d={svg} />
+ </svg>
+ <div style={{position: 'relative'}}>
+ {lines.map((line, i) => (
+ <div key={i} style={lineStyle}>
+ {line}
+ </div>
+ ))}
</div>
- </AbsoluteFill>
+ </div>
);
};
diff --git a/packages/example/tsconfig.json b/packages/example/tsconfig.json
index 50a04044b5..dd2da48fc0 100644
--- a/packages/example/tsconfig.json
+++ b/packages/example/tsconfig.json
@@ -40,6 +40,7 @@
{"path": "../webcodecs"},
{"path": "../whisper-web"},
{"path": "../layout-utils"},
- {"path": "../media"}
+ {"path": "../media"},
+ {"path": "../rounded-text-box"}
]
}
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 c8a5483d8f..bffa570981 100644
--- a/packages/rounded-text-box/src/create-rounded-text-box.ts
+++ b/packages/rounded-text-box/src/create-rounded-text-box.ts
@@ -35,20 +35,20 @@ export const createRoundedTextBox = ({
let yOffset = 0;
for (let i = 0; i < textMeasurements.length; i++) {
- const prevCornerRounding = textMeasurements[i - 1];
- const cornerRounding = textMeasurements[i];
- const nextCornerRounding = textMeasurements[i + 1];
+ const previousLine = textMeasurements[i - 1];
+ const currentLine = textMeasurements[i];
+ const nextLine = textMeasurements[i + 1];
let xOffset = 0;
if (textAlign === 'center') {
- xOffset = (maxWidth - (cornerRounding.width + horizontalPadding * 2)) / 2;
+ xOffset = (maxWidth - (currentLine.width + horizontalPadding * 2)) / 2;
} else if (textAlign === 'right') {
- xOffset = maxWidth - (cornerRounding.width + horizontalPadding * 2);
+ xOffset = maxWidth - (currentLine.width + horizontalPadding * 2);
}
const maxCornerRadius = clamp(
unclampedMaxCornerRadius,
0,
- cornerRounding.height / 2,
+ currentLine.height / 2,
);
if (i === 0) {
@@ -60,12 +60,12 @@ export const createRoundedTextBox = ({
}
const topRightCornerRadius = clamp(
- prevCornerRounding
+ previousLine
? textAlign === 'right'
? 0
: textAlign === 'left'
- ? (prevCornerRounding.width - cornerRounding.width) / 2
- : (prevCornerRounding.width - cornerRounding.width) / 4
+ ? (previousLine.width - currentLine.width) / 2
+ : (previousLine.width - currentLine.width) / 4
: -Infinity,
-maxCornerRadius,
maxCornerRadius,
@@ -76,7 +76,7 @@ export const createRoundedTextBox = ({
type: 'L',
x:
xOffset +
- cornerRounding.width +
+ currentLine.width +
horizontalPadding * 2 +
topRightCornerRadius,
y: yOffset,
@@ -89,24 +89,24 @@ export const createRoundedTextBox = ({
xAxisRotation: 0,
largeArcFlag: false,
sweepFlag: topRightCornerRadius < 0,
- x: xOffset + cornerRounding.width + horizontalPadding * 2,
+ x: xOffset + currentLine.width + horizontalPadding * 2,
y: yOffset + Math.abs(topRightCornerRadius),
});
} else {
instructions.push({
type: 'L',
- x: xOffset + cornerRounding.width + horizontalPadding * 2,
+ x: xOffset + currentLine.width + horizontalPadding * 2,
y: yOffset,
});
}
const bottomRightCornerRadius = clamp(
- nextCornerRounding
+ nextLine
? textAlign === 'right'
? 0
: textAlign === 'left'
- ? (nextCornerRounding.width - cornerRounding.width) / 2
- : (nextCornerRounding.width - cornerRounding.width) / 4
+ ? (nextLine.width - currentLine.width) / 2
+ : (nextLine.width - currentLine.width) / 4
: -Infinity,
-maxCornerRadius,
maxCornerRadius,
@@ -116,8 +116,8 @@ export const createRoundedTextBox = ({
if (bottomRightCornerRadius !== 0) {
instructions.push({
type: 'L',
- x: xOffset + cornerRounding.width + horizontalPadding * 2,
- y: yOffset + cornerRounding.height - Math.abs(bottomRightCornerRadius),
+ x: xOffset + currentLine.width + horizontalPadding * 2,
+ y: yOffset + currentLine.height - Math.abs(bottomRightCornerRadius),
});
// Arc for rounded corner (bottom right)
instructions.push({
@@ -129,20 +129,20 @@ export const createRoundedTextBox = ({
sweepFlag: bottomRightCornerRadius < 0,
x:
xOffset +
- cornerRounding.width +
+ currentLine.width +
horizontalPadding * 2 +
bottomRightCornerRadius,
- y: yOffset + cornerRounding.height,
+ y: yOffset + currentLine.height,
});
} else {
instructions.push({
type: 'L',
- x: xOffset + cornerRounding.width + horizontalPadding * 2,
- y: yOffset + cornerRounding.height,
+ x: xOffset + currentLine.width + horizontalPadding * 2,
+ y: yOffset + currentLine.height,
});
}
- yOffset += cornerRounding.height;
+ yOffset += currentLine.height;
}
for (let i = textMeasurements.length - 1; i >= 0; i--) {
diff --git a/tsconfig.json b/tsconfig.json
index 74090128bc..8fd0bfd8c6 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -236,6 +236,9 @@
},
{
"path": "./packages/web-renderer"
+ },
+ {
+ "path": "./packages/rounded-text-box"
}
]
}