🎥 Make videos programmatically with React
bar chart refactor
Jonny Burger1c9e86c294d8
diff --git a/packages/skills/skills/charts/assets/bar-chart.tsx b/packages/skills/skills/charts/assets/bar-chart.tsxindex fddf534eb7..91383d1e71 100644--- a/packages/skills/skills/charts/assets/bar-chart.tsx+++ b/packages/skills/skills/charts/assets/bar-chart.tsx@@ -1,183 +1,179 @@-import { useCurrentFrame, useVideoConfig, AbsoluteFill, spring } from "remotion";+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';-export const MyAnimation = () => {- /*- * Elegant animated bar chart showing gold prices with Y-axis, staggered spring animations, and full-width layout.- */- const frame = useCurrentFrame();- const { fps, height: videoHeight } = useVideoConfig();+const COLOR_BAR = '#D4AF37';+const COLOR_TEXT = '#ffffff';+const COLOR_MUTED = '#888888';+const COLOR_BG = '#0a0a0a';+const COLOR_AXIS = '#333333';++// Ideal composition size: 1280x720++const Title: React.FC<{title: string; opacity: number}> = ({+ title,+ opacity,+}) => (+ <div style={{textAlign: 'center', opacity, marginBottom: 40}}>+ <div style={{color: COLOR_TEXT, fontSize: 48, fontWeight: 600}}>+ {title}+ </div>+ </div>+);- const TITLE = "Gold Price 2024";- const UNIT = "USD per troy ounce";- const COLOR_BAR = "#D4AF37";- const COLOR_TEXT = "#ffffff";- const COLOR_MUTED = "#888888";- const COLOR_BG = "#0a0a0a";- const COLOR_AXIS = "#333333";+const YAxis: React.FC<{steps: number[]; height: number}> = ({+ steps,+ height,+}) => (+ <div+ style={{+ display: 'flex',+ flexDirection: 'column',+ justifyContent: 'space-between',+ height,+ paddingRight: 16,+ }}+ >+ {steps+ .slice()+ .reverse()+ .map((step) => (+ <div+ key={step}+ style={{+ color: COLOR_MUTED,+ fontSize: 20,+ textAlign: 'right',+ }}+ >+ {step.toLocaleString()}+ </div>+ ))}+ </div>+);- const PADDING = 50;- const HEADER_HEIGHT = 70;- const LABEL_HEIGHT = 32;- const BAR_GAP = 8;- const BAR_RADIUS = 4;- const TITLE_FONT_SIZE = 24;- const LABEL_FONT_SIZE = 11;- const VALUE_FONT_SIZE = 11;- const AXIS_FONT_SIZE = 12;- const STAGGER_DELAY = 5;- const HEADER_START_FRAME = 0;- const BARS_START_FRAME = 10;- const SPRING_DAMPING = 18;- const SPRING_STIFFNESS = 80;+const Bar: React.FC<{+ height: number;+ progress: number;+}> = ({height, progress}) => (+ <div+ style={{+ flex: 1,+ display: 'flex',+ flexDirection: 'column',+ justifyContent: 'flex-end',+ }}+ >+ <div+ style={{+ width: '100%',+ height,+ backgroundColor: COLOR_BAR,+ borderRadius: '8px 8px 0 0',+ opacity: progress,+ }}+ />+ </div>+);- const data = [- { month: "Jan", price: 2039 },- { month: "Feb", price: 2024 },- { month: "Mar", price: 2160 },- { month: "Apr", price: 2330 },- { month: "May", price: 2327 },- { month: "Jun", price: 2339 },- { month: "Jul", price: 2426 },- { month: "Aug", price: 2503 },- { month: "Sep", price: 2634 },- { month: "Oct", price: 2735 },- { month: "Nov", price: 2672 },- { month: "Dec", price: 2650 },- ];+const XAxis: React.FC<{+ children: React.ReactNode;+ labels: string[];+ height: number;+}> = ({children, labels, height}) => (+ <div style={{flex: 1, display: 'flex', flexDirection: 'column'}}>+ <div+ style={{+ display: 'flex',+ alignItems: 'flex-end',+ gap: 16,+ height,+ borderLeft: `2px solid ${COLOR_AXIS}`,+ borderBottom: `2px solid ${COLOR_AXIS}`,+ paddingLeft: 16,+ }}+ >+ {children}+ </div>+ <div+ style={{+ display: 'flex',+ gap: 16,+ paddingLeft: 16,+ marginTop: 12,+ }}+ >+ {labels.map((label) => (+ <div+ key={label}+ style={{+ flex: 1,+ textAlign: 'center',+ color: COLOR_MUTED,+ fontSize: 20,+ }}+ >+ {label}+ </div>+ ))}+ </div>+ </div>+);++export const MyAnimation = () => {+ const frame = useCurrentFrame();+ const {fps, height} = useVideoConfig();- const minPrice = 1900;- const maxPrice = 2800;- const priceRange = maxPrice - minPrice;- const chartHeight = videoHeight - (PADDING * 2) - HEADER_HEIGHT - LABEL_HEIGHT;- const yAxisSteps = [1900, 2100, 2300, 2500, 2700];+ const data = [+ {month: 'Jan', price: 2039},+ {month: 'Mar', price: 2160},+ {month: 'May', price: 2327},+ {month: 'Jul', price: 2426},+ {month: 'Sep', price: 2634},+ {month: 'Nov', price: 2672},+ ];- const headerOpacity = spring({- frame: frame - HEADER_START_FRAME,- fps,- config: { damping: 20, stiffness: 100 },- });+ const minPrice = 2000;+ const maxPrice = 2800;+ const priceRange = maxPrice - minPrice;+ const chartHeight = height - 280;+ const yAxisSteps = [2000, 2400, 2800];- return (- <AbsoluteFill- style={{- backgroundColor: COLOR_BG,- padding: PADDING,- display: "flex",- flexDirection: "column",- fontFamily: "Inter, sans-serif",- }}- >- {/* Header */}- <div style={{ height: HEADER_HEIGHT, opacity: headerOpacity, marginBottom: 10 }}>- <div style={{ color: COLOR_TEXT, fontSize: TITLE_FONT_SIZE, fontWeight: 600 }}>{TITLE}</div>- <div style={{ color: COLOR_MUTED, fontSize: 14, marginTop: 4 }}>{UNIT}</div>- </div>+ const headerOpacity = spring({+ frame,+ fps,+ config: {damping: 20, stiffness: 100},+ });- {/* Chart container */}- <div style={{ display: "flex", alignItems: "flex-end", width: "100%", flex: 1 }}>- {/* Y-Axis */}- <div- style={{- display: "flex",- flexDirection: "column",- justifyContent: "space-between",- height: chartHeight,- paddingRight: 12,- marginBottom: LABEL_HEIGHT,- }}- >- {yAxisSteps.slice().reverse().map((step) => (- <div- key={step}- style={{- color: COLOR_MUTED,- fontSize: AXIS_FONT_SIZE,- textAlign: "right",- minWidth: 40,- }}- >- {step.toLocaleString()}- </div>- ))}- </div>+ return (+ <AbsoluteFill+ style={{+ backgroundColor: COLOR_BG,+ padding: 60,+ display: 'flex',+ flexDirection: 'column',+ fontFamily: 'Inter, sans-serif',+ }}+ >+ <Title title="Gold Price 2024" opacity={headerOpacity} />- {/* Chart area */}- <div- style={{- display: "flex",- alignItems: "flex-end",- gap: BAR_GAP,- height: chartHeight,- flex: 1,- borderLeft: `1px solid ${COLOR_AXIS}`,- borderBottom: `1px solid ${COLOR_AXIS}`,- paddingLeft: 8,- }}- >- {data.map((item, i) => {- const delay = i * STAGGER_DELAY;- const progress = spring({- frame: frame - delay - BARS_START_FRAME,- fps,- config: { damping: SPRING_DAMPING, stiffness: SPRING_STIFFNESS },- });+ <div style={{display: 'flex', flex: 1}}>+ <YAxis steps={yAxisSteps} height={chartHeight} />+ <XAxis height={chartHeight} labels={data.map((d) => d.month)}>+ {data.map((item, i) => {+ const progress = spring({+ frame: frame - i * 5 - 10,+ fps,+ config: {damping: 18, stiffness: 80},+ });- const normalizedHeight = ((item.price - minPrice) / priceRange) * chartHeight;- const height = normalizedHeight * progress;+ const barHeight =+ ((item.price - minPrice) / priceRange) * chartHeight * progress;- return (- <div- key={i}- style={{- textAlign: "center",- flex: 1,- height: "100%",- display: "flex",- flexDirection: "column",- justifyContent: "flex-end",- }}- >- <div- style={{- width: "100%",- height,- backgroundColor: COLOR_BAR,- borderRadius: `${BAR_RADIUS}px ${BAR_RADIUS}px 0 0`,- display: "flex",- justifyContent: "center",- alignItems: "flex-start",- paddingTop: 6,- minHeight: height > 0 ? 4 : 0,- }}- >- {height > 30 && (- <span- style={{- color: COLOR_BG,- fontSize: VALUE_FONT_SIZE,- fontWeight: 600,- opacity: progress,- }}- >- {item.price.toLocaleString()}- </span>- )}- </div>- <div- style={{- color: COLOR_MUTED,- fontSize: LABEL_FONT_SIZE,- marginTop: 8,- height: LABEL_HEIGHT - 8,- }}- >- {item.month}- </div>- </div>- );- })}- </div>- </div>- </AbsoluteFill>- );+ return (+ <Bar key={item.month} height={barHeight} progress={progress} />+ );+ })}+ </XAxis>+ </div>+ </AbsoluteFill>+ );};diff --git a/packages/skills/src/Root.tsx b/packages/skills/src/Root.tsxindex 70a17cd132..6bcdea0203 100644--- a/packages/skills/src/Root.tsx+++ b/packages/skills/src/Root.tsx@@ -4,10 +4,10 @@ import {COMPOSITION_WIDTH,MyAnimation,} from '../skills/charts/assets/animated-shapes';+import {MyAnimation as BarChartAnimation} from '../skills/charts/assets/bar-chart';import {MyAnimation as ChatMessagesAnimation} from '../skills/example-chat-messages/assets/example';import {MyAnimation as CounterAnimation} from '../skills/example-counter/assets/example';import {MyAnimation as FallingSpheresAnimation} from '../skills/example-falling-spheres/assets/example';-import {MyAnimation as BarChartAnimation} from '../skills/charts/assets/bar-chart';import {MyAnimation as HistogramAnimation} from '../skills/example-histogram/assets/example';import {MyAnimation as ProgressBarAnimation} from '../skills/example-progress-bar/assets/example';import {MyAnimation as StaggeredListAnimation} from '../skills/example-staggered-list/assets/example';@@ -88,8 +88,8 @@ export const RemotionRoot = () => {component={BarChartAnimation}durationInFrames={120}fps={30}- width={1080}- height={1080}+ width={1280}+ height={720}/><Compositionid="TypewriterHighlight"