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": [],
"dimensions": null,
"displayName": "Life Grid",
"initialProps": null,
"slug": "life-grid",
"sourceCode": "import { forwardRef, useImperativeHandle, useMemo, useRef, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, random, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ntype LifeGridProps = {\n /** Columns; rows follow the frame aspect ratio. */\n cols?: number;\n /** Share of cells alive in the seeded first generation, 0 to 1. */\n density?: number;\n /** Frames each generation is held. */\n framesPerGeneration?: number;\n /** Gap between tiles, as a fraction of the cell. */\n gap?: number;\n /** Draw the faint lattice of empty cells. */\n showGrid?: boolean;\n seed?: number;\n speed?: number;\n background?: string;\n /** Living cells. */\n color?: string;\n /** Flash color for newborn cells. */\n accent?: string;\n};\n\nconst lifeGridDefaults: Required<LifeGridProps> = {\n cols: 48,\n density: 0.24,\n framesPerGeneration: 5,\n gap: 0.16,\n showGrid: true,\n seed: 7,\n speed: 1,\n background: \"#111111\",\n color: \"#FFFFFF\",\n accent: \"#E5322D\",\n};\n\nconst fmt = (n: number) => Math.round(n * 10) / 10;\nconst grow = Easing.out(Easing.back(2));\nconst shrink = Easing.in(Easing.cubic);\nconst settle = Easing.inOut(Easing.cubic);\n\n// Generations are a pure function of the seed and grid size; computed once and\n// shared across frames (and component instances) of a render.\nconst cache = new Map<string, Uint8Array[]>();\n\nfunction generations(\n cols: number,\n rows: number,\n density: number,\n seed: number,\n count: number,\n) {\n const key = `${cols}x${rows}|${density}|${seed}`;\n const known = cache.get(key);\n if (known && known.length >= count) return known;\n const list: Uint8Array[] = known ?? [];\n if (list.length === 0) {\n const first = new Uint8Array(cols * rows);\n for (let i = 0; i < first.length; i++) {\n first[i] = random(`life-grid-${seed}-${i}`) < density ? 1 : 0;\n }\n list.push(first);\n }\n while (list.length < count) {\n const prev = list[list.length - 1];\n const next = new Uint8Array(cols * rows);\n for (let y = 0; y < rows; y++) {\n for (let x = 0; x < cols; x++) {\n let n = 0;\n for (let dy = -1; dy <= 1; dy++) {\n for (let dx = -1; dx <= 1; dx++) {\n if (dx === 0 && dy === 0) continue;\n // Wrap around the edges (torus) so the board never runs dry at the border.\n const xx = (x + dx + cols) % cols;\n const yy = (y + dy + rows) % rows;\n n += prev[yy * cols + xx];\n }\n }\n const alive = prev[y * cols + x] === 1;\n next[y * cols + x] = n === 3 || (alive && n === 2) ? 1 : 0;\n }\n }\n list.push(next);\n }\n if (cache.size > 16) cache.clear();\n cache.set(key, list);\n return list;\n}\n\nfunction LifeGridBase({\n cols = lifeGridDefaults.cols,\n density = lifeGridDefaults.density,\n framesPerGeneration = lifeGridDefaults.framesPerGeneration,\n gap = lifeGridDefaults.gap,\n showGrid = lifeGridDefaults.showGrid,\n seed = lifeGridDefaults.seed,\n speed = lifeGridDefaults.speed,\n background = lifeGridDefaults.background,\n color = lifeGridDefaults.color,\n accent = lifeGridDefaults.accent,\n}: LifeGridProps) {\n const frame = useCurrentFrame();\n const { width, height, durationInFrames } = useVideoConfig();\n const nx = Math.max(4, Math.round(cols));\n const ny = Math.max(4, Math.round((nx * height) / width));\n const fpg = Math.max(1, framesPerGeneration);\n const total = Math.ceil((durationInFrames * Math.max(0, speed)) / fpg) + 2;\n const gens = useMemo(\n () => generations(nx, ny, density, seed, total),\n [nx, ny, density, seed, total],\n );\n\n const cell = Math.min(width / nx, height / ny);\n const ox = (width - cell * nx) / 2;\n const oy = (height - cell * ny) / 2;\n const tile = cell * (1 - gap);\n\n const time = Math.max(0, frame * speed);\n const g = Math.min(gens.length - 1, Math.floor(time / fpg));\n const local = Math.min(1, (time - g * fpg) / fpg);\n const now = gens[g];\n const before = g > 0 ? gens[g - 1] : now;\n\n const square = (index: number, size: number) => {\n const x = ox + (index % nx) * cell + (cell - size) / 2;\n const y = oy + Math.floor(index / nx) * cell + (cell - size) / 2;\n const s = fmt(size);\n return `M ${fmt(x)} ${fmt(y)} h ${s} v ${s} h ${-s} Z `;\n };\n\n const bornSize = tile * (0.35 + 0.65 * grow(local));\n const dyingSize = tile * (1 - shrink(Math.min(1, local * 1.6)));\n let lattice = \"\";\n let alive = \"\";\n let fresh = \"\";\n // Newborns pop in as accent tiles, then a white tile grows from their\n // centre: a crisp flash with no pink cross-fade in between.\n const coreSize =\n tile * settle(Math.min(1, Math.max(0, (local - 0.35) / 0.65)));\n for (let i = 0; i < now.length; i++) {\n if (showGrid) lattice += square(i, tile);\n if (now[i]) {\n if (!before[i] && g > 0) {\n fresh += square(i, bornSize);\n if (coreSize > 0.5) alive += square(i, coreSize);\n } else {\n alive += square(i, tile);\n }\n } else if (before[i] && dyingSize > 0.5) {\n alive += square(i, dyingSize);\n }\n }\n\n return (\n <AbsoluteFill style={{ backgroundColor: background }}>\n <svg\n width=\"100%\"\n height=\"100%\"\n viewBox={`0 0 ${width} ${height}`}\n role=\"img\"\n aria-label=\"Game of Life grid\"\n style={{ display: \"block\" }}\n >\n {showGrid ? <path d={lattice} fill={color} fillOpacity={0.05} /> : null}\n <path d={fresh} fill={accent} />\n <path d={alive} fill={color} />\n </svg>\n </AbsoluteFill>\n );\n}\n\nconst elementSchema = {\n ...Interactive.baseSchema,\n cols: {\n type: \"number\",\n default: 48,\n min: 12,\n max: 96,\n step: 2,\n description: \"Columns\",\n hiddenFromList: false,\n },\n density: {\n type: \"number\",\n default: 0.24,\n min: 0.05,\n max: 0.6,\n step: 0.01,\n description: \"Density\",\n hiddenFromList: false,\n },\n framesPerGeneration: {\n type: \"number\",\n default: 5,\n min: 1,\n max: 15,\n step: 1,\n description: \"Frames per generation\",\n hiddenFromList: false,\n },\n gap: {\n type: \"number\",\n default: 0.16,\n min: 0,\n max: 0.4,\n step: 0.01,\n description: \"Gap\",\n hiddenFromList: false,\n },\n showGrid: {\n type: \"boolean\",\n default: true,\n description: \"Lattice\",\n },\n seed: {\n type: \"number\",\n default: 7,\n min: 1,\n max: 50,\n step: 1,\n description: \"Seed\",\n hiddenFromList: false,\n },\n background: {\n type: \"color\",\n default: \"#111111\",\n description: \"Background\",\n },\n color: {\n type: \"color\",\n default: \"#FFFFFF\",\n description: \"Cells\",\n },\n accent: {\n type: \"color\",\n default: \"#E5322D\",\n description: \"Newborns\",\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([\"cols\",\"density\",\"framesPerGeneration\",\"gap\",\"showGrid\",\"seed\",\"background\",\"color\",\"accent\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n cols: 48,\n density: 0.24,\n framesPerGeneration: 5,\n gap: 0.16,\n showGrid: true,\n seed: 7,\n background: \"#111111\",\n color: \"#FFFFFF\",\n accent: \"#E5322D\",\n speed: 1,\n};\n\ntype LifeGridElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof LifeGridBase>;\n\nconst LifeGridInner = forwardRef<\n HTMLDivElement,\n LifeGridElementProps & { 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 ?? \"<LifeGrid>\"}\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 <LifeGridBase\n {...(componentProps as unknown as ComponentProps<typeof LifeGridBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const LifeGrid = Interactive.withSchema({\n Component: LifeGridInner,\n componentName: \"<LifeGrid>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 300,
"installationMode": "component-owned-sequence"
},
"durationInFrames": 300
}