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": "selection-snap",
"title": "Selection Snap",
"description": "A thin word in a loose viewfinder snaps into a design-tool selection while it goes from Thin to Black, pops its handles and counts up a size badge.",
"dependencies": ["remotion", "@remotion/google-fonts"],
"files": [
{
"path": "registry/remocn/selection-snap/index.tsx",
"content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Inter\";\nimport { type CSSProperties, useEffect, useState } from \"react\";\nimport {\n continueRender,\n delayRender,\n Easing,\n interpolate,\n interpolateColors,\n useCurrentFrame,\n useVideoConfig,\n} from \"remotion\";\n\nconst { fontFamily: INTER } = loadFont(\"normal\", {\n weights: [\"100\", \"900\"],\n subsets: [\"latin\"],\n});\n\nexport interface SelectionSnapProps {\n text?: string;\n fontSize?: number;\n fontFamily?: string;\n fromWeight?: number;\n toWeight?: number;\n color?: string;\n accent?: string;\n selectionColor?: string;\n badge?: boolean;\n speed?: number;\n className?: string;\n}\n\nexport const selectionSnapDefaults = {\n text: \"Frame\",\n fontSize: 150,\n fontFamily: INTER,\n fromWeight: 100,\n toWeight: 900,\n color: \"#fafafa\",\n accent: \"#D97757\",\n selectionColor: \"#0d99ff\",\n badge: true,\n speed: 1,\n} satisfies Required<Omit<SelectionSnapProps, \"className\">>;\n\n/** Frame on the 30 fps timeline where every channel has come to rest. */\nexport const selectionSnapLength = 38;\n\n/** Padding around the word, in em: the loose viewfinder and the snapped box. */\nexport const selectionSnapPadding = {\n loose: { x: 1.1, y: 0.9 },\n tight: { x: 0.12, y: 0 },\n} as const;\n\n/** Corners first, then edge midpoints, as fractions of the box. */\nexport const selectionSnapHandles = [\n [0, 0],\n [1, 0],\n [1, 1],\n [0, 1],\n [0.5, 0],\n [1, 0.5],\n [0.5, 1],\n [0, 0.5],\n] as const;\n\nconst CLAMP = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nconst easeOutCubic = Easing.out(Easing.cubic);\nconst easeInOutCubic = Easing.inOut(Easing.cubic);\nconst snapEase = Easing.bezier(0.34, 1.3, 0.64, 1);\nconst popEase = Easing.out(Easing.back(1.7));\n\nfunction finite(value: number | undefined, fallback: number) {\n return typeof value === \"number\" && Number.isFinite(value) ? value : fallback;\n}\n\n/** Back-eased 0 → 1 over `duration` frames from `start`, never below 0. */\nfunction pop(t: number, start: number, duration: number) {\n return Math.max(\n 0,\n interpolate(t, [start, start + duration], [0, 1], {\n ...CLAMP,\n easing: popEase,\n }),\n );\n}\n\n/** Maps a composition frame onto the 30 fps timeline, scaled by `speed`. */\nexport function getSelectionSnapTime(\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\nexport interface SelectionSnapState {\n /** Word and brackets fading in, 0..1. */\n intro: number;\n /** Viewfinder settle scale. */\n bracketScale: number;\n /** Loose (0) to tight (1) padding. Overshoots past 1 on the snap. */\n snap: number;\n /** Weight progress, 0..1. */\n weight: number;\n /** Color progress toward the accent, 0..1. */\n tint: number;\n /** Bracket arms growing along the edges; 1 closes them into a box. */\n edges: number;\n /** Scale of each handle, in `selectionSnapHandles` order. */\n handles: number[];\n /** Badge entrance, 0..1 with overshoot. */\n badge: number;\n /** Badge readout progress, 0..1. */\n count: number;\n}\n\nexport function getSelectionSnapState(t: number): SelectionSnapState {\n return {\n intro: interpolate(t, [0, 8], [0, 1], { ...CLAMP, easing: easeOutCubic }),\n bracketScale: interpolate(t, [0, 8], [1.06, 1], {\n ...CLAMP,\n easing: easeOutCubic,\n }),\n snap: interpolate(t, [14, 28], [0, 1], { ...CLAMP, easing: snapEase }),\n weight: interpolate(t, [14, 30], [0, 1], {\n ...CLAMP,\n easing: easeInOutCubic,\n }),\n tint: interpolate(t, [18, 30], [0, 1], {\n ...CLAMP,\n easing: easeInOutCubic,\n }),\n edges: interpolate(t, [16, 26], [0, 1], {\n ...CLAMP,\n easing: easeInOutCubic,\n }),\n handles: selectionSnapHandles.map((_, i) => pop(t, 24 + i, 5)),\n badge: pop(t, 24, 8),\n count: interpolate(t, [24, 38], [0, 1], { ...CLAMP, easing: easeOutCubic }),\n };\n}\n\n/** Padding in em for a snap progress. */\nexport function getSelectionSnapPadding(snap: number) {\n const { loose, tight } = selectionSnapPadding;\n return {\n x: loose.x + (tight.x - loose.x) * snap,\n y: loose.y + (tight.y - loose.y) * snap,\n };\n}\n\n/** Size of the snapped box in reference px, from the word's final width. */\nexport function getSelectionSnapSize(textWidth: number, fontSize: number) {\n const { tight } = selectionSnapPadding;\n return {\n width: Math.round(textWidth + 2 * tight.x * fontSize),\n height: Math.round(fontSize * (1 + 2 * tight.y)),\n };\n}\n\nfunction useTextWidth(text: string, font: string) {\n const [width, setWidth] = useState<number | null>(null);\n\n useEffect(() => {\n const handle = delayRender(\"selection-snap: measuring the word\");\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 setWidth(ctx.measureText(text).width);\n }\n release();\n };\n document.fonts.load(font, text).then(measure, measure);\n return release;\n }, [text, font]);\n\n return width;\n}\n\nexport function SelectionSnap({\n text = selectionSnapDefaults.text,\n fontSize = selectionSnapDefaults.fontSize,\n fontFamily = selectionSnapDefaults.fontFamily,\n fromWeight = selectionSnapDefaults.fromWeight,\n toWeight = selectionSnapDefaults.toWeight,\n color = selectionSnapDefaults.color,\n accent = selectionSnapDefaults.accent,\n selectionColor = selectionSnapDefaults.selectionColor,\n badge = selectionSnapDefaults.badge,\n speed = selectionSnapDefaults.speed,\n className,\n}: SelectionSnapProps) {\n const frame = useCurrentFrame();\n const { fps, height } = useVideoConfig();\n const unit = height / 720;\n const t = getSelectionSnapTime(frame, { fps, speed });\n const state = getSelectionSnapState(t);\n\n const size = fontSize * unit;\n const pad = getSelectionSnapPadding(state.snap);\n const weight = fromWeight + (toWeight - fromWeight) * state.weight;\n const stroke = Math.max(1, 1.5 * unit);\n const arm = 0.16 * size;\n const handleSize = 9 * unit;\n const finalWidth = useTextWidth(\n text,\n `${toWeight} ${fontSize}px ${fontFamily}`,\n );\n const readout =\n finalWidth === null ? null : getSelectionSnapSize(finalWidth, fontSize);\n\n // Percentages resolve against the box width for `width` and its height for\n // `height`, so one expression grows every arm to its edge's midpoint.\n const along = `calc(${arm}px + (50% - ${arm}px) * ${state.edges})`;\n const arms: CSSProperties[] = [\n { top: 0, left: 0, width: along, height: stroke },\n { top: 0, right: 0, width: along, height: stroke },\n { bottom: 0, left: 0, width: along, height: stroke },\n { bottom: 0, right: 0, width: along, height: stroke },\n { top: 0, left: 0, width: stroke, height: along },\n { bottom: 0, left: 0, width: stroke, height: along },\n { top: 0, right: 0, width: stroke, height: along },\n { bottom: 0, right: 0, width: stroke, height: along },\n ];\n\n return (\n <div\n className={className}\n style={{\n position: \"absolute\",\n inset: 0,\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n overflow: \"hidden\",\n }}\n >\n <div\n style={{\n position: \"relative\",\n display: \"inline-block\",\n padding: `${pad.y * size}px ${pad.x * size}px`,\n }}\n >\n <span\n style={{\n display: \"block\",\n whiteSpace: \"pre\",\n fontFamily,\n fontSize: size,\n lineHeight: 1,\n fontWeight: Math.round(weight / 100) * 100,\n fontVariationSettings: `\"wght\" ${weight.toFixed(2)}`,\n color: interpolateColors(state.tint, [0, 1], [color, accent]),\n opacity: state.intro,\n }}\n >\n {text}\n </span>\n <div\n style={{\n position: \"absolute\",\n inset: 0,\n opacity: state.intro,\n transform: `scale(${state.bracketScale})`,\n }}\n >\n {arms.map((style, i) => (\n <div\n key={i}\n style={{\n position: \"absolute\",\n background: selectionColor,\n ...style,\n }}\n />\n ))}\n {selectionSnapHandles.map(([x, y], i) => (\n <div\n key={`${x}-${y}`}\n style={{\n position: \"absolute\",\n left: `${x * 100}%`,\n top: `${y * 100}%`,\n width: handleSize,\n height: handleSize,\n boxSizing: \"border-box\",\n background: \"#ffffff\",\n border: `${stroke}px solid ${selectionColor}`,\n transform: `translate(-50%, -50%) scale(${state.handles[i]})`,\n }}\n />\n ))}\n </div>\n {badge && readout ? (\n <div\n style={{\n position: \"absolute\",\n left: \"50%\",\n top: `calc(100% + ${12 * unit}px)`,\n padding: `${3 * unit}px ${7 * unit}px`,\n borderRadius: 4 * unit,\n background: selectionColor,\n color: \"#ffffff\",\n fontFamily: INTER,\n fontSize: 14 * unit,\n lineHeight: 1.2,\n fontWeight: 600,\n fontVariationSettings: '\"wght\" 600',\n fontVariantNumeric: \"tabular-nums\",\n whiteSpace: \"nowrap\",\n opacity: Math.min(1, state.badge),\n transform: `translate(-50%, ${(1 - state.badge) * 8 * unit}px) scale(${0.6 + 0.4 * state.badge})`,\n transformOrigin: \"50% 0\",\n }}\n >\n {Math.round(readout.width * state.count)} ×{\" \"}\n {Math.round(readout.height * state.count)}\n </div>\n ) : null}\n </div>\n </div>\n );\n}\n",
"type": "registry:component",
"target": "components/remocn/selection-snap.tsx"
}
],
"type": "registry:component"
}