Skip to content

🎥 Make videos programmatically with React

nice example

Jonny Burgera9db19131980
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 6710e96820..651dd30719 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -606,11 +606,11 @@ export const Index: React.FC = () => {
<Still
id="FitTextOnNLines"
component={FitTextOnNLines}
- width={800}
- height={900}
+ width={1300}
+ height={350}
schema={fitTextOnNLinesSchema}
defaultProps={{
- line: 'This is awesome, it works extremely well! Yeah! No matter how much text I am adding, it always fits on',
+ line: 'No matter how much text I am adding, the text always fits on 2 lines and there is corner rounding like on TikTok.',
maxLines: 2,
}}
/>
diff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
index d75303779a..973a886535 100644
--- a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
@@ -1,5 +1,5 @@
import {measureText, TextTransform} from '@remotion/layout-utils';
-import {getPathForCorner} from './rounded-corner';
+import {getActualBorderRadius, getPathForCorner} from './rounded-corner';
interface TikTokTextBoxProps {
lines: string[];
@@ -22,9 +22,8 @@ interface CornerRounding {
cornerBottomRight: boolean;
cornerLeft: boolean;
cornerRight: boolean;
- currLength: number | undefined;
- prevLength: number | undefined;
- nextLength: number | undefined;
+ widthDifferenceToPrevious: number | undefined;
+ widthDifferenceToNext: number | undefined;
align: React.CSSProperties['textAlign'];
}
@@ -38,7 +37,6 @@ const getCornerRoundings = ({
letterSpacing,
textTransform,
align,
- borderRadius,
}: {
lines: string[];
fontFamily: string;
@@ -48,7 +46,6 @@ const getCornerRoundings = ({
fontWeight: number;
letterSpacing: string;
textTransform: TextTransform;
- borderRadius: number;
align: React.CSSProperties['textAlign'];
}): CornerRounding[] => {
const lengths = lines.map((line) =>
@@ -73,29 +70,31 @@ const getCornerRoundings = ({
const nextLen = i < n - 1 ? lengths[i + 1] : undefined;
const currLen = lengths[i];
- const borderRadiusToTakeIntoAccount =
- align === 'center' ? borderRadius / 2 : borderRadius;
+ const widthDifferenceToPrevious =
+ prevLen !== undefined ? currLen.width - prevLen.width : undefined;
+ const widthDifferenceToNext =
+ nextLen !== undefined ? currLen.width - nextLen.width : undefined;
// Determine rounded corners
let topLeft =
isFirst ||
(prevLen !== undefined &&
- currLen.width - borderRadiusToTakeIntoAccount > prevLen.width &&
+ currLen.width - 0 > prevLen.width &&
align !== 'left');
let topRight =
isFirst ||
(prevLen !== undefined &&
- currLen.width - borderRadiusToTakeIntoAccount > prevLen.width &&
+ currLen.width - 0 > prevLen.width &&
align !== 'right');
let bottomLeft =
isLast ||
(nextLen !== undefined &&
- currLen.width - borderRadiusToTakeIntoAccount > nextLen.width &&
+ currLen.width - 0 > nextLen.width &&
align !== 'left');
let bottomRight =
isLast ||
(nextLen !== undefined &&
- currLen.width - borderRadiusToTakeIntoAccount > nextLen.width &&
+ currLen.width - 0 > nextLen.width &&
align !== 'right');
if (!isFirst && !isLast) {
@@ -142,10 +141,10 @@ const getCornerRoundings = ({
cornerBottomRight,
cornerLeft,
cornerRight,
- currLength: currLen?.width,
- prevLength: prevLen?.width,
- nextLength: nextLen?.width,
+
align,
+ widthDifferenceToPrevious,
+ widthDifferenceToNext,
};
return roundings;
});
@@ -153,15 +152,151 @@ const getCornerRoundings = ({
const getBorderRadius = (rounding: CornerRounding, radius: number) => {
return [
- rounding.topLeft ? `${radius}px` : '0',
- rounding.topRight ? `${radius}px` : '0',
- rounding.bottomRight ? `${radius}px` : '0',
- rounding.bottomLeft ? `${radius}px` : '0',
+ rounding.topLeft
+ ? `${getActualBorderRadius({desiredBorderRadius: radius, widthDifference: rounding.widthDifferenceToPrevious})}px`
+ : '0',
+ rounding.topRight
+ ? `${getActualBorderRadius({desiredBorderRadius: radius, widthDifference: rounding.widthDifferenceToPrevious})}px`
+ : '0',
+ rounding.bottomRight
+ ? `${getActualBorderRadius({desiredBorderRadius: radius, widthDifference: rounding.widthDifferenceToNext})}px`
+ : '0',
+ rounding.bottomLeft
+ ? `${getActualBorderRadius({desiredBorderRadius: radius, widthDifference: rounding.widthDifferenceToNext})}px`
+ : '0',
].join(' ');
};
export const TIKTOK_TEXT_BOX_HORIZONTAL_PADDING = 10;
+const TopLeftCorner: React.FC<{
+ bgColor: string;
+ borderRadiusValue: number;
+ widthDifference: number | undefined;
+}> = ({bgColor, borderRadiusValue, widthDifference}) => {
+ const {d, borderRadius, width} = getPathForCorner({
+ corner: 'top-right',
+ desiredBorderRadius: borderRadiusValue,
+ widthDifference,
+ });
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ left: -width,
+ top: 0,
+ width: width,
+ }}
+ >
+ <svg
+ style={{
+ width,
+ height: width,
+ }}
+ viewBox={`0 0 ${borderRadius} ${borderRadius}`}
+ >
+ <path fill={bgColor} d={d} />
+ </svg>
+ </div>
+ );
+};
+
+const TopRightCorner: React.FC<{
+ bgColor: string;
+ borderRadiusValue: number;
+ widthDifference: number | undefined;
+}> = ({bgColor, borderRadiusValue, widthDifference}) => {
+ const {d, borderRadius, width} = getPathForCorner({
+ corner: 'top-left',
+ desiredBorderRadius: borderRadiusValue,
+ widthDifference,
+ });
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ right: -width,
+ top: 0,
+ width: width,
+ }}
+ >
+ <svg
+ style={{
+ width,
+ height: width,
+ }}
+ viewBox={`0 0 ${borderRadius} ${borderRadius}`}
+ >
+ <path fill={bgColor} d={d} />
+ </svg>
+ </div>
+ );
+};
+
+const BottomLeftCorner: React.FC<{
+ bgColor: string;
+ borderRadiusValue: number;
+ widthDifference: number | undefined;
+}> = ({bgColor, borderRadiusValue, widthDifference}) => {
+ const {d, borderRadius, width} = getPathForCorner({
+ corner: 'bottom-right',
+ desiredBorderRadius: borderRadiusValue,
+ widthDifference,
+ });
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ left: -width,
+ bottom: 0,
+ width: width,
+ }}
+ >
+ <svg
+ style={{
+ width,
+ height: width,
+ }}
+ viewBox={`0 0 ${borderRadius} ${borderRadius}`}
+ >
+ <path fill={bgColor} d={d} />
+ </svg>
+ </div>
+ );
+};
+
+const BottomRightCorner: React.FC<{
+ bgColor: string;
+ borderRadiusValue: number;
+ widthDifference: number | undefined;
+}> = ({bgColor, borderRadiusValue, widthDifference}) => {
+ const {d, borderRadius, width} = getPathForCorner({
+ corner: 'bottom-left',
+ desiredBorderRadius: borderRadiusValue,
+ widthDifference,
+ });
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ right: -width,
+ bottom: 0,
+ width: width,
+ }}
+ >
+ <svg
+ style={{
+ width,
+ height: width,
+ }}
+ viewBox={`0 0 ${borderRadius} ${borderRadius}`}
+ >
+ <path fill={bgColor} d={d} />
+ </svg>
+ </div>
+ );
+};
+
const TikTokTextLine: React.FC<{
text: string;
align?: React.CSSProperties['textAlign'];
@@ -184,79 +319,39 @@ const TikTokTextLine: React.FC<{
return (
<div style={{position: 'relative'}}>
{cornerRounding.cornerTopLeft && (
- <div
- style={{
- position: 'absolute',
- left: -borderRadiusValue,
- top: 0,
- width: borderRadiusValue,
- }}
- >
- <svg viewBox={`0 0 ${borderRadiusValue} ${borderRadiusValue}`}>
- <path
- fill={bgColor}
- d={getPathForCorner('top-right', borderRadiusValue)}
- />
- </svg>
- </div>
+ <TopLeftCorner
+ bgColor={bgColor}
+ borderRadiusValue={borderRadiusValue}
+ widthDifference={cornerRounding.widthDifferenceToPrevious}
+ />
)}
{cornerRounding.cornerBottomLeft && (
- <div
- style={{
- position: 'absolute',
- left: -borderRadiusValue,
- bottom: 0,
- width: borderRadiusValue,
- }}
- >
- <svg viewBox={`0 0 ${borderRadiusValue} ${borderRadiusValue}`}>
- <path
- fill={bgColor}
- d={getPathForCorner('bottom-right', borderRadiusValue)}
- />
- </svg>
- </div>
+ <BottomLeftCorner
+ bgColor={bgColor}
+ borderRadiusValue={borderRadiusValue}
+ widthDifference={cornerRounding.widthDifferenceToNext}
+ />
)}
{cornerRounding.cornerTopRight && (
- <div
- style={{
- position: 'absolute',
- right: -borderRadiusValue,
- top: 0,
- width: borderRadiusValue,
- }}
- >
- <svg viewBox={`0 0 ${borderRadiusValue} ${borderRadiusValue}`}>
- <path
- fill={bgColor}
- d={getPathForCorner('top-left', borderRadiusValue)}
- />
- </svg>
- </div>
+ <TopRightCorner
+ bgColor={bgColor}
+ borderRadiusValue={borderRadiusValue}
+ widthDifference={cornerRounding.widthDifferenceToPrevious}
+ />
)}
{cornerRounding.cornerBottomRight && (
- <div
- style={{
- position: 'absolute',
- right: -borderRadiusValue,
- bottom: 0,
- width: borderRadiusValue,
- }}
- >
- <svg viewBox={`0 0 ${borderRadiusValue} ${borderRadiusValue}`}>
- <path
- fill={bgColor}
- d={getPathForCorner('bottom-left', borderRadiusValue)}
- />
- </svg>
- </div>
+ <BottomRightCorner
+ bgColor={bgColor}
+ borderRadiusValue={borderRadiusValue}
+ widthDifference={cornerRounding.widthDifferenceToNext}
+ />
)}
<div
style={
{
textAlign: align,
backgroundColor: bgColor ?? 'white',
- padding: `10px ${TIKTOK_TEXT_BOX_HORIZONTAL_PADDING}px`,
+ padding: `0px ${TIKTOK_TEXT_BOX_HORIZONTAL_PADDING}px`,
borderRadius: borderRadius,
width: 'fit-content',
...style,
@@ -276,7 +371,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
fontFamily = 'Arial',
bgColor = 'white',
textColor,
- borderRadius = 7,
+ borderRadius = 10,
}) => {
const fontSize = 16;
const fontWeight = 400;
@@ -290,11 +385,8 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
letterSpacing: 'normal',
textTransform: 'none',
align,
- borderRadius,
});
- console.log({roundings});
-
return (
<div
style={{
@@ -310,7 +402,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
fontFamily,
color: textColor ?? 'black',
width: 'fit-content',
- lineHeight: 1,
+ lineHeight: 1.5,
fontWeight,
}}
>
diff --git a/packages/example/src/TikTokTextbox/rounded-corner.tsx b/packages/example/src/TikTokTextbox/rounded-corner.tsx
index df490bac5d..4ae450ec0d 100644
--- a/packages/example/src/TikTokTextbox/rounded-corner.tsx
+++ b/packages/example/src/TikTokTextbox/rounded-corner.tsx
@@ -2,34 +2,66 @@ type Corner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
const magicCssBorderRadiusNumber = 0.5522847682119205;
-export const getPathForCorner = (corner: Corner, borderRadius: number) => {
+export const getActualBorderRadius = ({
+ desiredBorderRadius,
+ widthDifference,
+}: {
+ desiredBorderRadius: number;
+ widthDifference: number | undefined;
+}) => {
+ return (
+ Math.floor(
+ (widthDifference !== undefined
+ ? Math.min(Math.abs(widthDifference / 2), desiredBorderRadius)
+ : desiredBorderRadius) * window.devicePixelRatio,
+ ) / window.devicePixelRatio
+ );
+};
+
+export const getPathForCorner = ({
+ corner,
+ desiredBorderRadius,
+ widthDifference,
+}: {
+ corner: Corner;
+ desiredBorderRadius: number;
+ widthDifference: number | undefined;
+}) => {
+ const width = getActualBorderRadius({
+ desiredBorderRadius,
+ widthDifference,
+ });
+ const borderRadius = width * 100;
+
if (corner === 'top-left') {
const cp1X = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp1Y = 0;
const cp2X = 0;
const cp2Y = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const d = `M 0 0 L ${borderRadius} ${0} C ${cp1X} ${cp1Y} ${cp2X} ${cp2Y} 0 ${borderRadius}`;
- return d;
+ return {d, borderRadius, width};
} else if (corner === 'top-right') {
const cp1X = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp1Y = 0;
const cp2X = borderRadius;
const cp2Y = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const d = `M ${borderRadius} 0 L 0 ${0} C ${cp1X} ${cp1Y} ${cp2X} ${cp2Y} ${borderRadius} ${borderRadius}`;
- return d;
+ return {d, borderRadius, width};
} else if (corner === 'bottom-left') {
const cp1X = 0;
const cp1Y = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp2X = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp2Y = borderRadius;
const d = `M 0 ${borderRadius} L 0 0 C ${cp1X} ${cp1Y} ${cp2X} ${cp2Y} ${borderRadius} ${borderRadius}`;
- return d;
+ return {d, borderRadius, width};
} else if (corner === 'bottom-right') {
const cp1X = borderRadius;
const cp1Y = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp2X = borderRadius - magicCssBorderRadiusNumber * borderRadius;
const cp2Y = borderRadius;
const d = `M ${borderRadius} ${borderRadius} L ${borderRadius} 0 C ${cp1X} ${cp1Y} ${cp2X} ${cp2Y} 0 ${borderRadius}`;
- return d;
+ return {d, borderRadius, width};
}
+
+ throw new Error(`Invalid corner: ${corner}`);
};
diff --git a/packages/example/src/Title/FitTextOnNLines.tsx b/packages/example/src/Title/FitTextOnNLines.tsx
index 2ffbda79dc..70fd13e2a7 100644
--- a/packages/example/src/Title/FitTextOnNLines.tsx
+++ b/packages/example/src/Title/FitTextOnNLines.tsx
@@ -7,8 +7,8 @@ import {
TikTokTextBox,
} from '../TikTokTextbox/TikTokTextBox';
-const boxWidth = 600;
-const fontFamily = 'GT Planar';
+const boxWidth = 1200;
+const fontFamily = 'Proxima Nova';
const fontWeight = 'bold';
export const fitTextOnNLinesSchema = z.object({
@@ -25,9 +25,8 @@ export const FitTextOnNLines: React.FC<
fontFamily,
text: line,
fontWeight,
- maxFontSize: 80,
+ maxFontSize: 100,
});
- console.log({lines});
const fontSize = bestFontSize;
return (
@@ -35,7 +34,7 @@ export const FitTextOnNLines: React.FC<
style={{
justifyContent: 'center',
alignItems: 'center',
- backgroundColor: 'white',
+ backgroundColor: '#1F2429',
}}
>
<div
@@ -52,8 +51,8 @@ export const FitTextOnNLines: React.FC<
}}
>
<TikTokTextBox
- bgColor="black"
- textColor="white"
+ bgColor="white"
+ textColor="black"
lines={lines}
fontFamily={fontFamily}
textAlign="center"
diff --git a/packages/layout-utils/src/layouts/measure-text.ts b/packages/layout-utils/src/layouts/measure-text.ts
index 0ba4712aa2..5e904fab51 100644
--- a/packages/layout-utils/src/layouts/measure-text.ts
+++ b/packages/layout-utils/src/layouts/measure-text.ts
@@ -162,7 +162,12 @@ export const measureText = ({
boundingBox.height === boundingBoxOfFallbackFont.height &&
boundingBox.width === boundingBoxOfFallbackFont.width;
- if (sameAsFallbackFont && computedFallback !== computedFontFamily) {
+ // Ensure there are at least 4 unique characters, with just a few, there is more likely to be a false positive
+ if (
+ sameAsFallbackFont &&
+ computedFallback !== computedFontFamily &&
+ new Set(text).size > 4
+ ) {
const err = [
`Called measureText() with "fontFamily": ${JSON.stringify(
fontFamily,