🎥 Make videos programmatically with React
cool
JonnyBurger480960c94727
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex 056a596a74..d7fde5b74b 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -634,6 +634,7 @@ export const Index: React.FC = () => {defaultProps={{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,+ textAlign: 'center',}}/><Compositiondiff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsxindex 0a9e7c93bc..fa761f6020 100644--- a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx@@ -3,157 +3,21 @@* and improved: https://discord.com/channels/809501355504959528/833092843290624050/1379446302903107624*/-import {measureText, TextTransform} from '@remotion/layout-utils';+import {getBoundingBox} from '@remotion/paths';+import {CornerRounding, getCornerRoundings} from './get-corner-roundings';import {getActualBorderRadius, getPathForCorner} from './rounded-corner';+import {makeSvg} from './svg';interface TikTokTextBoxProps {lines: string[];- textAlign: React.CSSProperties['textAlign'];+ textAlign: 'left' | 'center' | 'right';fontFamily: string;bgColor?: string;textColor?: string;className?: string;borderRadius?: number;-}--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'];-}--const getCornerRoundings = ({- lines,- fontFamily,- fontSize,- additionalStyles,- fontVariantNumeric,- fontWeight,- letterSpacing,- textTransform,- align,-}: {- lines: string[];- fontFamily: string;fontSize: number;- additionalStyles: React.CSSProperties;- fontVariantNumeric: string;- fontWeight: number;- letterSpacing: string;- textTransform: TextTransform;- align: React.CSSProperties['textAlign'];-}): CornerRounding[] => {- const lengths = lines.map((line) =>- measureText({- text: line,- fontFamily,- fontSize,- additionalStyles,- fontVariantNumeric,- fontWeight,- letterSpacing,- textTransform,- 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 =- isFirst ||- (prevLen !== undefined &&- currLen.width - 0 > prevLen.width &&- align !== 'left');- let topRight =- isFirst ||- (prevLen !== undefined &&- currLen.width - 0 > prevLen.width &&- align !== 'right');- let bottomLeft =- isLast ||- (nextLen !== undefined &&- currLen.width - 0 > nextLen.width &&- align !== 'left');- let bottomRight =- isLast ||- (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,- };- return roundings;- });-};+}const getBorderRadius = (rounding: CornerRounding, radius: number) => {return [@@ -381,8 +245,8 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({bgColor = 'white',textColor,borderRadius = 10,+ fontSize,}) => {- const fontSize = 16;const fontWeight = 400;const roundings = getCornerRoundings({lines,@@ -395,6 +259,8 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({textTransform: 'none',align,});+ const svg = makeSvg(roundings, align);+ const boundingBox = getBoundingBox(svg);return (<div@@ -413,6 +279,7 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({width: 'fit-content',lineHeight: 1.5,fontWeight,+ position: 'relative',}}>{lines.map((line, i) => (@@ -427,6 +294,16 @@ export const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({borderRadiusValue={borderRadius}/>))}+ <svg+ viewBox={boundingBox.viewBox}+ style={{+ position: 'absolute',+ width: boundingBox.width,+ height: boundingBox.height,+ }}+ >+ <path d={svg} fill="rgba(255, 0, 0, 0.5)" />+ </svg></div>);};@@ -451,11 +328,13 @@ export const TikTokTextBoxPlayground = () => {textAlign="left"fontFamily="Arial"bgColor="red"+ fontSize={16}/><TikTokTextBoxlines={['Align Center', 'short', 'Third Line']}textAlign="center"fontFamily="Proxima Nova Semibold"+ fontSize={16}/><TikTokTextBoxlines={['Align Right', 'with two lines', 'Third Line']}@@ -463,11 +342,13 @@ export const TikTokTextBoxPlayground = () => {bgColor="#FF683E"fontFamily="Proxima Nova Semibold"textColor="black"+ fontSize={16}/><TikTokTextBoxlines={['short1', 'short']}textAlign="center"fontFamily="Proxima Nova Semibold"+ fontSize={16}/></div>);diff --git a/packages/example/src/TikTokTextbox/get-corner-roundings.ts b/packages/example/src/TikTokTextbox/get-corner-roundings.tsnew file mode 100644index 0000000000..1b78aa6078--- /dev/null+++ b/packages/example/src/TikTokTextbox/get-corner-roundings.ts@@ -0,0 +1,144 @@+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;+}++export const getCornerRoundings = ({+ lines,+ fontFamily,+ fontSize,+ additionalStyles,+ fontVariantNumeric,+ fontWeight,+ letterSpacing,+ textTransform,+ align,+}: {+ lines: string[];+ fontFamily: string;+ fontSize: number;+ additionalStyles: React.CSSProperties;+ fontVariantNumeric: string;+ fontWeight: number;+ letterSpacing: string;+ textTransform: TextTransform;+ align: React.CSSProperties['textAlign'];+}): CornerRounding[] => {+ const lengths = lines.map((line) =>+ measureText({+ text: line,+ fontFamily,+ fontSize,+ additionalStyles,+ fontVariantNumeric,+ fontWeight,+ letterSpacing,+ textTransform,+ 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 =+ isFirst ||+ (prevLen !== undefined &&+ currLen.width - 0 > prevLen.width &&+ align !== 'left');+ let topRight =+ isFirst ||+ (prevLen !== undefined &&+ currLen.width - 0 > prevLen.width &&+ align !== 'right');+ let bottomLeft =+ isLast ||+ (nextLen !== undefined &&+ currLen.width - 0 > nextLen.width &&+ align !== 'left');+ let bottomRight =+ isLast ||+ (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,+ };++ return roundings;+ });+};diff --git a/packages/example/src/TikTokTextbox/svg.ts b/packages/example/src/TikTokTextbox/svg.tsnew file mode 100644index 0000000000..88ad13d4ed--- /dev/null+++ b/packages/example/src/TikTokTextbox/svg.ts@@ -0,0 +1,53 @@+import {Instruction, serializeInstructions} from '@remotion/paths';+import {CornerRounding} from './get-corner-roundings';++export const makeSvg = (+ cornerRoundings: CornerRounding[],+ textAlign: 'left' | 'center' | 'right',+) => {+ const instructions: Instruction[] = [];++ let maxWidth = 0;+ for (const cornerRounding of cornerRoundings) {+ maxWidth = Math.max(maxWidth, cornerRounding.width);+ }++ let yOffset = 0;++ for (const cornerRounding of cornerRoundings) {+ let xOffset = 0;+ if (textAlign === 'center') {+ xOffset = (maxWidth - cornerRounding.width) / 2;+ } else if (textAlign === 'right') {+ xOffset = maxWidth - cornerRounding.width;+ }+ instructions.push({+ type: 'M',+ x: xOffset,+ y: yOffset,+ });+ instructions.push({+ type: 'L',+ x: xOffset + cornerRounding.width,+ y: yOffset,+ });+ instructions.push({+ type: 'L',+ x: xOffset + cornerRounding.width,+ y: yOffset + cornerRounding.height,+ });+ instructions.push({+ type: 'L',+ x: xOffset,+ y: yOffset + cornerRounding.height,+ });+ instructions.push({+ type: 'L',+ x: xOffset,+ y: yOffset,+ });+ yOffset += cornerRounding.height;+ }++ return serializeInstructions(instructions);+};diff --git a/packages/example/src/Title/FitTextOnNLines.tsx b/packages/example/src/Title/FitTextOnNLines.tsxindex 70fd13e2a7..0bc5cbc420 100644--- a/packages/example/src/Title/FitTextOnNLines.tsx+++ b/packages/example/src/Title/FitTextOnNLines.tsx@@ -14,11 +14,12 @@ const fontWeight = 'bold';export const fitTextOnNLinesSchema = z.object({line: z.string(),maxLines: z.number(),+ textAlign: z.enum(['left', 'center']),});export const FitTextOnNLines: React.FC<z.infer<typeof fitTextOnNLinesSchema>-> = ({line, maxLines}) => {+> = ({line, maxLines, textAlign}) => {const {fontSize: bestFontSize, lines} = fitTextOnNLines({maxLines,maxBoxWidth: boxWidth - TIKTOK_TEXT_BOX_HORIZONTAL_PADDING * 2,@@ -55,8 +56,9 @@ export const FitTextOnNLines: React.FC<textColor="black"lines={lines}fontFamily={fontFamily}- textAlign="center"- ></TikTokTextBox>+ textAlign={textAlign}+ fontSize={fontSize}+ /></div></AbsoluteFill>);