Skip to content

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

10.2 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": null,
    "displayName": "Gooey Morph",
    "initialProps": null,
    "slug": "gooey-morph",
    "sourceCode": "import { forwardRef, useId, useImperativeHandle, useRef, type ComponentProps } from \"react\";\nimport { Easing, Interactive, Sequence, interpolate, useCurrentFrame, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ninterface GooeyMorphProps {\n  className?: string;\n  width?: number;\n  height?: number;\n  barWidth?: number;\n  barHeight?: number;\n  barStartPositions?: [number, number][];\n  barRestPositions?: [number, number][];\n  barEntryFrames?: number[];\n  barTravelFrames?: number;\n  word?: string;\n  fontFamily?: string;\n  fontSize?: number;\n  tracking?: number;\n  wordCenterX?: number;\n  wordBaselineY?: number;\n  blurRadius?: number;\n  blurIterations?: number;\n  alphaInputBlack?: number;\n  alphaInputWhite?: number;\n  morphStartFrame?: number;\n  morphPeakFrame?: number;\n  morphEndFrame?: number;\n  displaceAmount?: number;\n  displaceSize?: number;\n  displaceComplexity?: number;\n  displaceEvolutionPerSecond?: number;\n  fill?: string;\n}\n\nfunction boxBlurSigma(radius: number, iterations: number): number {\n  const w = 2 * radius + 1;\n  return Math.sqrt((iterations * (w * w - 1)) / 12);\n}\n\nfunction GooeyMorphBase({\n  className,\n  width = 1920,\n  height = 1080,\n  barWidth = 213,\n  barHeight = 181,\n  barStartPositions = [\n    [956.9, 764.9],\n    [297.5, 799.9],\n    [738.2, 257.9],\n    [1617.9, 302.9],\n  ],\n  barRestPositions = [\n    [634.9, 538.9],\n    [852.5, 538.9],\n    [1070.2, 538.9],\n    [1287.9, 538.9],\n  ],\n  barEntryFrames = [8, 0, 4, 13],\n  barTravelFrames = 30,\n  word = \"HELLO\",\n  fontFamily = \"sans-serif\",\n  fontSize = 237,\n  tracking = 0,\n  wordCenterX = 961.4,\n  wordBaselineY = 601.3,\n  blurRadius = 6,\n  blurIterations = 6,\n  alphaInputBlack = 0.4588,\n  alphaInputWhite = 0.5098,\n  morphStartFrame = 50,\n  morphPeakFrame = 63,\n  morphEndFrame = 76,\n  displaceAmount = 20,\n  displaceSize = 19,\n  displaceComplexity = 1,\n  displaceEvolutionPerSecond = 50,\n  fill = \"#ffffff\",\n}: GooeyMorphProps) {\n  const frame = useCurrentFrame();\n  const filterId = useId();\n\n  const travelEase = Easing.bezier(0.88, 0.14, 0.12, 0.86);\n\n  const morphWindow: [number, number, number] = [\n    morphStartFrame,\n    morphPeakFrame,\n    morphEndFrame,\n  ];\n\n  const blur = interpolate(frame, morphWindow, [0, blurRadius, 0], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing: Easing.bezier(0.33, 0, 0.67, 1),\n  });\n  const sigma = boxBlurSigma(blur, blurIterations);\n\n  const displaceScale = interpolate(\n    frame,\n    morphWindow,\n    [0, displaceAmount, 0],\n    {\n      extrapolateLeft: \"clamp\",\n      extrapolateRight: \"clamp\",\n      easing: Easing.bezier(0.33, 0, 0.67, 1),\n    },\n  );\n\n  const barsOpacity = interpolate(\n    frame,\n    [morphStartFrame, morphEndFrame],\n    [1, 0],\n    {\n      extrapolateLeft: \"clamp\",\n      extrapolateRight: \"clamp\",\n    },\n  );\n  const wordOpacity = interpolate(\n    frame,\n    [morphStartFrame, morphEndFrame],\n    [0, 1],\n    {\n      extrapolateLeft: \"clamp\",\n      extrapolateRight: \"clamp\",\n    },\n  );\n\n  const slope = 1 / Math.max(alphaInputWhite - alphaInputBlack, 1e-6);\n  const offset = -alphaInputBlack * slope;\n\n  const evolution = (frame / 30) * displaceEvolutionPerSecond;\n\n  return (\n    <div\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\" }}\n    >\n      <svg\n        width=\"100%\"\n        height=\"100%\"\n        viewBox={`0 0 ${width} ${height}`}\n        preserveAspectRatio=\"xMidYMid meet\"\n        style={{ display: \"block\" }}\n      >\n        <defs>\n          <filter\n            id={filterId}\n            x=\"-15%\"\n            y=\"-15%\"\n            width=\"130%\"\n            height=\"130%\"\n            colorInterpolationFilters=\"sRGB\"\n          >\n            <feGaussianBlur\n              in=\"SourceGraphic\"\n              stdDeviation={sigma}\n              result=\"blurred\"\n            />\n            <feColorMatrix\n              in=\"blurred\"\n              type=\"matrix\"\n              values={[\n                \"1 0 0 0 0\",\n                \"0 1 0 0 0\",\n                \"0 0 1 0 0\",\n                `0 0 0 ${slope} ${offset}`,\n              ].join(\" \")}\n              result=\"threshold\"\n            />\n            <feTurbulence\n              type=\"fractalNoise\"\n              baseFrequency={1 / Math.max(displaceSize, 1)}\n              numOctaves={Math.max(1, Math.round(displaceComplexity))}\n              seed={7}\n              result=\"noise\"\n            />\n            <feOffset\n              in=\"noise\"\n              dx={evolution}\n              dy={evolution * 0.6}\n              result=\"noiseMoved\"\n            />\n            <feDisplacementMap\n              in=\"threshold\"\n              in2=\"noiseMoved\"\n              scale={displaceScale}\n              xChannelSelector=\"R\"\n              yChannelSelector=\"G\"\n            />\n          </filter>\n        </defs>\n\n        <g filter={`url(#${filterId})`}>\n          <g opacity={barsOpacity}>\n            {barRestPositions.map((rest, i) => {\n              const start = barStartPositions[i] ?? rest;\n              const entry = barEntryFrames[i] ?? 0;\n              const local = frame - entry;\n              const stops = [0, barTravelFrames / 2, barTravelFrames];\n\n              const x = interpolate(\n                local,\n                stops,\n                [start[0], rest[0], rest[0]],\n                {\n                  extrapolateLeft: \"clamp\",\n                  extrapolateRight: \"clamp\",\n                  easing: travelEase,\n                },\n              );\n              const y = interpolate(\n                local,\n                stops,\n                [start[1], start[1], rest[1]],\n                {\n                  extrapolateLeft: \"clamp\",\n                  extrapolateRight: \"clamp\",\n                  easing: travelEase,\n                },\n              );\n\n              return (\n                <rect\n                  key={rest.join(\",\")}\n                  x={x - barWidth / 2}\n                  y={y - barHeight / 2}\n                  width={barWidth}\n                  height={barHeight}\n                  fill={fill}\n                />\n              );\n            })}\n          </g>\n\n          <text\n            x={wordCenterX}\n            y={wordBaselineY}\n            textAnchor=\"middle\"\n            fontFamily={fontFamily}\n            fontSize={fontSize}\n            fontWeight={900}\n            letterSpacing={tracking}\n            fill={fill}\n            opacity={wordOpacity}\n          >\n            {word}\n          </text>\n        </g>\n      </svg>\n    </div>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  word: {\n    type: \"text-content\",\n    default: \"HELLO\",\n    description: \"Word\",\n  },\n  fill: {\n    type: \"color\",\n    default: \"#f2f2f2\",\n    description: \"Fill\",\n  },\n  fontFamily: {\n    type: \"text-content\",\n    default: \"'Bodoni Moda', Georgia, serif\",\n    description: \"Font family\",\n  },\n  blurRadius: {\n    type: \"number\",\n    min: 0,\n    max: 20,\n    step: 1,\n    default: 6,\n    description: \"Goo blur radius\",\n    hiddenFromList: false,\n  },\n  displaceAmount: {\n    type: \"number\",\n    min: 0,\n    max: 80,\n    step: 1,\n    default: 20,\n    description: \"Displace amount\",\n    hiddenFromList: false,\n  },\n  barTravelFrames: {\n    type: \"number\",\n    min: 10,\n    max: 60,\n    step: 1,\n    default: 30,\n    description: \"Bar travel (frames)\",\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([\"word\",\"fill\",\"fontFamily\",\"blurRadius\",\"displaceAmount\",\"barTravelFrames\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  word: \"HELLO\",\n  fill: \"#f2f2f2\",\n  fontFamily: \"'Bodoni Moda', Georgia, serif\",\n  blurRadius: 6,\n  displaceAmount: 20,\n  barTravelFrames: 30,\n  speed: 1,\n};\n\ntype GooeyMorphElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof GooeyMorphBase>;\n\nconst GooeyMorphInner = forwardRef<\n  HTMLDivElement,\n  GooeyMorphElementProps & { 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 ?? \"<GooeyMorph>\"}\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        <GooeyMorphBase\n          {...(componentProps as unknown as ComponentProps<typeof GooeyMorphBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const GooeyMorph = Interactive.withSchema({\n  Component: GooeyMorphInner,\n  componentName: \"<GooeyMorph>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 76,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 76
}