Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
{
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "times-turn",
"title": "Times Turn",
"description": "A multiplication sign flips on an orange panel next to a large 3 with a caption.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/times-turn/index.tsx",
"content": "\"use client\";\n\nimport { loadFont as loadFamiljen } from \"@remotion/google-fonts/FamiljenGrotesk\";\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport {\n AbsoluteFill,\n continueRender,\n delayRender,\n Easing,\n interpolate,\n spring,\n useCurrentFrame,\n useVideoConfig,\n} from \"remotion\";\n\nconst familjen = loadFamiljen(\"normal\", {\n weights: [\"400\", \"500\"],\n subsets: [\"latin\"],\n});\n\nexport interface TimesTurnProps {\n value?: string;\n before?: string;\n label?: string;\n accent?: string;\n background?: string;\n speed?: number;\n theme?: \"dark\" | \"light\";\n}\n\nconst ORANGE = \"#FF4F00\";\n\nconst THEMES = {\n dark: { field: \"#1E1E1E\", ink: \"#FFE7D6\", inkOnAccent: \"#1E1E1E\" },\n light: { field: \"#FFE7D6\", ink: \"#1E1E1E\", inkOnAccent: \"#1E1E1E\" },\n} as const;\n\nconst TRACK = -0.03;\nconst ARM = 0.56;\nconst STROKE = 0.085;\nconst CROSS_K = 0.86;\nconst GAP = 0.03;\n\nconst RISE_AT = 0;\nconst RISE_LEN = 16;\nconst TURN_AT = 30;\nconst PCT_AT = 32;\nconst SWELL_AT = 38;\nconst SWELL_END = 74;\nconst LABEL_AT = 64;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IN_OUT = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\ninterface Metrics {\n digits: number;\n unit: number;\n figH: number;\n baseOff: number;\n}\n\nfunction measure(value: string, unit: string): Metrics {\n const canvas = document.createElement(\"canvas\");\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) throw new Error(\"times-turn: no 2d context\");\n const fam = `\"${familjen.fontFamily}\"`;\n ctx.font = `500 100px ${fam}`;\n const n = Array.from(value).length;\n const digits = ctx.measureText(value).width / 100 + TRACK * n;\n const unitW = unit\n ? ctx.measureText(unit).width / 100 + TRACK * Array.from(unit).length\n : 0;\n const fm = ctx.measureText(\"0123456789\");\n const figH = fm.actualBoundingBoxAscent / 100;\n const asc = fm.fontBoundingBoxAscent / 100;\n const desc = fm.fontBoundingBoxDescent / 100;\n const baseOff = (1 - (asc + desc)) / 2 + asc;\n return { digits, unit: unitW, figH, baseOff };\n}\n\nfunction useMetrics(value: string, unit: string) {\n const key = `${value}\\u0001${unit}`;\n const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n const [handle] = useState(() => delayRender(\"times-turn: measuring\"));\n const released = useRef(false);\n useEffect(() => {\n let live = true;\n const [v, u] = key.split(\"\\u0001\");\n Promise.all([\n familjen.waitUntilDone(),\n document.fonts.load(`500 100px \"${familjen.fontFamily}\"`),\n document.fonts.load(`400 100px \"${familjen.fontFamily}\"`),\n ])\n .catch(() => undefined)\n .then(() => {\n if (!live) return;\n setState({ key, m: measure(v, u) });\n if (!released.current) {\n released.current = true;\n continueRender(handle);\n }\n });\n return () => {\n live = false;\n };\n }, [key, handle]);\n return state && state.key === key ? state.m : null;\n}\n\nexport function TimesTurn({\n value = \"3\",\n before = \"%\",\n label = \"more launch cuts from the same storyboard\",\n accent,\n background,\n speed = 1,\n theme = \"dark\",\n}: TimesTurnProps) {\n const t = useCurrentFrame() * speed;\n const { width: W, height: H, fps } = useVideoConfig();\n const c = THEMES[theme];\n const gid = `tt${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n const m = useMetrics(value, before);\n\n const transparent = background === \"transparent\";\n const hot = accent ?? ORANGE;\n const field = transparent ? c.field : (background ?? c.field);\n\n if (!m) {\n return (\n <div\n style={{\n position: \"absolute\",\n inset: 0,\n background: transparent ? \"transparent\" : field,\n }}\n />\n );\n }\n\n const portrait = H > W * 1.1;\n const s = portrait ? W / 1080 : Math.min(W / 1280, H / 720);\n const pad = Math.min(W, H) * 0.075;\n\n const signBox = portrait\n ? { x: 0, y: 0, w: W, h: H * 0.4 }\n : { x: 0, y: 0, w: W * 0.4, h: H };\n const numBox = portrait\n ? { x: 0, y: signBox.h, w: W, h: H - signBox.h }\n : { x: signBox.w, y: 0, w: W - signBox.w, h: H };\n\n const turn = spring({\n frame: t - TURN_AT,\n fps,\n config: { damping: 11, stiffness: 120, mass: 0.9 },\n });\n const turnPrev = spring({\n frame: t - TURN_AT - 1,\n fps,\n config: { damping: 11, stiffness: 120, mass: 0.9 },\n });\n const angle = 45 * turn;\n const armK = 1 + (CROSS_K - 1) * Math.min(1, turn);\n const lean = Math.max(-9, Math.min(9, (turn - turnPrev) * 60));\n\n const pctOut = interpolate(t, [PCT_AT, PCT_AT + 14], [0, 1], {\n ...CLAMP,\n easing: EASE_IN_OUT,\n });\n const swell = interpolate(t, [SWELL_AT, SWELL_END], [0, 1], {\n ...CLAMP,\n easing: EASE_IN_OUT,\n });\n const fill = interpolate(t, [SWELL_AT + 4, SWELL_END - 4], [0, 1], {\n ...CLAMP,\n easing: EASE_IN_OUT,\n });\n\n const labelFs = (portrait ? 48 : 34) * s;\n const labelLines = portrait ? 2 : 3;\n const labelRoom = label ? labelFs * (1.25 * labelLines + 0.9) : 0;\n const innerW = numBox.w - pad * 2;\n const F1 = Math.min(\n innerW / Math.max(0.01, m.digits),\n (numBox.h - pad * 2 - labelRoom) / m.figH,\n (signBox.w * 0.62) / ARM,\n (signBox.h * 0.62) / ARM,\n );\n const F0 = Math.min(F1 * 0.34, innerW / (m.digits + GAP + m.unit));\n const F = F0 + (F1 - F0) * swell;\n\n const base1 = numBox.y + pad + m.figH * F1;\n const base0 = numBox.y + numBox.h / 2 + (m.figH * F0) / 2;\n const baseY = base0 + (base1 - base0) * swell;\n const digitsLeft = numBox.x + pad;\n const lineTop = baseY - m.baseOff * F;\n\n const rise = (k: number) =>\n interpolate(t, [RISE_AT + k * 2, RISE_AT + k * 2 + RISE_LEN], [0, 1], {\n ...CLAMP,\n easing: EASE_OUT,\n });\n\n const signCx = signBox.x + signBox.w / 2;\n const signCy = portrait\n ? signBox.y + signBox.h / 2\n : baseY - m.figH * F * 0.5;\n const arm = ARM * F * armK;\n const stroke = STROKE * F;\n const signRise = rise(0);\n const signDy = (1 - signRise) * F * 0.9;\n const digitsRise = rise(1);\n const unitRise = rise(2);\n\n const maskTop = baseY - m.figH * F - F * 0.12;\n const maskH = m.figH * F + F * 0.16;\n\n const quietInk = `color-mix(in srgb, ${c.ink} 42%, ${field})`;\n const quietSign = `color-mix(in srgb, ${c.inkOnAccent} 45%, ${hot})`;\n const signInk = transparent ? c.ink : c.inkOnAccent;\n\n const labelWords = label ? label.split(\" \") : [];\n\n const textStyle = {\n position: \"absolute\" as const,\n fontFamily: familjen.fontFamily,\n fontWeight: 500,\n fontSize: F,\n lineHeight: `${F}px`,\n letterSpacing: `${TRACK}em`,\n whiteSpace: \"pre\" as const,\n };\n\n const signClipTop = portrait ? signBox.y : maskTop;\n const signClipH = portrait ? signBox.h : maskH;\n\n return (\n <AbsoluteFill\n style={{\n background: transparent ? \"transparent\" : field,\n overflow: \"hidden\",\n }}\n >\n {transparent ? null : (\n <div\n style={{\n position: \"absolute\",\n left: signBox.x,\n top: signBox.y,\n width: signBox.w,\n height: signBox.h,\n background: hot,\n }}\n />\n )}\n\n <svg\n width={W}\n height={H}\n style={{ position: \"absolute\", inset: 0 }}\n aria-hidden\n >\n <defs>\n <clipPath id={`${gid}s`}>\n <rect\n x={signBox.x}\n y={signClipTop}\n width={signBox.w}\n height={signClipH}\n />\n </clipPath>\n </defs>\n <g clipPath={`url(#${gid}s)`}>\n <g\n transform={`translate(${signCx.toFixed(2)} ${(signCy + signDy).toFixed(2)}) rotate(${angle.toFixed(3)})`}\n >\n {[quietSign, signInk].map((paint, k) => (\n <g key={paint} opacity={k === 0 ? 1 - fill : fill}>\n <rect\n x={-arm / 2}\n y={-stroke / 2}\n width={arm}\n height={stroke}\n style={{ fill: paint }}\n />\n <rect\n x={-stroke / 2}\n y={-arm / 2}\n width={stroke}\n height={arm}\n style={{ fill: paint }}\n />\n </g>\n ))}\n </g>\n </g>\n </svg>\n\n <div\n style={{\n position: \"absolute\",\n left: numBox.x,\n width: numBox.w,\n top: maskTop,\n height: maskH,\n overflow: \"hidden\",\n }}\n >\n <div\n style={{\n ...textStyle,\n left: digitsLeft - numBox.x,\n top: lineTop - maskTop + (1 - digitsRise) * maskH,\n transform: `skewX(${(-lean).toFixed(2)}deg)`,\n transformOrigin: `0 ${(m.baseOff * F).toFixed(1)}px`,\n }}\n >\n <span style={{ position: \"relative\", display: \"inline-block\" }}>\n <span style={{ color: quietInk, opacity: 1 - fill }}>{value}</span>\n <span\n style={{\n position: \"absolute\",\n left: 0,\n top: 0,\n opacity: fill,\n color: c.ink,\n }}\n >\n {value}\n </span>\n </span>\n </div>\n {before ? (\n <div\n style={{\n ...textStyle,\n left: digitsLeft - numBox.x + (m.digits + GAP) * F,\n top: lineTop - maskTop + (1 - unitRise) * maskH + pctOut * maskH,\n color: quietInk,\n opacity: 1 - pctOut * pctOut,\n transform: `skewX(${(-lean).toFixed(2)}deg)`,\n transformOrigin: `0 ${(m.baseOff * F).toFixed(1)}px`,\n }}\n >\n {before}\n </div>\n ) : null}\n </div>\n\n {label ? (\n <div\n style={{\n position: \"absolute\",\n left: digitsLeft,\n width: Math.min(innerW, labelFs * 14),\n bottom: H - numBox.y - numBox.h + pad - labelFs * 0.2,\n display: \"flex\",\n flexWrap: \"wrap\",\n columnGap: \"0.26em\",\n fontFamily: familjen.fontFamily,\n fontWeight: 400,\n fontSize: labelFs,\n lineHeight: 1.25,\n letterSpacing: \"-0.01em\",\n color: c.ink,\n }}\n >\n {labelWords.map((w, k) => {\n const p = interpolate(\n t,\n [LABEL_AT + k * 2, LABEL_AT + k * 2 + 16],\n [0, 1],\n { ...CLAMP, easing: EASE_OUT },\n );\n return (\n <span\n key={`${w}-${k}`}\n style={{\n display: \"inline-block\",\n opacity: p,\n transform: `translateY(${((1 - p) * labelFs * 0.7).toFixed(2)}px)`,\n }}\n >\n {w}\n </span>\n );\n })}\n </div>\n ) : null}\n </AbsoluteFill>\n );\n}\n",
"type": "registry:component",
"target": "components/remocn/times-turn.tsx"
}
],
"type": "registry:component"
}