Skip to content

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

9.0 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/google-fonts",
        "version": null
      }
    ],
    "dimensions": null,
    "displayName": "Unmask Line",
    "initialProps": null,
    "slug": "unmask-line",
    "sourceCode": "import { loadFont as loadPlexMono } from \"@remotion/google-fonts/IBMPlexMono\";\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 plex = loadPlexMono(\"normal\", {\n  weights: [\"500\"],\n  subsets: [\"latin\"],\n});\n\ninterface UnmaskLineProps {\n  lines?: string[];\n  highlight?: number;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst GREEN = { dark: \"#00C805\", light: \"#00873C\" } as const;\n\nconst THEMES = {\n  dark: { bg: \"#000000\", ink: \"#FFFFFF\" },\n  light: { bg: \"#FFFFFF\", ink: \"#000000\" },\n} as const;\n\nconst DOT_R = 0.1;\nconst LEADING = 1.08;\nconst DOT_GAP = 1.2;\nconst DOT_LEN = 10;\nconst BREATH_AT = 18;\nconst BREATH_GAP = 1.4;\nconst BREATH_LEN = 12;\nconst OPEN_AT = 38;\nconst OPEN_GAP = 2.2;\nconst OPEN_LEN = 12;\nconst LINE_LAG = 4;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_OPEN = Easing.bezier(0.33, 0, 0.15, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Metrics {\n  adv: number;\n  cap: number;\n  xh: number;\n  base: number;\n}\n\nfunction measure(): Metrics {\n  const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n  if (!ctx) throw new Error(\"unmask-line: no 2d context\");\n  ctx.font = `500 100px \"${plex.fontFamily}\"`;\n  const m = ctx.measureText(\"HHHHHHHHHH\");\n  const asc = m.fontBoundingBoxAscent / 100;\n  const desc = m.fontBoundingBoxDescent / 100;\n  return {\n    adv: m.width / 1000,\n    cap: m.actualBoundingBoxAscent / 100,\n    xh: ctx.measureText(\"x\").actualBoundingBoxAscent / 100,\n    base: (1 - (asc + desc)) / 2 + asc,\n  };\n}\n\nfunction useMetrics() {\n  const [m, setM] = useState<Metrics | null>(null);\n  const [handle] = useState(() => delayRender(\"unmask-line: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      plex.waitUntilDone(),\n      document.fonts.load(`500 100px \"${plex.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setM(measure());\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [handle]);\n  return m;\n}\n\nfunction UnmaskLineBase({\n  lines = [\"Out of\", \"stealth.\"],\n  highlight = -1,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: UnmaskLineProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H } = useVideoConfig();\n  const c = THEMES[theme];\n  const green = accent ?? GREEN[theme];\n  const m = useMetrics();\n\n  const transparent = background === \"transparent\";\n  const stage = transparent ? \"transparent\" : (background ?? c.bg);\n\n  if (!m) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const list = lines.length > 0 ? lines : [\" \"];\n  const hl = highlight < 0 ? list.length + highlight : highlight;\n  const portrait = H > W * 1.1;\n  const margin = Math.min(W, H) * (portrait ? 0.08 : 0.09);\n  const widest = Math.max(...list.map((l) => Array.from(l).length));\n  const F = Math.min(\n    (H * (portrait ? 0.4 : 0.66)) / (list.length * LEADING),\n    (W - 2 * margin) / Math.max(1, widest * m.adv),\n  );\n  const lineH = F * LEADING;\n  const blockH = lineH * (list.length - 1) + m.cap * F;\n  const firstBase = (H - blockH) / 2 + m.cap * F;\n  const cell = m.adv * F;\n\n  let order = 0;\n  const nodes: React.ReactNode[] = [];\n  list.forEach((line, li) => {\n    const baseY = firstBase + li * lineH;\n    const color = li === hl ? green : c.ink;\n    Array.from(line).forEach((ch, ci) => {\n      if (ch === \" \") return;\n      const i = order++;\n      const x = margin + ci * cell;\n      const cx = x + cell / 2;\n      const cy = baseY - m.xh * F * 0.5;\n      const arrive = interpolate(\n        t,\n        [li * LINE_LAG + i * DOT_GAP, li * LINE_LAG + i * DOT_GAP + DOT_LEN],\n        [0, 1],\n        { ...CLAMP, easing: EASE_OUT },\n      );\n      const bt = t - (BREATH_AT + i * BREATH_GAP);\n      const breath =\n        bt > 0 && bt < BREATH_LEN ? Math.sin((bt / BREATH_LEN) * Math.PI) : 0;\n      const p = interpolate(\n        t,\n        [OPEN_AT + i * OPEN_GAP, OPEN_AT + i * OPEN_GAP + OPEN_LEN],\n        [0, 1],\n        { ...CLAMP, easing: EASE_OPEN },\n      );\n      const r0 = DOT_R * F * (0.5 + 0.5 * arrive) * (1 + 0.22 * breath);\n      const rFull = Math.hypot(cell * 0.6, F * 0.8);\n      const r = r0 + (rFull - r0) * p;\n      const dotOpacity = arrive * interpolate(p, [0, 0.4], [1, 0], CLAMP);\n      if (dotOpacity > 0.002) {\n        nodes.push(\n          <div\n            key={`d${li}-${ci}`}\n            style={{\n              position: \"absolute\",\n              left: cx - r,\n              top: cy - r,\n              width: r * 2,\n              height: r * 2,\n              borderRadius: \"50%\",\n              background: color,\n              opacity: dotOpacity,\n            }}\n          />,\n        );\n      }\n      if (p > 0) {\n        const top = baseY - m.base * F;\n        nodes.push(\n          <div\n            key={`g${li}-${ci}`}\n            style={{\n              position: \"absolute\",\n              left: x,\n              top,\n              height: F,\n              lineHeight: `${F}px`,\n              fontFamily: plex.fontFamily,\n              fontWeight: 500,\n              fontSize: F,\n              whiteSpace: \"pre\",\n              color,\n              clipPath:\n                p >= 0.999\n                  ? undefined\n                  : `circle(${r.toFixed(2)}px at ${(cx - x).toFixed(2)}px ${(cy - top).toFixed(2)}px)`,\n            }}\n          >\n            {ch}\n          </div>,\n        );\n      }\n    });\n  });\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      {nodes}\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  highlight: {\n    type: \"number\",\n    default: -1,\n    min: -10,\n    max: 10,\n    step: 1,\n    description: \"Highlight\",\n    hiddenFromList: false,\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\",\"highlight\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  theme: \"dark\",\n  highlight: -1,\n  speed: 1,\n};\n\ntype UnmaskLineElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof UnmaskLineBase>;\n\nconst UnmaskLineInner = forwardRef<\n  HTMLDivElement,\n  UnmaskLineElementProps & { 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 ?? \"<UnmaskLine>\"}\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        <UnmaskLineBase\n          {...(componentProps as unknown as ComponentProps<typeof UnmaskLineBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const UnmaskLine = Interactive.withSchema({\n  Component: UnmaskLineInner,\n  componentName: \"<UnmaskLine>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 77,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 77
}