Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

14.7 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/google-fonts",
        "version": null
      }
    ],
    "dimensions": null,
    "displayName": "Your Name Here",
    "initialProps": null,
    "slug": "your-name-here",
    "sourceCode": "import { loadFont } from \"@remotion/google-fonts/Lexend\";\nimport React, { forwardRef, useEffect, useImperativeHandle, useRef, useState, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, continueRender, delayRender, interpolate, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\nconst lexend = loadFont(\"normal\", {\n  weights: [\"300\", \"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\ninterface YourNameHereProps {\n  lead?: string;\n  brand?: string;\n  slot?: string;\n  end?: string;\n  note?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#E8392B\", ink: \"#FFFFFF\", accent: \"#0A0A0A\" },\n  light: { bg: \"#FFFFFF\", ink: \"#0A0A0A\", accent: \"#E8392B\" },\n} as const;\n\nconst TRACK = -0.04;\nconst NOTE_TRACK = -0.01;\nconst LH = 1.02;\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_IN = Easing.bezier(0.55, 0, 0.9, 0.4);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Spec {\n  text: string;\n  weight: number;\n}\n\nfunction useWidths(specs: Spec[]) {\n  const key = specs.map((s) => `${s.weight}|${s.text}`).join(\"\\u0001\");\n  const [state, setState] = useState<{\n    key: string;\n    w: Record<string, number>;\n  } | null>(null);\n  const [handle] = useState(() => delayRender(\"your-name-here: measuring\"));\n  const released = useRef(false);\n  const ref = useRef(specs);\n  ref.current = specs;\n  useEffect(() => {\n    let live = true;\n    const weights = Array.from(new Set(ref.current.map((s) => s.weight)));\n    Promise.all([\n      lexend.waitUntilDone(),\n      ...weights.map((wt) =>\n        document.fonts.load(`${wt} 100px \"${lexend.fontFamily}\"`),\n      ),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n        const w: Record<string, number> = {};\n        for (const s of ref.current) {\n          const k = `${s.weight}|${s.text}`;\n          if (ctx) {\n            ctx.font = `${s.weight} 100px \"${lexend.fontFamily}\"`;\n            w[k] = ctx.measureText(s.text).width / 100;\n          } else {\n            w[k] = s.text.length * 0.56;\n          }\n        }\n        setState({ key, w });\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.w : null;\n}\n\nfunction greedy(\n  words: string[],\n  adv: (s: string) => number,\n  space: number,\n  maxEm: number,\n) {\n  const lines: string[][] = [];\n  let line: string[] = [];\n  let x = 0;\n  for (const word of words) {\n    const w = adv(word);\n    if (line.length > 0 && x + space + w > maxEm) {\n      lines.push(line);\n      line = [word];\n      x = w;\n    } else {\n      x += (line.length > 0 ? space : 0) + w;\n      line.push(word);\n    }\n  }\n  if (line.length > 0) lines.push(line);\n  return lines;\n}\n\nfunction balanced(\n  words: string[],\n  adv: (s: string) => number,\n  space: number,\n  maxEm: number,\n) {\n  const base = greedy(words, adv, space, maxEm);\n  if (base.length <= 1) return base;\n  let lo = 0;\n  let hi = maxEm;\n  for (let k = 0; k < 24; k++) {\n    const mid = (lo + hi) / 2;\n    if (greedy(words, adv, space, mid).length <= base.length) hi = mid;\n    else lo = mid;\n  }\n  return greedy(words, adv, space, hi);\n}\n\nconst splitWords = (s: string) => s.split(/\\s+/).filter(Boolean);\n\nfunction YourNameHereBase({\n  lead = \"A launch film for\",\n  brand = \"remocn\",\n  slot = \"yourProduct\",\n  end = \".\",\n  note = \"Swap one prop. The film is yours.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: YourNameHereProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, durationInFrames } = useVideoConfig();\n  const Tt = durationInFrames * speed;\n  const c = THEMES[theme];\n  const hi = accent ?? c.accent;\n  const stage =\n    background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n\n  const leadWords = splitWords(lead);\n  const w = useWidths([\n    { text: \" \", weight: 500 },\n    ...leadWords.map((text) => ({ text, weight: 500 })),\n    { text: brand, weight: 500 },\n    { text: slot, weight: 500 },\n    { text: \"{\", weight: 400 },\n    { text: \"}\", weight: 400 },\n    { text: end, weight: 500 },\n    { text: note, weight: 300 },\n  ]);\n\n  if (!w) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const adv = (s: string, wt = 500) =>\n    s ? (w[`${wt}|${s}`] ?? s.length * 0.56) + TRACK * Array.from(s).length : 0;\n  const space = w[\"500| \"] ?? 0.25;\n  const bw = adv(\"{\", 400) + 0.02;\n  const cw = adv(\"}\", 400) + 0.02;\n  const brandW = adv(brand);\n  const slotW = adv(slot);\n  const endW = adv(end);\n  const slotLineEm = Math.max(bw + brandW + cw, bw + slotW + cw) + endW;\n\n  const portrait = H > W * 1.1;\n  const mx = W * (portrait ? 0.07 : 0.065);\n  const availW = W - mx * 2;\n  let fs = Math.min(portrait ? W * 0.2 : H * 0.24, availW / slotLineEm);\n  let leadLines = balanced(leadWords, adv, space, availW / fs);\n  const fN0 = portrait ? W * 0.045 : H * 0.045;\n  const noteEm =\n    (w[`300|${note}`] ?? note.length * 0.56) +\n    NOTE_TRACK * Array.from(note).length;\n  const fN = note ? Math.min(fN0, availW / Math.max(0.01, noteEm)) : 0;\n  const totalH = () =>\n    (leadLines.length + 1) * LH * fs + (note ? fs * 0.34 + fN * 1.3 : 0);\n  for (let k = 0; k < 80; k++) {\n    if (\n      leadLines.length <= (portrait ? 4 : 3) &&\n      totalH() <= H * (portrait ? 0.7 : 0.84)\n    )\n      break;\n    fs *= 0.96;\n    leadLines = balanced(leadWords, adv, space, availW / fs);\n  }\n\n  const lineH = LH * fs;\n  const blockTop = (H - totalH()) / 2 - H * (portrait ? 0.02 : 0.01);\n  const slotTop = blockTop + leadLines.length * lineH;\n  const noteTop = slotTop + lineH + fs * 0.34;\n\n  const k = Math.min(1.5, Math.max(0.85, Tt / 180));\n  const braceAt = 44 * k;\n  const swapAt = braceAt + 26 * k;\n  const openAt = swapAt + 28;\n  const noteAt = openAt + 30;\n\n  const p1 = interpolate(t, [braceAt, braceAt + 16], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const out = interpolate(t, [swapAt, swapAt + 12], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IN,\n  });\n  const shut = interpolate(t, [swapAt + 4, swapAt + 18], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const open = interpolate(t, [openAt, openAt + 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const rise = interpolate(t, [openAt + 4, openAt + 22], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const noteE = interpolate(t, [noteAt, noteAt + 18], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n\n  const gapW = 0.42;\n  const inner = brandW + (gapW - brandW) * shut + (slotW - gapW) * open;\n  const brandX = bw * p1;\n  const closeX = bw * p1 + inner;\n  const endX = bw * p1 + inner + cw * p1;\n\n  let wordIndex = 0;\n  const leadNodes = leadLines.map((line, li) => (\n    <div\n      key={`l${li}`}\n      style={{\n        position: \"absolute\",\n        left: mx,\n        top: blockTop + li * lineH,\n        height: lineH,\n        lineHeight: `${lineH}px`,\n        fontSize: fs,\n        fontWeight: 500,\n        whiteSpace: \"pre\",\n        letterSpacing: `${TRACK}em`,\n        color: c.ink,\n      }}\n    >\n      {line.map((word, wi) => {\n        const s = 3 + wordIndex * 3;\n        wordIndex++;\n        const e = interpolate(t, [s, s + 18], [0, 1], {\n          ...CLAMP,\n          easing: EASE_OUT,\n        });\n        return (\n          <span key={`lw${wi}`}>\n            {wi > 0 ? \" \" : \"\"}\n            <span\n              style={{\n                display: \"inline-block\",\n                opacity: e,\n                transform: `translateY(${((1 - e) * fs * 0.28).toFixed(2)}px)`,\n              }}\n            >\n              {word}\n            </span>\n          </span>\n        );\n      })}\n    </div>\n  ));\n\n  const brandIn = interpolate(\n    t,\n    [3 + wordIndex * 3 + 2, 3 + wordIndex * 3 + 22],\n    [0, 1],\n    { ...CLAMP, easing: EASE_OUT },\n  );\n\n  const glyph = (\n    text: string,\n    x: number,\n    extra: React.CSSProperties,\n  ): React.ReactNode => (\n    <div\n      style={{\n        position: \"absolute\",\n        left: x * fs,\n        top: 0,\n        height: lineH,\n        lineHeight: `${lineH}px`,\n        whiteSpace: \"pre\",\n        ...extra,\n      }}\n    >\n      {text}\n    </div>\n  );\n\n  const clipPad = fs * 0.08;\n\n  return (\n    <AbsoluteFill\n      style={{\n        background: stage,\n        overflow: \"hidden\",\n        fontFamily: lexend.fontFamily,\n      }}\n    >\n      {leadNodes}\n      <div\n        style={{\n          position: \"absolute\",\n          left: mx,\n          top: slotTop,\n          height: lineH,\n          width: availW,\n          fontSize: fs,\n          fontWeight: 500,\n          letterSpacing: `${TRACK}em`,\n          opacity: brandIn,\n          transform: `translateY(${((1 - brandIn) * fs * 0.28).toFixed(2)}px)`,\n        }}\n      >\n        {glyph(\"{\", 0, {\n          color: hi,\n          fontWeight: 400,\n          opacity: p1,\n          transform: `translateX(${((1 - p1) * fs * 0.25).toFixed(2)}px)`,\n        })}\n        <div\n          style={{\n            position: \"absolute\",\n            left: brandX * fs,\n            top: -clipPad,\n            width: (inner + 0.03) * fs,\n            height: lineH + clipPad + fs * 0.14,\n            overflow: \"hidden\",\n          }}\n        >\n          <div\n            style={{\n              position: \"absolute\",\n              left: 0,\n              top: clipPad + out * fs * 0.55,\n              height: lineH,\n              lineHeight: `${lineH}px`,\n              whiteSpace: \"pre\",\n              color: c.ink,\n              opacity: 1 - out,\n            }}\n          >\n            {brand}\n          </div>\n          <div\n            style={{\n              position: \"absolute\",\n              left: 0,\n              top: clipPad + (1 - rise) * fs * 0.45,\n              height: lineH,\n              lineHeight: `${lineH}px`,\n              whiteSpace: \"pre\",\n              color: hi,\n              opacity: rise,\n            }}\n          >\n            {slot}\n          </div>\n        </div>\n        {glyph(\"}\", closeX, {\n          color: hi,\n          fontWeight: 400,\n          opacity: p1,\n          transform: `translateX(${((1 - p1) * -fs * 0.25).toFixed(2)}px)`,\n        })}\n        {glyph(end, endX, { color: c.ink })}\n      </div>\n      {note ? (\n        <div\n          style={{\n            position: \"absolute\",\n            left: mx,\n            top: noteTop,\n            fontSize: fN,\n            lineHeight: `${fN * 1.3}px`,\n            fontWeight: 300,\n            whiteSpace: \"pre\",\n            letterSpacing: `${NOTE_TRACK}em`,\n            color: c.ink,\n            opacity: noteE,\n            transform: `translateY(${((1 - noteE) * fN * 0.6).toFixed(2)}px)`,\n          }}\n        >\n          {note}\n        </div>\n      ) : null}\n    </AbsoluteFill>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  theme: {\n    type: \"enum\",\n    default: \"dark\",\n    variants: {\n      dark: {},\n      light: {},\n    },\n    description: \"Theme\",\n  },\n  lead: {\n    type: \"text-content\",\n    default: \"A launch film for\",\n    description: \"Lead\",\n  },\n  brand: {\n    type: \"text-content\",\n    default: \"remocn\",\n    description: \"Brand\",\n  },\n  slot: {\n    type: \"text-content\",\n    default: \"yourProduct\",\n    description: \"Slot\",\n  },\n  end: {\n    type: \"text-content\",\n    default: \".\",\n    description: \"End\",\n  },\n  note: {\n    type: \"text-content\",\n    default: \"Swap one prop. The film is yours.\",\n    description: \"Note\",\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([\"theme\",\"lead\",\"brand\",\"slot\",\"end\",\"note\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  theme: \"dark\",\n  lead: \"A launch film for\",\n  brand: \"remocn\",\n  slot: \"yourProduct\",\n  end: \".\",\n  note: \"Swap one prop. The film is yours.\",\n  speed: 1,\n};\n\ntype YourNameHereElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof YourNameHereBase>;\n\nconst YourNameHereInner = forwardRef<\n  HTMLDivElement,\n  YourNameHereElementProps & { 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 ?? \"<YourNameHere>\"}\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        <YourNameHereBase\n          {...(componentProps as unknown as ComponentProps<typeof YourNameHereBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const YourNameHere = Interactive.withSchema({\n  Component: YourNameHereInner,\n  componentName: \"<YourNameHere>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 146,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 146
}