Skip to content

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

9.2 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/google-fonts",
        "version": null
      }
    ],
    "dimensions": null,
    "displayName": "Free As in",
    "initialProps": null,
    "slug": "free-as-in",
    "sourceCode": "import { loadFont } from \"@remotion/google-fonts/AlbertSans\";\nimport { 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 albert = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\ninterface FreeAsInProps {\n  word?: string;\n  glosses?: string[];\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#141414\", ink: \"#FFFFFF\", accent: \"#3DD9FF\" },\n  light: { bg: \"#FFFFFF\", ink: \"#000000\", accent: \"#0077B6\" },\n} as const;\n\nconst DEFAULT_GLOSSES = [\n  \"as in beer.\",\n  \"as in speech.\",\n  \"as in no seats.\",\n  \"as in yours.\",\n];\n\nconst TRACK_WORD = -0.045;\nconst TRACK_GLOSS = -0.015;\nconst GLH = 1.22;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nfunction useWidths(keys: string[]) {\n  const key = keys.join(\"\\u0001\");\n  const [state, setState] = useState<{\n    key: string;\n    w: Record<string, number>;\n  } | null>(null);\n  const [handle] = useState(() => delayRender(\"free-as-in: measuring\"));\n  const released = useRef(false);\n  const ref = useRef(keys);\n  ref.current = keys;\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      albert.waitUntilDone(),\n      document.fonts.load(`400 100px \"${albert.fontFamily}\"`),\n      document.fonts.load(`500 100px \"${albert.fontFamily}\"`),\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 k of ref.current) {\n          const cut = k.indexOf(\"|\");\n          const wt = k.slice(0, cut);\n          const s = k.slice(cut + 1);\n          if (ctx) ctx.font = `${wt} 100px \"${albert.fontFamily}\"`;\n          w[k] = ctx ? ctx.measureText(s).width / 100 : s.length * 0.5;\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 FreeAsInBase({\n  word = \"Free\",\n  glosses = DEFAULT_GLOSSES,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: FreeAsInProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, durationInFrames } = useVideoConfig();\n  const T = 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 list = glosses.filter((s) => s.trim().length > 0);\n  const keys = [`500|${word}`, ...list.map((s) => `400|${s}`)];\n  const w = useWidths(keys);\n\n  if (!w) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const adv = (wt: number, s: string, track: number) =>\n    (w[`${wt}|${s}`] ?? 0) + track * Array.from(s).length;\n\n  const portrait = H > W * 1.1;\n  const n = list.length;\n  const left = W * (portrait ? 0.08 : 0.07);\n  const wordEm = Math.max(0.01, adv(500, word, TRACK_WORD));\n  const glossEm = Math.max(0.01, ...list.map((s) => adv(400, s, TRACK_GLOSS)));\n\n  let Fw: number;\n  let Fg: number;\n  let gx: number;\n  let slotY: number;\n  if (portrait) {\n    Fw = Math.min(H * 0.2, (W * 0.84) / wordEm);\n    Fg = Math.min(Fw * 0.3, (W * 0.84) / glossEm);\n    gx = left;\n    slotY = H * 0.5 + Fw * 0.15 + Fg * 1.5;\n  } else {\n    const capW = Math.min(H * 0.46, (W * 0.52) / wordEm);\n    Fw = Math.max(\n      Math.min(H * 0.28, capW),\n      Math.min(capW, (W * 0.87) / (wordEm + 0.13 + glossEm * 0.17)),\n    );\n    gx = left + wordEm * Fw + Fw * 0.13;\n    const avail = W - W * 0.06 - gx;\n    Fg = Math.min(Fw * 0.17, avail / glossEm);\n    slotY = H * 0.8;\n  }\n  const gLine = Fg * GLH;\n  const wordBase = portrait ? H * 0.5 + Fw * 0.15 : slotY;\n\n  const wordIn = interpolate(t, [0, 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const first = 22;\n  const tail = 46;\n  const step =\n    n > 0 ? Math.max(12, Math.min(30, (T - first - tail) / Math.max(1, n))) : 0;\n  const startOf = (k: number) => first + k * step;\n  const push = (k: number) =>\n    interpolate(t, [startOf(k), startOf(k) + 12], [0, 1], {\n      ...CLAMP,\n      easing: EASE_IO,\n    });\n\n  const glossNodes = list.map((s, k) => {\n    const enter = interpolate(\n      t,\n      [startOf(k) + (k > 0 ? 5 : 0), startOf(k) + (k > 0 ? 21 : 16)],\n      [0, 1],\n      {\n        ...CLAMP,\n        easing: EASE_OUT,\n      },\n    );\n    if (enter <= 0) return null;\n    let level = 0;\n    for (let j = k + 1; j < n; j++) level += push(j);\n    const last = k === n - 1;\n    const fade = interpolate(level, [0, 1, 3, 4], [1, 0.38, 0.38, 0], CLAMP);\n    if (fade <= 0) return null;\n    const y = portrait\n      ? slotY + level * gLine\n      : slotY - level * gLine + (1 - enter) * Fg * 0.7;\n    const dy = portrait ? (1 - enter) * Fg * 0.7 : 0;\n    return (\n      <text\n        key={`g${k}`}\n        x={gx}\n        y={y + dy}\n        fontSize={Fg}\n        fontWeight={400}\n        letterSpacing={`${TRACK_GLOSS}em`}\n        fill={last ? hi : c.ink}\n        opacity={enter * fade}\n      >\n        {s}\n      </text>\n    );\n  });\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      <svg\n        width={W}\n        height={H}\n        viewBox={`0 0 ${W} ${H}`}\n        style={{ position: \"absolute\", inset: 0 }}\n        fontFamily={albert.fontFamily}\n        aria-hidden\n      >\n        <text\n          x={left - Fw * 0.04}\n          y={wordBase}\n          fontSize={Fw}\n          fontWeight={500}\n          letterSpacing={`${TRACK_WORD}em`}\n          fill={c.ink}\n          opacity={wordIn}\n          transform={`translate(0 ${((1 - wordIn) * Fw * 0.12).toFixed(2)})`}\n        >\n          {word}\n        </text>\n        {glossNodes}\n      </svg>\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  word: {\n    type: \"text-content\",\n    default: \"Free\",\n    description: \"Word\",\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\",\"word\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  theme: \"dark\",\n  word: \"Free\",\n  speed: 1,\n};\n\ntype FreeAsInElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof FreeAsInBase>;\n\nconst FreeAsInInner = forwardRef<\n  HTMLDivElement,\n  FreeAsInElementProps & { 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 ?? \"<FreeAsIn>\"}\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        <FreeAsInBase\n          {...(componentProps as unknown as ComponentProps<typeof FreeAsInBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const FreeAsIn = Interactive.withSchema({\n  Component: FreeAsInInner,\n  componentName: \"<FreeAsIn>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 105,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 105
}