Skip to content

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

9.9 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/google-fonts",
        "version": null
      }
    ],
    "dimensions": null,
    "displayName": "Not Yet",
    "initialProps": null,
    "slug": "not-yet",
    "sourceCode": "import { loadFont } from \"@remotion/google-fonts/GolosText\";\nimport React, { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, 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 golos = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\ninterface NotYetProps {\n  lines?: string[];\n  word?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#1D2B53\", ink: \"#FFFFFF\", accent: \"#FFB38A\", rule: 0.22 },\n  light: { bg: \"#FFF4EC\", ink: \"#1D2B53\", accent: \"#D9623B\", rule: 0.2 },\n} as const;\n\nconst DEFAULT_LINES = [\n  \"It can’t write your script.\",\n  \"It can’t pick your music.\",\n  \"It can’t voice your launch.\",\n];\n\nconst WORD_GAP = 3;\nconst WORD_RISE = 12;\nconst YET_PAUSE = 9;\nconst YET_HOLD = 20;\nconst STEP_DUR = 16;\nconst OLD_SCALE = 0.58;\nconst OLD_INK = 0.4;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_STEP = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nfunction splitWords(s: string) {\n  return s.split(/\\s+/).filter(Boolean);\n}\n\nfunction schedule(lines: string[][]) {\n  const starts: number[] = [];\n  const yets: number[] = [];\n  let at = 0;\n  for (const words of lines) {\n    starts.push(at);\n    const set =\n      (at === 0 ? 0 : STEP_DUR - 6) +\n      2 +\n      (words.length - 1) * WORD_GAP +\n      WORD_RISE;\n    const yet = at + set + YET_PAUSE;\n    yets.push(yet);\n    at = yet + YET_HOLD;\n  }\n  const end = (yets[yets.length - 1] ?? 0) + 10;\n  return { starts, yets, end };\n}\n\nfunction NotYetBase({\n  lines = DEFAULT_LINES,\n  word = \"Yet.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: NotYetProps) {\n  const frame = useCurrentFrame();\n  const { width: W, height: H, durationInFrames } = useVideoConfig();\n  const c = THEMES[theme];\n  const hi = accent ?? c.accent;\n  const stage =\n    background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n\n  const wordLines = lines.map(splitWords);\n  const plan = schedule(wordLines);\n  const pace = Math.max(1, (plan.end + 30) / Math.max(1, durationInFrames));\n  const t = frame * speed * pace;\n\n  const portrait = H > W * 1.1;\n  const margin = W * (portrait ? 0.08 : 0.07);\n  const size = Math.min(H * 0.105, W * (portrait ? 0.125 : 0.063));\n  const baseline = H * (portrait ? 0.7 : 0.765);\n  const blockW = W - margin * 2;\n  const gap = size * 0.34;\n\n  const refs = useRef<(HTMLDivElement | null)[]>([]);\n  const [handle] = useState(() => delayRender(\"not-yet: measuring\"));\n  const released = useRef(false);\n  const [fontsOk, setFontsOk] = useState(false);\n  const [heights, setHeights] = useState<number[] | null>(null);\n\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      golos.waitUntilDone(),\n      document.fonts.load(`500 100px \"${golos.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (live) setFontsOk(true);\n      });\n    return () => {\n      live = false;\n    };\n  }, []);\n\n  useLayoutEffect(() => {\n    if (!fontsOk) return;\n    const next = wordLines.map((_, i) => refs.current[i]?.offsetHeight ?? 0);\n    setHeights((prev) =>\n      prev && prev.length === next.length && prev.every((v, i) => v === next[i])\n        ? prev\n        : next,\n    );\n  });\n\n  useEffect(() => {\n    if (heights && !released.current) {\n      released.current = true;\n      continueRender(handle);\n    }\n  }, [heights, handle]);\n\n  const stepIn = wordLines.map((_, i) =>\n    i === 0\n      ? 1\n      : interpolate(t, [plan.starts[i], plan.starts[i] + STEP_DUR], [0, 1], {\n          ...CLAMP,\n          easing: EASE_STEP,\n        }),\n  );\n  const ages = wordLines.map((_, i) => {\n    let a = 0;\n    for (let j = i + 1; j < wordLines.length; j++) a += stepIn[j];\n    return a;\n  });\n  const scaleOf = (a: number) => interpolate(a, [0, 1], [1, OLD_SCALE], CLAMP);\n\n  const ruleIn = interpolate(t, [0, 22], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n\n  const rise = (start: number, dur = WORD_RISE) =>\n    interpolate(t, [start, start + dur], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n\n  const text: React.CSSProperties = {\n    fontFamily: golos.fontFamily,\n    fontWeight: 500,\n    fontSize: size,\n    lineHeight: 1.08,\n    letterSpacing: \"-0.03em\",\n  };\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      <div\n        style={{\n          position: \"absolute\",\n          left: margin,\n          top: baseline + size * 0.2,\n          width: blockW,\n          height: Math.max(1, Math.round(H * 0.0018)),\n          background: c.ink,\n          opacity: c.rule,\n          transformOrigin: \"0 50%\",\n          transform: `scaleX(${ruleIn.toFixed(4)})`,\n        }}\n      />\n      {wordLines.map((words, i) => {\n        const a = ages[i];\n        const sc = scaleOf(a);\n        let lift = 0;\n        if (heights) {\n          for (let j = i + 1; j < wordLines.length; j++) {\n            lift += (heights[j] * scaleOf(ages[j]) + gap) * stepIn[j];\n          }\n        }\n        const inkAlpha = interpolate(a, [0, 1], [1, OLD_INK], CLAMP);\n        const gone = interpolate(a, [2.2, 3], [1, 0], CLAMP);\n        const s0 = plan.starts[i] + (i === 0 ? 0 : STEP_DUR - 6);\n        const yetT = interpolate(t, [plan.yets[i], plan.yets[i] + 8], [0, 1], {\n          ...CLAMP,\n          easing: EASE_OUT,\n        });\n        return (\n          <div\n            key={`${i}-${words.join(\" \")}`}\n            ref={(el) => {\n              refs.current[i] = el;\n            }}\n            style={{\n              ...text,\n              position: \"absolute\",\n              left: margin,\n              bottom: H - baseline + lift,\n              width: blockW,\n              transformOrigin: \"0 100%\",\n              transform: `scale(${sc.toFixed(4)})`,\n              opacity: heights ? gone : 0,\n              textWrap: \"balance\",\n            }}\n          >\n            {words.map((w, wi) => {\n              const p = rise(s0 + 2 + wi * WORD_GAP);\n              return (\n                <span\n                  key={`${wi}-${w}`}\n                  style={{\n                    display: \"inline-block\",\n                    marginRight: \"0.24em\",\n                    color: c.ink,\n                    opacity: p * inkAlpha,\n                    transform: `translateY(${((1 - p) * 0.3).toFixed(3)}em)`,\n                  }}\n                >\n                  {w}\n                </span>\n              );\n            })}\n            <span\n              style={{\n                display: \"inline-block\",\n                color: hi,\n                opacity: yetT,\n                transform: `translateX(${((1 - yetT) * 0.35).toFixed(3)}em)`,\n              }}\n            >\n              {word}\n            </span>\n          </div>\n        );\n      })}\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: \"Yet.\",\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: \"Yet.\",\n  speed: 1,\n};\n\ntype NotYetElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof NotYetBase>;\n\nconst NotYetInner = forwardRef<\n  HTMLDivElement,\n  NotYetElementProps & { 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 ?? \"<NotYet>\"}\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        <NotYetBase\n          {...(componentProps as unknown as ComponentProps<typeof NotYetBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const NotYet = Interactive.withSchema({\n  Component: NotYetInner,\n  componentName: \"<NotYet>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 173,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 173
}