Skip to content

🎥 Make videos programmatically with React

much simplified

JonnyBurger0fe681359863
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 1d40619016..0629d67082 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -632,9 +632,9 @@ export const Index: React.FC = () => {
height={350}
schema={fitTextOnNLinesSchema}
defaultProps={{
- line: 'No matter how much text I am adding, the text always fits on 2 lines and there is corner rounding like on TikT.',
- maxLines: 2,
- textAlign: 'center' as const,
+ line: '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,
}}
/>
<Composition
diff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
index 3fea7adddb..2dc8490971 100644
--- a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
@@ -4,191 +4,27 @@
*/
import {getBoundingBox} from '@remotion/paths';
-import {CornerRounding, getCornerRoundings} from './get-corner-roundings';
-import {getActualBorderRadius, getPathForCorner} from './rounded-corner';
+import {getCornerRoundings} from './get-corner-roundings';
import {makeSvg} from './svg';
interface TikTokTextBoxProps {
lines: string[];
textAlign: 'left' | 'center' | 'right';
fontFamily: string;
- bgColor?: string;
textColor?: string;
className?: string;
borderRadius?: number;
fontSize: number;
}
-const getBorderRadius = (rounding: CornerRounding, radius: number) => {
- return [
- 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 = 20;
-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,
- overflow: 'visible',
- }}
- 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,
- overflow: 'visible',
- }}
- 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,
- overflow: 'visible',
- }}
- 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,
- overflow: 'visible',
- }}
- viewBox={`0 0 ${borderRadius} ${borderRadius}`}
- >
- <path fill={bgColor} d={d} />
- </svg>
- </div>
- );
-};
-
const TikTokTextLine: React.FC<{
text: string;
align?: React.CSSProperties['textAlign'];
- bgColor: string;
- borderRadius: string;
className?: string;
style?: React.CSSProperties;
- cornerRounding: CornerRounding;
- borderRadiusValue: number;
-}> = ({
- text,
- align,
- bgColor,
- borderRadius,
- className,
- style,
- cornerRounding,
- borderRadiusValue,
-}) => {
+}> = ({text, align, className, style}) => {
return (
<div style={{position: 'relative'}}>
<div
@@ -196,7 +32,6 @@ const TikTokTextLine: React.FC<{
{
textAlign: align,
padding: `0px ${TIKTOK_TEXT_BOX_HORIZONTAL_PADDING}px`,
- borderRadius: borderRadius,
...style,
} as React.CSSProperties
}
@@ -212,9 +47,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
lines,
textAlign: align,
fontFamily = 'Arial',
- bgColor = 'white',
textColor,
- borderRadius = 10,
fontSize,
}) => {
const fontWeight = 400;
@@ -254,18 +87,6 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
fontWeight,
}}
>
- {lines.map((line, i) => (
- <TikTokTextLine
- key={i}
- text={line}
- align={align}
- bgColor={bgColor}
- borderRadius={getBorderRadius(roundings[i], borderRadius)}
- style={{}}
- cornerRounding={roundings[i]}
- borderRadiusValue={borderRadius}
- />
- ))}
<svg
viewBox={boundingBox.viewBox}
style={{
@@ -275,13 +96,12 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
overflow: 'visible',
}}
>
- <path
- fill="none"
- d={svg}
- stroke="rgba(255, 0, 0, 0.8)"
- strokeWidth={3}
- />
+ <path fill="white" d={svg} strokeWidth={3} />
</svg>
+
+ {lines.map((line, i) => (
+ <TikTokTextLine key={i} text={line} align={align} style={{}} />
+ ))}
</div>
);
};
@@ -305,7 +125,6 @@ export const TikTokTextBoxPlayground = () => {
lines={['Align Left', 'with two lines', 'Third Line']}
textAlign="left"
fontFamily="Arial"
- bgColor="red"
fontSize={16}
/>
<TikTokTextBox
@@ -317,7 +136,6 @@ export const TikTokTextBoxPlayground = () => {
<TikTokTextBox
lines={['Align Right', 'with two lines', 'Third Line']}
textAlign="right"
- bgColor="#FF683E"
fontFamily="Proxima Nova Semibold"
textColor="black"
fontSize={16}
diff --git a/packages/example/src/TikTokTextbox/get-corner-roundings.ts b/packages/example/src/TikTokTextbox/get-corner-roundings.ts
index 9f3b2fcbce..5e63799494 100644
--- a/packages/example/src/TikTokTextbox/get-corner-roundings.ts
+++ b/packages/example/src/TikTokTextbox/get-corner-roundings.ts
@@ -1,18 +1,6 @@
import {TextTransform, measureText} from '@remotion/layout-utils';
export interface CornerRounding {
- topLeft: boolean;
- topRight: boolean;
- bottomLeft: boolean;
- bottomRight: boolean;
- cornerTopLeft: boolean;
- cornerTopRight: boolean;
- cornerBottomLeft: boolean;
- cornerBottomRight: boolean;
- cornerLeft: boolean;
- cornerRight: boolean;
- widthDifferenceToPrevious: number | undefined;
- widthDifferenceToNext: number | undefined;
align: React.CSSProperties['textAlign'];
width: number;
height: number;
@@ -52,81 +40,12 @@ export const getCornerRoundings = ({
validateFontIsLoaded: true,
}),
);
- const n = lengths.length;
return lines.map((_, i) => {
- const isFirst = i === 0;
- const isLast = i === n - 1;
- const prevLen = i > 0 ? lengths[i - 1] : undefined;
- const nextLen = i < n - 1 ? lengths[i + 1] : undefined;
const currLen = lengths[i];
- const widthDifferenceToPrevious =
- prevLen !== undefined ? currLen.width - prevLen.width : undefined;
- const widthDifferenceToNext =
- nextLen !== undefined ? currLen.width - nextLen.width : undefined;
-
- // Determine rounded corners
- let topLeft =
- prevLen === undefined ||
- (currLen.width - 0 > prevLen.width && align !== 'left');
- let topRight =
- prevLen === undefined ||
- (currLen.width - 0 > prevLen.width && align !== 'right');
- let bottomLeft =
- nextLen === undefined ||
- (currLen.width - 0 > nextLen.width && align !== 'left');
- let bottomRight =
- nextLen === undefined ||
- (currLen.width - 0 > nextLen.width && align !== 'right');
-
- if (!isFirst && !isLast) {
- if (align === 'left') {
- topLeft = bottomLeft = false;
- } else if (align === 'right') {
- topRight = bottomRight = false;
- }
- }
-
- // Determine corner properties
- let cornerTopLeft = false;
- let cornerTopRight = false;
- let cornerBottomLeft = false;
- let cornerBottomRight = false;
- const cornerLeft = false;
- const cornerRight = false;
-
- if (align === 'left') {
- cornerBottomRight = !bottomRight;
- cornerTopRight = !topRight;
- } else if (align === 'right') {
- cornerBottomLeft = !bottomLeft;
- cornerTopLeft = !topLeft;
- } else if (align === 'center') {
- // LEFT side
-
- cornerTopLeft = !topLeft;
- cornerBottomLeft = !bottomLeft;
- // RIGHT side
-
- cornerTopRight = !topRight;
- cornerBottomRight = !bottomRight;
- }
-
const roundings: CornerRounding = {
- topLeft,
- topRight,
- bottomLeft,
- bottomRight,
- cornerTopLeft,
- cornerTopRight,
- cornerBottomLeft,
- cornerBottomRight,
- cornerLeft,
- cornerRight,
align,
- widthDifferenceToPrevious,
- widthDifferenceToNext,
width: currLen.width,
height: currLen.height,
};
diff --git a/packages/example/src/TikTokTextbox/svg.ts b/packages/example/src/TikTokTextbox/svg.ts
index 921b14b9a7..5a2443e781 100644
--- a/packages/example/src/TikTokTextbox/svg.ts
+++ b/packages/example/src/TikTokTextbox/svg.ts
@@ -39,16 +39,19 @@ export const makeSvg = ({
if (i === 0) {
instructions.push({
type: 'M',
- x: xOffset,
+ x: xOffset + CORNER_RADIUS,
y: yOffset,
});
}
- const topRightWidthDifference = prevCornerRounding
- ? prevCornerRounding.width - cornerRounding.width
- : -Infinity;
const topRightCornerRadius = clamp(
- topRightWidthDifference / 4,
+ prevCornerRounding
+ ? textAlign === 'right'
+ ? 0
+ : textAlign === 'left'
+ ? (prevCornerRounding.width - cornerRounding.width) / 2
+ : (prevCornerRounding.width - cornerRounding.width) / 4
+ : -Infinity,
-CORNER_RADIUS,
CORNER_RADIUS,
);
@@ -82,11 +85,14 @@ export const makeSvg = ({
});
}
- const bottomRightWidthDifference = nextCornerRounding
- ? nextCornerRounding.width - cornerRounding.width
- : -Infinity;
const bottomRightCornerRadius = clamp(
- bottomRightWidthDifference / 4,
+ nextCornerRounding
+ ? textAlign === 'right'
+ ? 0
+ : textAlign === 'left'
+ ? (nextCornerRounding.width - cornerRounding.width) / 2
+ : (nextCornerRounding.width - cornerRounding.width) / 4
+ : -Infinity,
-CORNER_RADIUS,
CORNER_RADIUS,
);
@@ -125,6 +131,8 @@ export const makeSvg = ({
for (let i = cornerRoundings.length - 1; i >= 0; i--) {
const cornerRounding = cornerRoundings[i];
+ const prevCornerRounding = cornerRoundings[i + 1];
+ const nextCornerRounding = cornerRoundings[i - 1];
let xOffset = 0;
if (textAlign === 'center') {
xOffset = (maxWidth - (cornerRounding.width + horizontalPadding * 2)) / 2;
@@ -132,25 +140,39 @@ export const makeSvg = ({
xOffset = maxWidth - (cornerRounding.width + horizontalPadding * 2);
}
- const bottomLeft = i === cornerRoundings.length - 1;
- const topLeft = i === 0;
+ const bottomLeftWidthDifference = prevCornerRounding
+ ? prevCornerRounding.width - cornerRounding.width
+ : -Infinity;
+
+ const bottomLeftCornerRadius = clamp(
+ prevCornerRounding
+ ? textAlign === 'left'
+ ? 0
+ : textAlign === 'right'
+ ? bottomLeftWidthDifference / 2
+ : bottomLeftWidthDifference / 4
+ : -Infinity,
+ -CORNER_RADIUS,
+ CORNER_RADIUS,
+ );
+
// Bottom Left Corner
- if (bottomLeft) {
+ if (bottomLeftCornerRadius !== 0) {
instructions.push({
type: 'L',
- x: xOffset + CORNER_RADIUS,
+ x: xOffset - bottomLeftCornerRadius,
y: yOffset,
});
// Arc for rounded corner (bottom left)
instructions.push({
type: 'A',
- rx: CORNER_RADIUS,
- ry: CORNER_RADIUS,
+ rx: Math.abs(bottomLeftCornerRadius),
+ ry: Math.abs(bottomLeftCornerRadius),
xAxisRotation: 0,
largeArcFlag: false,
- sweepFlag: true,
+ sweepFlag: bottomLeftCornerRadius < 0,
x: xOffset,
- y: yOffset - CORNER_RADIUS,
+ y: yOffset - Math.abs(bottomLeftCornerRadius),
});
} else {
instructions.push({
@@ -160,22 +182,37 @@ export const makeSvg = ({
});
}
+ const topLeftWidthDifference = nextCornerRounding
+ ? nextCornerRounding.width - cornerRounding.width
+ : -Infinity;
+ const topLeftCornerRadius = clamp(
+ nextCornerRounding
+ ? textAlign === 'left'
+ ? 0
+ : textAlign === 'right'
+ ? topLeftWidthDifference / 2
+ : topLeftWidthDifference / 4
+ : -Infinity,
+ -CORNER_RADIUS,
+ CORNER_RADIUS,
+ );
+
// Top Left Corner
- if (topLeft) {
+ if (topLeftCornerRadius !== 0) {
instructions.push({
type: 'L',
x: xOffset,
- y: yOffset - cornerRounding.height + CORNER_RADIUS,
+ y: yOffset - cornerRounding.height + Math.abs(topLeftCornerRadius),
});
// Arc for rounded corner (top left)
instructions.push({
type: 'A',
- rx: CORNER_RADIUS,
- ry: CORNER_RADIUS,
+ rx: Math.abs(topLeftCornerRadius),
+ ry: Math.abs(topLeftCornerRadius),
xAxisRotation: 0,
largeArcFlag: false,
- sweepFlag: true,
- x: xOffset + CORNER_RADIUS,
+ sweepFlag: topLeftCornerRadius < 0,
+ x: xOffset - topLeftCornerRadius,
y: yOffset - cornerRounding.height,
});
} else {
diff --git a/packages/example/src/Title/FitTextOnNLines.tsx b/packages/example/src/Title/FitTextOnNLines.tsx
index 0bc5cbc420..89d1ab09f9 100644
--- a/packages/example/src/Title/FitTextOnNLines.tsx
+++ b/packages/example/src/Title/FitTextOnNLines.tsx
@@ -13,8 +13,8 @@ const fontWeight = 'bold';
export const fitTextOnNLinesSchema = z.object({
line: z.string(),
- maxLines: z.number(),
- textAlign: z.enum(['left', 'center']),
+ maxLines: z.number().step(1),
+ textAlign: z.enum(['left', 'center', 'right']),
});
export const FitTextOnNLines: React.FC<
@@ -52,7 +52,6 @@ export const FitTextOnNLines: React.FC<
}}
>
<TikTokTextBox
- bgColor="white"
textColor="black"
lines={lines}
fontFamily={fontFamily}