Skip to content

🎥 Make videos programmatically with React

add tiktok text box playground

Jonny Burger3dd20a89573c
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 8ea2df2c26..d8d4e1b3ac 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -129,6 +129,7 @@ import {ParseAndDownloadMedia} from './ParseAndDownloadMedia';
import {PremountOnTransitionSeries} from './PremountOnTransitionSeries';
import {SmoothTextTransition} from './SmoothTextTransition';
import {Seek} from './StudioApis/Seek';
+import {TikTokTextBoxPlayground} from './TikTokTextbox/TikTokTextBox';
import {TransitionRounding} from './TransitionRounding';
import {VisualControls} from './VisualControls';
import {VoiceVisualization} from './voice-visualization';
@@ -1608,6 +1609,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={500}
/>
+ <Composition
+ id="TikTokTextBoxes"
+ component={TikTokTextBoxPlayground}
+ width={1080}
+ height={2160}
+ fps={30}
+ durationInFrames={500}
+ />
</>
);
};
diff --git a/packages/example/src/TikTokTextbox/TikTokTextBox.tsx b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
new file mode 100644
index 0000000000..ce495c13dd
--- /dev/null
+++ b/packages/example/src/TikTokTextbox/TikTokTextBox.tsx
@@ -0,0 +1,293 @@
+interface TikTokTextBoxProps {
+ lines: string[];
+ align?: React.CSSProperties['textAlign'];
+ fontFamily?: string;
+ bgColor?: string;
+ textColor?: string;
+ className?: string;
+ borderRadius?: number;
+}
+
+interface CornerRounding {
+ TL: boolean;
+ TR: boolean;
+ BL: boolean;
+ BR: boolean;
+ classList: string[];
+}
+
+const getCornerRoundings = (
+ lines: string[],
+ align?: React.CSSProperties['textAlign'],
+): CornerRounding[] => {
+ const lengths = lines.map((line) => line.length);
+ 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];
+
+ // Determine rounded corners
+ let TL = isFirst || (prevLen !== undefined && currLen > prevLen);
+ let TR = TL;
+ let BL = isLast || (nextLen !== undefined && currLen > nextLen);
+ let BR = BL;
+
+ if (!isFirst && !isLast) {
+ if (align === 'left') {
+ TL = BL = false;
+ } else if (align === 'right') {
+ TR = BR = false;
+ }
+ }
+
+ // Build class list
+ const classList: string[] = [];
+ if (align === 'left') {
+ if (!BR) classList.push('corner-br');
+ if (!TR) classList.push('corner-tr');
+ } else if (align === 'right') {
+ if (!BL) classList.push('corner-bl');
+ if (!TL) classList.push('corner-tl');
+ } else if (align === 'center') {
+ // LEFT side
+ if (!TL && !BL) {
+ classList.push('corner-left');
+ } else {
+ if (!TL) classList.push('corner-tl');
+ if (!BL) classList.push('corner-bl');
+ }
+ // RIGHT side
+ if (!TR && !BR) {
+ classList.push('corner-right');
+ } else {
+ if (!TR) classList.push('corner-tr');
+ if (!BR) classList.push('corner-br');
+ }
+ }
+
+ return {TL, TR, BL, BR, classList};
+ });
+};
+
+const getBorderRadius = (rounding: CornerRounding, radius: number) => {
+ return [
+ rounding.TL ? `${radius}px` : '0',
+ rounding.TR ? `${radius}px` : '0',
+ rounding.BR ? `${radius}px` : '0',
+ rounding.BL ? `${radius}px` : '0',
+ ].join(' ');
+};
+
+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,
+}) => {
+ return (
+ <div
+ style={
+ {
+ position: 'relative',
+ textAlign: align,
+ backgroundColor: bgColor ?? 'white',
+ padding: '20px 10px',
+ borderRadius: borderRadius,
+ width: 'fit-content',
+ ...style,
+ } as React.CSSProperties
+ }
+ className={className}
+ >
+ {text}
+ {cornerRounding.classList.includes('corner-tl') && (
+ <div
+ style={{
+ position: 'absolute',
+ left: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(255, 0, 0, 1)',
+ borderTopRightRadius: borderRadiusValue,
+ boxShadow: `0px -${borderRadiusValue * 2}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ {cornerRounding.classList.includes('corner-bl') && (
+ <div
+ style={{
+ position: 'absolute',
+ left: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(0, 255, 0, 1)',
+ borderBottomRightRadius: borderRadiusValue,
+ boxShadow: `0px ${borderRadiusValue * 2}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ {cornerRounding.classList.includes('corner-tr') && (
+ <div
+ style={{
+ position: 'absolute',
+ right: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(0, 0, 255, 1)',
+ borderTopLeftRadius: borderRadiusValue,
+ boxShadow: `0px -${borderRadiusValue * 2}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ {cornerRounding.classList.includes('corner-br') && (
+ <div
+ style={{
+ position: 'absolute',
+ right: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(255, 255, 0, 1)',
+ borderBottomLeftRadius: borderRadiusValue,
+ boxShadow: `0px ${borderRadiusValue * 2}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ {cornerRounding.classList.includes('corner-left') && (
+ <div
+ style={{
+ position: 'absolute',
+ left: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(255, 0, 255, 1)',
+ borderTopRightRadius: borderRadiusValue,
+ borderBottomRightRadius: borderRadiusValue,
+ boxShadow: `0 -${borderRadiusValue}px 0 ${bgColor}, 0 ${borderRadiusValue}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ {cornerRounding.classList.includes('corner-right') && (
+ <div
+ style={{
+ position: 'absolute',
+ right: -borderRadiusValue,
+ top: 0,
+ width: borderRadiusValue,
+ height: '100%',
+ background: 'rgba(0, 255, 255, 1)',
+ borderTopLeftRadius: borderRadiusValue,
+ borderBottomLeftRadius: borderRadiusValue,
+ boxShadow: `0 -${borderRadiusValue}px 0 ${bgColor}, 0 ${borderRadiusValue}px 0 ${bgColor}`,
+ }}
+ />
+ )}
+ </div>
+ );
+};
+
+const TikTokTextBox: React.FC<TikTokTextBoxProps> = ({
+ lines,
+ align,
+ fontFamily,
+ bgColor = 'white',
+ textColor,
+ borderRadius = 7,
+}) => {
+ const roundings = getCornerRoundings(lines, align);
+
+ return (
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'column',
+ alignItems:
+ align === 'left'
+ ? 'flex-start'
+ : align === 'right'
+ ? 'flex-end'
+ : 'center',
+ textAlign: align,
+ fontFamily: fontFamily,
+ color: textColor ?? 'black',
+ width: 'fit-content',
+ }}
+ >
+ {lines.map((line, i) => (
+ <TikTokTextLine
+ key={i}
+ text={line}
+ align={align}
+ bgColor={bgColor}
+ borderRadius={getBorderRadius(roundings[i], borderRadius)}
+ style={{}}
+ className={`tiktok-text-line ${roundings[i].classList.join(' ')}`}
+ cornerRounding={roundings[i]}
+ borderRadiusValue={borderRadius}
+ />
+ ))}
+ </div>
+ );
+};
+
+export const TikTokTextBoxPlayground = () => {
+ return (
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'column',
+ alignItems: 'center',
+ justifyContent: 'center',
+ height: '100%',
+ padding: '0.4em',
+ backgroundColor: '#d2d9d4',
+ fontSize: '4em',
+ gap: '0.2em',
+ }}
+ >
+ <TikTokTextBox
+ lines={['Align Left', 'with two lines', 'Third Line']}
+ align="left"
+ fontFamily="Arial"
+ bgColor="red"
+ />
+ <TikTokTextBox
+ lines={['Align Center', 'short', 'Third Line']}
+ align="center"
+ fontFamily="Proxima Nova Semibold"
+ />
+ <TikTokTextBox
+ lines={['Align Right', 'with two lines', 'Third Line']}
+ align="right"
+ bgColor="#FF683E"
+ textColor="black"
+ />
+ <TikTokTextBox
+ lines={['short1', 'short']}
+ align="center"
+ fontFamily="Proxima Nova Semibold"
+ />
+ </div>
+ );
+};