Skip to content

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

7.9 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": {
      "width": 760,
      "height": 110
    },
    "displayName": "Word Push",
    "initialProps": null,
    "slug": "word-push",
    "sourceCode": "import { Easing, Interactive, Sequence, interpolate, useCurrentFrame, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\nimport { forwardRef, useImperativeHandle, useRef, type ComponentProps } from \"react\";\n\ninterface WordPushProps {\n  text: string;\n  fontSize?: number;\n  color?: string;\n  fontWeight?: number;\n  wordGap?: number;\n  accel?: number;\n  speed?: number;\n  className?: string;\n}\n\nconst REVEAL_FRAMES = 2;\nconst SLIDE_FRAMES = 12;\nconst OVERSHOOT_EM = 0.5;\nconst CHAR_WIDTH_EM = 0.5;\nconst SPACE_WIDTH_EM = 0.22;\n\nfunction revealFrame(index: number, wordGap: number, accel: number): number {\n  if (accel === 1) return index * wordGap;\n  return (wordGap * (1 - accel ** index)) / (1 - accel);\n}\n\nfunction wordPushLength(\n  text: string,\n  wordGap = 10,\n  accel = 0.8,\n): number {\n  const words = text.trim().split(/\\s+/).filter(Boolean);\n  if (words.length === 0) return 0;\n  return (\n    Math.ceil(revealFrame(words.length - 1, wordGap, accel)) + SLIDE_FRAMES\n  );\n}\n\nconst pushEasing = Easing.out(Easing.cubic);\nconst driveEasing = Easing.bezier(0.15, 0.7, 0.25, 1);\n\nfunction WordPushBase({\n  text,\n  fontSize = 72,\n  color = \"#171717\",\n  fontWeight = 400,\n  wordGap = 10,\n  accel = 0.8,\n  speed = 1,\n  className,\n}: WordPushProps) {\n  const frame = useCurrentFrame() * speed;\n\n  const words = text.trim().split(/\\s+/).filter(Boolean);\n\n  let lineX = interpolate(\n    frame,\n    [0, SLIDE_FRAMES],\n    [-OVERSHOOT_EM * fontSize, 0],\n    {\n      extrapolateLeft: \"clamp\",\n      extrapolateRight: \"clamp\",\n      easing: driveEasing,\n    },\n  );\n  for (let j = 1; j < words.length; j++) {\n    const wordWidth =\n      (words[j].length * CHAR_WIDTH_EM + SPACE_WIDTH_EM) * fontSize;\n    const revealAt = revealFrame(j, wordGap, accel);\n    const pushed = interpolate(\n      frame,\n      [revealAt + 1, revealAt + 1 + SLIDE_FRAMES],\n      [0, 1],\n      {\n        extrapolateLeft: \"clamp\",\n        extrapolateRight: \"clamp\",\n        easing: pushEasing,\n      },\n    );\n    lineX += (wordWidth / 2) * (1 - pushed);\n  }\n\n  return (\n    <div\n      style={{\n        position: \"absolute\",\n        inset: 0,\n        display: \"flex\",\n        alignItems: \"center\",\n        justifyContent: \"center\",\n        whiteSpace: \"nowrap\",\n        translate: `${lineX}px 0`,\n        background: \"transparent\",\n      }}\n    >\n      <span\n        className={className}\n        style={{\n          fontSize,\n          fontWeight,\n          color,\n          letterSpacing: \"-0.02em\",\n          fontFamily:\n            \"var(--font-geist-sans, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif)\",\n        }}\n      >\n        {words.map((word, j) => {\n          const revealAt = revealFrame(j, wordGap, accel);\n\n          const opacity = interpolate(\n            frame,\n            [revealAt, revealAt + REVEAL_FRAMES],\n            [0, 1],\n            {\n              extrapolateLeft: \"clamp\",\n              extrapolateRight: \"clamp\",\n            },\n          );\n\n          const driven =\n            j === 0\n              ? 1\n              : interpolate(\n                  frame,\n                  [revealAt, revealAt + SLIDE_FRAMES],\n                  [0, 1],\n                  {\n                    extrapolateLeft: \"clamp\",\n                    extrapolateRight: \"clamp\",\n                    easing: driveEasing,\n                  },\n                );\n\n          return (\n            <span\n              key={j}\n              style={{\n                display: \"inline-block\",\n                whiteSpace: \"pre\",\n                marginRight: j < words.length - 1 ? \"0.22em\" : undefined,\n                opacity,\n                translate: `${OVERSHOOT_EM * fontSize * (1 - driven)}px 0`,\n              }}\n            >\n              {word}\n            </span>\n          );\n        })}\n      </span>\n    </div>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  text: {\n    type: \"text-content\",\n    default: \"move at your own pace\",\n    description: \"Text\",\n  },\n  fontSize: {\n    type: \"number\",\n    min: 12,\n    max: 160,\n    step: 1,\n    default: 72,\n    description: \"Font size\",\n    hiddenFromList: false,\n  },\n  wordGap: {\n    type: \"number\",\n    min: 3,\n    max: 30,\n    step: 1,\n    default: 10,\n    description: \"Frames / word\",\n    hiddenFromList: false,\n  },\n  accel: {\n    type: \"number\",\n    min: 0.5,\n    max: 1,\n    step: 0.05,\n    default: 0.8,\n    description: \"Gap shrink / word\",\n    hiddenFromList: false,\n  },\n  color: {\n    type: \"color\",\n    default: \"#171717\",\n    description: \"Color\",\n  },\n  fontWeight: {\n    type: \"enum\",\n    default: \"400\",\n    variants: {\n      \"400\": {},\n      \"500\": {},\n      \"600\": {},\n      \"700\": {},\n    },\n    description: \"Font weight\",\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  width: {\n    type: \"number\",\n    default: 760,\n    min: 10,\n    step: 10,\n    description: \"Width\",\n  },\n  height: {\n    type: \"number\",\n    default: 110,\n    min: 10,\n    step: 10,\n    description: \"Height\",\n  },\n  ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"text\",\"fontSize\",\"wordGap\",\"accel\",\"color\",\"fontWeight\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  text: \"move at your own pace\",\n  fontSize: 72,\n  wordGap: 10,\n  accel: 0.8,\n  color: \"#171717\",\n  fontWeight: \"400\",\n  speed: 1,\n};\n\ntype WordPushElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof WordPushBase>;\n\nconst WordPushInner = forwardRef<\n  HTMLDivElement,\n  WordPushElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, width = 760, height = 110, ...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 ?? \"<WordPush>\"}\n      outlineRef={outlineRef}\n    >\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n        }}\n      >\n        <div\n          ref={outlineRef}\n          style={{\n            position: \"relative\",\n            width,\n            height,\n            ...style,\n          }}\n        >\n          <WordPushBase\n            {...(componentProps as unknown as ComponentProps<typeof WordPushBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const WordPush = Interactive.withSchema({\n  Component: WordPushInner,\n  componentName: \"<WordPush>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 42,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 42
}