Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
{
"type": "remotion-element",
"version": 1,
"element": {
"assets": [],
"dependencies": [
{
"name": "@remotion/google-fonts",
"version": null
},
{
"name": "@remotion/paths",
"version": null
}
],
"dimensions": null,
"displayName": "Path Ride",
"initialProps": null,
"slug": "path-ride",
"sourceCode": "import { loadFont } from \"@remotion/google-fonts/Inter\";\nimport { getLength, getPointAtLength, getTangentAtLength } from \"@remotion/paths\";\nimport { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState, type ComponentProps } from \"react\";\nimport { Easing, Interactive, Sequence, continueRender, delayRender, interpolate, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\nconst { fontFamily: INTER } = loadFont(\"normal\", {\n weights: [\"100\", \"900\"],\n subsets: [\"latin\"],\n});\n\ninterface PathRideProps {\n text?: string;\n path?: string;\n fontSize?: number;\n fontWeight?: number;\n fontFamily?: string;\n color?: string;\n lineColor?: string;\n lineWidth?: number;\n speed?: number;\n className?: string;\n}\n\n/** An S-curve across a 1280×720 frame. */\nconst pathRideDefaultPath =\n \"M 120 560 C 360 120 560 120 640 360 S 920 600 1160 160\";\n\nconst pathRideDefaults = {\n text: \"Follow the line\",\n path: pathRideDefaultPath,\n fontSize: 44,\n fontWeight: 700,\n fontFamily: INTER,\n color: \"#fafafa\",\n lineColor: \"#D97757\",\n lineWidth: 3,\n speed: 1,\n} satisfies Required<Omit<PathRideProps, \"className\">>;\n\n/** Beats on the 30 fps clock. */\nconst pathRideTiming = {\n draw: [0, 40],\n straighten: [50, 68],\n erase: [52, 70],\n} as const;\n\n/** Gap between the head and the last letter, in em. */\nconst LEAD = 0.3;\n/** Letters fade in over this much arc length, in em. */\nconst FADE_IN = 0.5;\n/** Extra lift of the baseline above the line, in em. */\nconst LIFT = 0.08;\n/** Samples along the path; the line and the letters read positions from them. */\nconst SAMPLES = 400;\nconst MEASURE_SIZE = 100;\n/** Inter's baseline and cap height in a `line-height: 1` box, until measured. */\nconst INTER_METRICS = { baseline: 0.8637, capHeight: 0.7275 };\n\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst easeInOutCubic = Easing.inOut(Easing.cubic);\n\nfunction finite(value: number | undefined, fallback: number) {\n return typeof value === \"number\" && Number.isFinite(value) ? value : fallback;\n}\n\n/** Maps a composition frame onto the 30 fps timeline, scaled by `speed`. */\nfunction getPathRideTime(\n frame: number,\n options: { fps?: number; speed?: number } = {},\n) {\n const fps = Math.max(1, finite(options.fps, 30));\n const speed = Math.max(0, finite(options.speed, 1));\n return (Math.max(0, finite(frame, 0)) * 30 * speed) / fps;\n}\n\n/** Arc length drawn so far. */\nfunction getPathRideHead(t: number, length: number) {\n return interpolate(t, [...pathRideTiming.draw], [0, length], {\n ...CLAMP,\n easing: easeInOutCubic,\n });\n}\n\n/** Arc length erased from the start. */\nfunction getPathRideTail(t: number, length: number) {\n return interpolate(t, [...pathRideTiming.erase], [0, length], {\n ...CLAMP,\n easing: easeInOutCubic,\n });\n}\n\n/** How far the curve has pulled straight, 0..1. */\nfunction getPathRideStraighten(t: number) {\n return interpolate(t, [...pathRideTiming.straighten], [0, 1], {\n ...CLAMP,\n easing: easeInOutCubic,\n });\n}\n\n/** Frame where the line has erased and the text rests on its baseline. */\nconst pathRideLength = Math.max(\n pathRideTiming.straighten[1],\n pathRideTiming.erase[1],\n);\n\n/** Letter centers from the text start, and the total width. */\nfunction getPathRideCenters(advances: readonly number[]) {\n let x = 0;\n const centers = advances.map((a) => {\n const c = x + a / 2;\n x += a;\n return c;\n });\n return { centers, width: x };\n}\n\n/**\n * Arc length of a letter's center: it trails the head by the lead plus the\n * text after it, so the last letter follows the head most closely.\n */\nfunction getPathRideArc(\n head: number,\n center: number,\n width: number,\n fontSize: number,\n) {\n return head - LEAD * fontSize - (width - center);\n}\n\n/** Opacity of a letter from how far onto the curve it has come. */\nfunction getPathRideFade(arc: number, fontSize: number) {\n return Math.min(1, Math.max(0, arc / (FADE_IN * fontSize)));\n}\n\ninterface Point {\n x: number;\n y: number;\n}\n\ninterface PathSamples {\n length: number;\n points: Point[];\n tangents: Point[];\n}\n\n/** Points and unit tangents at evenly spaced arc lengths along `path`. */\nfunction samplePath(path: string, count = SAMPLES): PathSamples {\n const length = getLength(path);\n const n = Math.max(2, Math.round(count));\n const points: Point[] = [];\n const tangents: Point[] = [];\n for (let i = 0; i < n; i++) {\n const at = (length * i) / (n - 1);\n // Both can come back null on a degenerate path; reuse the last sample.\n const p = getPointAtLength(path, at) ?? points[i - 1] ?? { x: 0, y: 0 };\n const tangent = getTangentAtLength(path, at) ??\n tangents[i - 1] ?? { x: 1, y: 0 };\n points.push({ x: p.x, y: p.y });\n tangents.push({ x: tangent.x, y: tangent.y });\n }\n return { length, points, tangents };\n}\n\n/** Position and unit tangent at arc length `s`, clamped to the path. */\nfunction getPathSample(samples: PathSamples, s: number) {\n const n = samples.points.length;\n const u = samples.length > 0 ? (s / samples.length) * (n - 1) : 0;\n const i = Math.min(n - 2, Math.max(0, Math.floor(u)));\n const f = Math.min(1, Math.max(0, u - i));\n const a = samples.points[i];\n const b = samples.points[i + 1];\n const ta = samples.tangents[i];\n const tb = samples.tangents[i + 1];\n const tx = ta.x + (tb.x - ta.x) * f;\n const ty = ta.y + (tb.y - ta.y) * f;\n const norm = Math.hypot(tx, ty) || 1;\n return {\n x: a.x + (b.x - a.x) * f,\n y: a.y + (b.y - a.y) * f,\n tx: tx / norm,\n ty: ty / norm,\n };\n}\n\ninterface PathRideStraight {\n /** Arc length that lands on the text's left edge. */\n start: number;\n /** Reference x of the text's left edge on the straight baseline. */\n left: number;\n /** Reference y of the straight baseline. */\n baseline: number;\n}\n\ninterface PathRidePose {\n x: number;\n y: number;\n angle: number;\n}\n\n/**\n * A point of the path pulled `k` of the way from the curve to the straight\n * baseline, lifted `lift` along its normal. Arc length maps to x one to one,\n * so the text keeps its spacing while the curve pulls taut.\n */\nfunction getPathRidePoint(\n samples: PathSamples,\n s: number,\n k: number,\n straight: PathRideStraight,\n lift = 0,\n): PathRidePose {\n const p = getPathSample(samples, s);\n const x = p.x + (straight.left + (s - straight.start) - p.x) * k;\n const y = p.y + (straight.baseline - p.y) * k;\n const tx = p.tx + (1 - p.tx) * k;\n const ty = p.ty * (1 - k);\n const norm = Math.hypot(tx, ty) || 1;\n return {\n x: x + (ty / norm) * lift,\n y: y - (tx / norm) * lift,\n angle: Math.atan2(ty, tx),\n };\n}\n\n/** Polyline of the pulled path between two arc lengths. */\nfunction getPathRideLine(\n samples: PathSamples,\n from: number,\n to: number,\n k: number,\n straight: PathRideStraight,\n) {\n if (to <= from) return \"\";\n const step = samples.length / (samples.points.length - 1);\n const stops = [from];\n for (let s = Math.ceil(from / step) * step; s < to; s += step) {\n if (s > from) stops.push(s);\n }\n stops.push(to);\n return stops\n .map((s, i) => {\n const p = getPathRidePoint(samples, s, k, straight);\n return `${i === 0 ? \"M\" : \"L\"}${p.x.toFixed(2)} ${p.y.toFixed(2)}`;\n })\n .join(\"\");\n}\n\nfunction useMetrics(text: string, font: string, fallback: number) {\n const chars = Array.from(text);\n const key = chars.join(\"\");\n const [metrics, setMetrics] = useState<{\n advances: number[];\n baseline: number;\n capHeight: number;\n } | null>(null);\n\n useEffect(() => {\n const handle = delayRender(\"path-ride: measuring the text\");\n let released = false;\n const release = () => {\n if (released) return;\n released = true;\n continueRender(handle);\n };\n const measure = () => {\n if (released) return;\n const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n if (ctx) {\n ctx.font = font;\n const m = ctx.measureText(\"H\");\n const ascent = m.fontBoundingBoxAscent / MEASURE_SIZE;\n const descent = m.fontBoundingBoxDescent / MEASURE_SIZE;\n setMetrics({\n advances: Array.from(key).map(\n (c) => ctx.measureText(c).width / MEASURE_SIZE,\n ),\n baseline:\n ascent > 0\n ? (1 - (ascent + descent)) / 2 + ascent\n : INTER_METRICS.baseline,\n capHeight:\n m.actualBoundingBoxAscent > 0\n ? m.actualBoundingBoxAscent / MEASURE_SIZE\n : INTER_METRICS.capHeight,\n });\n }\n release();\n };\n document.fonts.load(font, key).then(measure, measure);\n return release;\n }, [key, font]);\n\n return (\n metrics ?? {\n advances: chars.map(() => fallback),\n baseline: INTER_METRICS.baseline,\n capHeight: INTER_METRICS.capHeight,\n }\n );\n}\n\nfunction PathRideBase({\n text = pathRideDefaults.text,\n path = pathRideDefaults.path,\n fontSize = pathRideDefaults.fontSize,\n fontWeight = pathRideDefaults.fontWeight,\n fontFamily = pathRideDefaults.fontFamily,\n color = pathRideDefaults.color,\n lineColor = pathRideDefaults.lineColor,\n lineWidth = pathRideDefaults.lineWidth,\n speed = pathRideDefaults.speed,\n className,\n}: PathRideProps) {\n const frame = useCurrentFrame();\n const { fps, width, height } = useVideoConfig();\n const unit = height / 720;\n const t = getPathRideTime(frame, { fps, speed });\n const metrics = useMetrics(\n text,\n `${fontWeight} ${MEASURE_SIZE}px ${fontFamily}`,\n 0.55,\n );\n const samples = useMemo(() => samplePath(path), [path]);\n const chars = Array.from(text);\n const advances = metrics.advances.map((a) => a * fontSize);\n const { centers, width: textWidth } = getPathRideCenters(advances);\n const length = samples.length;\n const head = getPathRideHead(t, length);\n const tail = getPathRideTail(t, length);\n const k = getPathRideStraighten(t);\n const lift = lineWidth / 2 + LIFT * fontSize;\n // The line settles `lift` below the text so the capitals center on the frame.\n const straight: PathRideStraight = {\n start: getPathRideArc(length, 0, textWidth, fontSize),\n left: 640 - textWidth / 2,\n baseline: 360 + (metrics.capHeight * fontSize) / 2 + lift,\n };\n const tip = getPathRidePoint(samples, head, k, straight);\n const tipOpacity = interpolate(\n head,\n [0, length * 0.02, length * 0.98, length],\n [0, 1, 1, 0],\n CLAMP,\n );\n // Reference space (1280×720, y down) mapped onto the composition.\n const toX = (x: number) => width / 2 + (x - 640) * unit;\n const toY = (y: number) => height / 2 + (y - 360) * unit;\n const size = fontSize * unit;\n const line = getPathRideLine(samples, tail, head, k, straight);\n\n return (\n <div\n className={className}\n style={{ position: \"absolute\", inset: 0, overflow: \"hidden\" }}\n >\n <svg\n width=\"100%\"\n height=\"100%\"\n viewBox={`0 0 ${width} ${height}`}\n style={{ position: \"absolute\", inset: 0 }}\n >\n <g\n transform={`translate(${toX(0)} ${toY(0)}) scale(${unit})`}\n fill=\"none\"\n >\n {line ? (\n <path\n d={line}\n stroke={lineColor}\n strokeWidth={lineWidth}\n strokeLinecap=\"round\"\n strokeLinejoin=\"round\"\n />\n ) : null}\n {tipOpacity > 0 ? (\n <circle\n cx={tip.x}\n cy={tip.y}\n r={lineWidth * 1.6}\n fill={lineColor}\n opacity={tipOpacity}\n />\n ) : null}\n </g>\n </svg>\n {chars.map((char, j) => {\n const arc = getPathRideArc(head, centers[j], textWidth, fontSize);\n const fade = getPathRideFade(arc, fontSize);\n if (fade <= 0) return null;\n const pose = getPathRidePoint(samples, arc, k, straight, lift);\n return (\n <span\n key={j}\n style={{\n position: \"absolute\",\n left: 0,\n top: 0,\n fontFamily,\n fontSize: size,\n lineHeight: 1,\n fontWeight,\n fontVariationSettings: `\"wght\" ${fontWeight}`,\n color,\n whiteSpace: \"pre\",\n opacity: fade,\n transformOrigin: \"0 0\",\n transform: `translate(${toX(pose.x)}px, ${toY(pose.y)}px) rotate(${pose.angle}rad) translate(-50%, ${-metrics.baseline * size}px)`,\n }}\n >\n {char}\n </span>\n );\n })}\n </div>\n );\n}\n\nconst elementSchema = {\n ...Interactive.baseSchema,\n text: {\n type: \"text-content\",\n default: \"Follow the line\",\n description: \"Text\",\n },\n path: {\n type: \"text-content\",\n default: \"M 120 560 C 360 120 560 120 640 360 S 920 600 1160 160\",\n description: \"Path (1280×720)\",\n },\n color: {\n type: \"color\",\n default: \"#111111\",\n description: \"Text color\",\n },\n lineColor: {\n type: \"color\",\n default: \"#fafafa\",\n description: \"Line color\",\n },\n fontSize: {\n type: \"number\",\n default: 44,\n min: 16,\n max: 120,\n step: 1,\n description: \"Font size\",\n hiddenFromList: false,\n },\n fontWeight: {\n type: \"number\",\n default: 700,\n min: 100,\n max: 900,\n step: 50,\n description: \"Weight\",\n hiddenFromList: false,\n },\n lineWidth: {\n type: \"number\",\n default: 3,\n min: 1,\n max: 12,\n step: 0.5,\n description: \"Line width\",\n hiddenFromList: false,\n },\n speed: {\n type: \"number\",\n default: 1,\n min: 0.25,\n max: 4,\n step: 0.25,\n description: \"Speed\",\n hiddenFromList: false,\n },\n ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"text\",\"path\",\"color\",\"lineColor\",\"fontSize\",\"fontWeight\",\"lineWidth\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n text: \"Follow the line\",\n path: \"M 120 560 C 360 120 560 120 640 360 S 920 600 1160 160\",\n color: \"#111111\",\n lineColor: \"#fafafa\",\n fontSize: 44,\n fontWeight: 700,\n lineWidth: 3,\n speed: 1,\n};\n\ntype PathRideElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof PathRideBase>;\n\nconst PathRideInner = forwardRef<\n HTMLDivElement,\n PathRideElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, ...rest }, ref) => {\n const outlineRef = useRef<HTMLDivElement>(null);\n useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);\n const componentProps: Record<string, unknown> = { ...ELEMENT_PROP_DEFAULTS };\n const sequenceProps: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(rest)) {\n if (ELEMENT_PROP_KEYS.has(key)) {\n if (value !== undefined) componentProps[key] = value;\n } else {\n sequenceProps[key] = value;\n }\n }\n return (\n <Sequence\n layout=\"none\"\n {...sequenceProps}\n controls={controls}\n name={name ?? \"<PathRide>\"}\n outlineRef={outlineRef}\n >\n <div\n ref={outlineRef}\n style={{\n position: \"relative\",\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n ...style,\n }}\n >\n <PathRideBase\n {...(componentProps as unknown as ComponentProps<typeof PathRideBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const PathRide = Interactive.withSchema({\n Component: PathRideInner,\n componentName: \"<PathRide>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 70,
"installationMode": "component-owned-sequence"
},
"durationInFrames": 70
}