Skip to content

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

11.6 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": null,
    "displayName": "Kinetic Warp",
    "initialProps": null,
    "slug": "kinetic-warp",
    "sourceCode": "import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState, type ComponentProps } from \"react\";\nimport { Easing, Interactive, Sequence, continueRender, delayRender, interpolate, useCurrentFrame, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ninterface KineticWarpProps {\n  className?: string;\n  compWidth?: number;\n  compHeight?: number;\n  textColor?: string;\n  text?: string;\n  fontFamily?: string;\n  fontUrl?: string;\n  fontWeight?: number;\n  fontSize?: number;\n  leading?: number;\n  tracking?: number;\n  layerScale?: number;\n  positionX?: number;\n  positionY?: number;\n  meshRows?: number;\n  meshColumns?: number;\n  colRestX?: number;\n  colRightX?: number;\n  colLeftX?: number;\n  rowRestY?: number;\n  rowDownY?: number;\n  rowUpY?: number;\n  keyframeStride?: number;\n  easingBezier?: [number, number, number, number];\n  resampleStep?: number;\n}\n\nfunction monotoneSpline(xs: number[], ys: number[]): (x: number) => number {\n  const n = xs.length;\n  const dx: number[] = [];\n  const secant: number[] = [];\n  for (let i = 0; i < n - 1; i++) {\n    dx.push(xs[i + 1] - xs[i]);\n    secant.push((ys[i + 1] - ys[i]) / (xs[i + 1] - xs[i]));\n  }\n\n  const m: number[] = new Array(n);\n  m[0] = secant[0];\n  m[n - 1] = secant[n - 2];\n  for (let i = 1; i < n - 1; i++) {\n    m[i] = secant[i - 1] * secant[i] <= 0 ? 0 : (secant[i - 1] + secant[i]) / 2;\n  }\n\n  for (let i = 0; i < n - 1; i++) {\n    if (secant[i] === 0) {\n      m[i] = 0;\n      m[i + 1] = 0;\n      continue;\n    }\n    const a = m[i] / secant[i];\n    const b = m[i + 1] / secant[i];\n    const s = a * a + b * b;\n    if (s > 9) {\n      const t = 3 / Math.sqrt(s);\n      m[i] = t * a * secant[i];\n      m[i + 1] = t * b * secant[i];\n    }\n  }\n\n  return (x: number) => {\n    if (x <= xs[0]) return ys[0] + m[0] * (x - xs[0]);\n    if (x >= xs[n - 1]) return ys[n - 1] + m[n - 1] * (x - xs[n - 1]);\n    let i = n - 2;\n    while (i > 0 && x < xs[i]) i--;\n    const h = dx[i];\n    const t = (x - xs[i]) / h;\n    const t2 = t * t;\n    const t3 = t2 * t;\n    return (\n      (2 * t3 - 3 * t2 + 1) * ys[i] +\n      (t3 - 2 * t2 + t) * h * m[i] +\n      (-2 * t3 + 3 * t2) * ys[i + 1] +\n      (t3 - t2) * h * m[i + 1]\n    );\n  };\n}\n\nfunction KineticWarpBase({\n  className,\n  compWidth = 1920,\n  compHeight = 1080,\n  textColor = \"#FFFFFF\",\n  text = \"REM\\nOCN\",\n  fontFamily = \"sans-serif\",\n  fontUrl = \"\",\n  fontWeight = 700,\n  fontSize = 107.1,\n  leading = 70,\n  tracking = -21,\n  layerScale = 480,\n  positionX = 960,\n  positionY = 540,\n  meshRows = 2,\n  meshColumns = 4,\n  colRestX = 960,\n  colRightX = 1255,\n  colLeftX = 678,\n  rowRestY = 540,\n  rowDownY = 850,\n  rowUpY = 387,\n  keyframeStride = 20,\n  easingBezier = [0.3, 0.74, 0.09, 1],\n  resampleStep = 4,\n}: KineticWarpProps) {\n  const frame = useCurrentFrame();\n  const canvasRef = useRef<HTMLCanvasElement | null>(null);\n  const [fontReady, setFontReady] = useState(false);\n\n  const scale = layerScale / 100;\n  const pxSize = fontSize * scale;\n  const pxLeading = leading * scale;\n  const pxTracking = (tracking / 1000) * pxSize;\n\n  useEffect(() => {\n    const handle = delayRender(\"kinetic-warp: loading font\");\n    let cancelled = false;\n    const done = () => {\n      if (!cancelled) setFontReady(true);\n      continueRender(handle);\n    };\n    if (typeof document === \"undefined\" || !document.fonts) {\n      done();\n      return;\n    }\n    document.fonts\n      .load(`${fontWeight} ${pxSize}px \"${fontFamily}\"`)\n      .then(done, done);\n    return () => {\n      cancelled = true;\n    };\n  }, [fontFamily, fontWeight, pxSize]);\n\n  const source = useMemo(() => {\n    if (!fontReady || typeof document === \"undefined\") return null;\n    const canvas = document.createElement(\"canvas\");\n    canvas.width = compWidth;\n    canvas.height = compHeight;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return null;\n\n    ctx.font = `${fontWeight} ${pxSize}px \"${fontFamily}\", sans-serif`;\n    ctx.letterSpacing = `${pxTracking}px`;\n    ctx.fillStyle = textColor;\n    ctx.textAlign = \"left\";\n    ctx.textBaseline = \"alphabetic\";\n\n    const lines = text.split(\"\\n\");\n    const metrics = lines.map((line) => ctx.measureText(line));\n\n    const ascent = metrics[0].actualBoundingBoxAscent;\n    const descent = metrics[metrics.length - 1].actualBoundingBoxDescent;\n    const span = (lines.length - 1) * pxLeading;\n    const firstBaseline = positionY - (span - ascent + descent) / 2;\n\n    lines.forEach((line, i) => {\n      const m = metrics[i];\n      const x =\n        positionX - (m.actualBoundingBoxRight - m.actualBoundingBoxLeft) / 2;\n      ctx.fillText(line, x, firstBaseline + i * pxLeading);\n    });\n\n    return canvas;\n  }, [\n    compWidth,\n    compHeight,\n    text,\n    fontFamily,\n    fontWeight,\n    pxSize,\n    pxLeading,\n    pxTracking,\n    textColor,\n    positionX,\n    positionY,\n    fontReady,\n  ]);\n\n  const scratch = useMemo(() => {\n    if (typeof document === \"undefined\") return null;\n    const canvas = document.createElement(\"canvas\");\n    canvas.width = compWidth;\n    canvas.height = compHeight;\n    return canvas;\n  }, [compWidth, compHeight]);\n\n  const easing = Easing.bezier(...easingBezier);\n  const stops = [0, 1, 2, 3, 4, 5].map((i) => i * keyframeStride);\n  const clamp = {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing,\n  } as const;\n\n  const colX = interpolate(\n    frame,\n    stops,\n    [colRestX, colRightX, colRightX, colLeftX, colLeftX, colRestX],\n    clamp,\n  );\n  const rowY = interpolate(\n    frame,\n    stops,\n    [rowRestY, rowRestY, rowDownY, rowDownY, rowUpY, rowRestY],\n    clamp,\n  );\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas || !source || !scratch) return;\n    const out = canvas.getContext(\"2d\");\n    const mid = scratch.getContext(\"2d\");\n    if (!out || !mid) return;\n\n    const xs = Array.from(\n      { length: meshColumns + 1 },\n      (_, i) => (i * compWidth) / meshColumns,\n    );\n    const ys = Array.from(\n      { length: meshRows + 1 },\n      (_, i) => (i * compHeight) / meshRows,\n    );\n    const xd = xs.slice();\n    const yd = ys.slice();\n    xd[Math.round(meshColumns / 2)] = colX;\n    yd[Math.round(meshRows / 2)] = rowY;\n\n    const gx = monotoneSpline(xd, xs);\n    const gy = monotoneSpline(yd, ys);\n\n    mid.clearRect(0, 0, compWidth, compHeight);\n    out.clearRect(0, 0, compWidth, compHeight);\n\n    for (let d0 = 0; d0 < compWidth; d0 += resampleStep) {\n      const d1 = Math.min(d0 + resampleStep, compWidth);\n      const s0 = gx(d0);\n      const sw = gx(d1) - s0;\n      if (sw <= 0) continue;\n      mid.drawImage(source, s0, 0, sw, compHeight, d0, 0, d1 - d0, compHeight);\n    }\n\n    for (let d0 = 0; d0 < compHeight; d0 += resampleStep) {\n      const d1 = Math.min(d0 + resampleStep, compHeight);\n      const s0 = gy(d0);\n      const sh = gy(d1) - s0;\n      if (sh <= 0) continue;\n      out.drawImage(scratch, 0, s0, compWidth, sh, 0, d0, compWidth, d1 - d0);\n    }\n  }, [\n    source,\n    scratch,\n    colX,\n    rowY,\n    compWidth,\n    compHeight,\n    meshRows,\n    meshColumns,\n    resampleStep,\n  ]);\n\n  return (\n    <div\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\" }}\n    >\n      {fontUrl ? <link rel=\"stylesheet\" href={fontUrl} /> : null}\n      <canvas\n        ref={canvasRef}\n        width={compWidth}\n        height={compHeight}\n        style={{ width: \"100%\", height: \"100%\", display: \"block\" }}\n      />\n    </div>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  text: {\n    type: \"text-content\",\n    default: \"REM\\nOCN\",\n    description: \"Text\",\n  },\n  textColor: {\n    type: \"color\",\n    default: \"#FFFFFF\",\n    description: \"Color\",\n  },\n  fontFamily: {\n    type: \"text-content\",\n    default: \"Passion One\",\n    description: \"Font family\",\n  },\n  fontUrl: {\n    type: \"text-content\",\n    default: \"https://fonts.googleapis.com/css2?family=Passion+One:wght@400;700;900&display=block\",\n    description: \"Font URL\",\n  },\n  fontWeight: {\n    type: \"enum\",\n    default: \"700\",\n    variants: {\n      \"400\": {},\n      \"500\": {},\n      \"600\": {},\n      \"700\": {},\n    },\n    description: \"Font weight\",\n  },\n  fontSize: {\n    type: \"number\",\n    min: 40,\n    max: 200,\n    step: 0.1,\n    default: 107.1,\n    description: \"Font size\",\n    hiddenFromList: false,\n  },\n  layerScale: {\n    type: \"number\",\n    min: 100,\n    max: 800,\n    step: 10,\n    default: 480,\n    description: \"Layer scale (%)\",\n    hiddenFromList: false,\n  },\n  keyframeStride: {\n    type: \"number\",\n    min: 5,\n    max: 40,\n    step: 1,\n    default: 20,\n    description: \"Keyframe stride\",\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([\"text\",\"textColor\",\"fontFamily\",\"fontUrl\",\"fontWeight\",\"fontSize\",\"layerScale\",\"keyframeStride\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  text: \"REM\\nOCN\",\n  textColor: \"#FFFFFF\",\n  fontFamily: \"Passion One\",\n  fontUrl: \"https://fonts.googleapis.com/css2?family=Passion+One:wght@400;700;900&display=block\",\n  fontWeight: \"700\",\n  fontSize: 107.1,\n  layerScale: 480,\n  keyframeStride: 20,\n  speed: 1,\n};\n\ntype KineticWarpElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof KineticWarpBase>;\n\nconst KineticWarpInner = forwardRef<\n  HTMLDivElement,\n  KineticWarpElementProps & { 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 ?? \"<KineticWarp>\"}\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        <KineticWarpBase\n          {...(componentProps as unknown as ComponentProps<typeof KineticWarpBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const KineticWarp = Interactive.withSchema({\n  Component: KineticWarpInner,\n  componentName: \"<KineticWarp>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 100,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 100
}