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": [],
    "dimensions": null,
    "displayName": "Spotlight",
    "initialProps": null,
    "slug": "spotlight",
    "sourceCode": "import React, { forwardRef, useId, useImperativeHandle, useRef, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, interpolate, spring, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ntype SpotlightProps = {\n  /** Lockup the beam lands on. Falls back to a placeholder \"remocn\" lockup. */\n  children?: React.ReactNode;\n  /** Color of the light cone and floor pool. */\n  beamColor?: string;\n  /** Dark stage behind everything. */\n  stageColor?: string;\n  /** Brightness of the unlit lockup, 0 to 1. */\n  dim?: number;\n  speed?: number;\n};\n\nconst spotlightDefaults = {\n  beamColor: \"#FFFFFF\",\n  stageColor: \"#111111\",\n  dim: 0.16,\n  speed: 1,\n};\n\nconst SEARCH = 12;\nconst SWING = 26;\nconst HOUSE = 38;\nconst clamp = {\n  extrapolateLeft: \"clamp\" as const,\n  extrapolateRight: \"clamp\" as const,\n};\n\nconst PLACEHOLDER_FONT =\n  \"-apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, Helvetica, Arial, sans-serif\";\n\n// Neutral lockup shown when no children are passed.\nfunction PlaceholderLockup({\n  mark,\n  dot,\n  text,\n  scale = 1,\n}: {\n  mark: string;\n  dot: string;\n  text: string;\n  scale?: number;\n}) {\n  return (\n    <div style={{ display: \"flex\", alignItems: \"center\", gap: 28 * scale }}>\n      <div\n        style={{\n          width: 120 * scale,\n          height: 120 * scale,\n          borderRadius: 32 * scale,\n          backgroundColor: mark,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n        }}\n      >\n        <div\n          style={{\n            width: 36 * scale,\n            height: 36 * scale,\n            borderRadius: \"50%\",\n            backgroundColor: dot,\n          }}\n        />\n      </div>\n      <span\n        style={{\n          fontFamily: PLACEHOLDER_FONT,\n          fontSize: 104 * scale,\n          fontWeight: 800,\n          letterSpacing: \"-0.04em\",\n          color: text,\n        }}\n      >\n        remocn\n      </span>\n    </div>\n  );\n}\n\nfunction SpotlightBase(props: SpotlightProps) {\n  const { children, beamColor, stageColor, dim, speed } = {\n    ...spotlightDefaults,\n    ...props,\n  };\n  const uid = `spotlight-${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  const frame = useCurrentFrame();\n  const { width, height, fps } = useVideoConfig();\n  const t = frame * Math.max(0, speed);\n\n  // Beam target, as a fraction of the width: search left, overshoot right, land.\n  let target: number;\n  if (t < SEARCH) {\n    target = interpolate(t, [0, SEARCH], [0.14, 0.27], {\n      ...clamp,\n      easing: Easing.inOut(Easing.sin),\n    });\n  } else if (t < SWING) {\n    target = interpolate(t, [SEARCH, SWING], [0.27, 0.74], {\n      ...clamp,\n      easing: Easing.inOut(Easing.cubic),\n    });\n  } else {\n    const land = spring({\n      frame: t - SWING,\n      fps,\n      config: { damping: 11, stiffness: 120, mass: 0.8 },\n    });\n    target = 0.74 + (0.5 - 0.74) * land;\n  }\n\n  const tx = target * width;\n  const apexX = width / 2;\n  const apexY = -height * 0.12;\n  const floorY = height * 0.8;\n  const spread = width * 0.27;\n  const apexHalf = width * 0.012;\n\n  const beamOn = interpolate(t, [0, 5], [0, 1], {\n    ...clamp,\n    easing: Easing.out(Easing.quad),\n  });\n  const house = interpolate(t, [HOUSE, HOUSE + 12], [0, 1], {\n    ...clamp,\n    easing: Easing.inOut(Easing.sin),\n  });\n  const coverage = Math.max(0, 1 - Math.abs(target - 0.5) / 0.3);\n  const lit = Math.max(\n    dim,\n    dim + (1 - dim) * Easing.inOut(Easing.quad)(coverage) * beamOn,\n  );\n  const brightness =\n    t >= SWING + 10 ? Math.max(lit, Math.min(1, lit + house)) : lit;\n  const blur = width * 0.012;\n\n  return (\n    <AbsoluteFill style={{ backgroundColor: stageColor }}>\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(ellipse 80% 70% at 50% 40%, ${beamColor} 0%, transparent 70%)`,\n          opacity: 0.09 * house,\n        }}\n      />\n      <svg\n        width={width}\n        height={height}\n        viewBox={`0 0 ${width} ${height}`}\n        style={{ position: \"absolute\", inset: 0 }}\n      >\n        <defs>\n          <linearGradient\n            id={`${uid}-beam`}\n            gradientUnits=\"userSpaceOnUse\"\n            x1={0}\n            y1={apexY}\n            x2={0}\n            y2={floorY}\n          >\n            <stop offset=\"0\" stopColor={beamColor} stopOpacity={0.55} />\n            <stop offset=\"0.6\" stopColor={beamColor} stopOpacity={0.2} />\n            <stop offset=\"1\" stopColor={beamColor} stopOpacity={0.12} />\n          </linearGradient>\n          <radialGradient id={`${uid}-pool`}>\n            <stop offset=\"0\" stopColor={beamColor} stopOpacity={0.45} />\n            <stop offset=\"0.6\" stopColor={beamColor} stopOpacity={0.14} />\n            <stop offset=\"1\" stopColor={beamColor} stopOpacity={0} />\n          </radialGradient>\n          <filter\n            id={`${uid}-soft`}\n            x={-width}\n            y={-height}\n            width={width * 3}\n            height={height * 3}\n            filterUnits=\"userSpaceOnUse\"\n          >\n            <feGaussianBlur stdDeviation={blur} />\n          </filter>\n        </defs>\n        <g opacity={beamOn * (1 - 0.35 * house)}>\n          <ellipse\n            cx={tx}\n            cy={floorY}\n            rx={spread * 1.25}\n            ry={height * 0.07}\n            fill={`url(#${uid}-pool)`}\n          />\n          <polygon\n            points={`${apexX - apexHalf},${apexY} ${apexX + apexHalf},${apexY} ${tx + spread},${floorY} ${tx - spread},${floorY}`}\n            fill={`url(#${uid}-beam)`}\n            filter={`url(#${uid}-soft)`}\n          />\n        </g>\n      </svg>\n      <AbsoluteFill\n        style={{\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          filter: `brightness(${brightness})`,\n        }}\n      >\n        {children ?? (\n          <PlaceholderLockup\n            mark={beamColor}\n            dot={stageColor}\n            text={beamColor}\n          />\n        )}\n      </AbsoluteFill>\n    </AbsoluteFill>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  dim: {\n    type: \"number\",\n    default: 0.16,\n    min: 0,\n    max: 0.6,\n    step: 0.01,\n    description: \"Unlit brightness\",\n    hiddenFromList: false,\n  },\n  beamColor: {\n    type: \"color\",\n    default: \"#FFFFFF\",\n    description: \"Beam\",\n  },\n  stageColor: {\n    type: \"color\",\n    default: \"#111111\",\n    description: \"Stage\",\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([\"dim\",\"beamColor\",\"stageColor\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  dim: 0.16,\n  beamColor: \"#FFFFFF\",\n  stageColor: \"#111111\",\n  speed: 1,\n};\n\ntype SpotlightElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof SpotlightBase>;\n\nconst SpotlightInner = forwardRef<\n  HTMLDivElement,\n  SpotlightElementProps & { 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 ?? \"<Spotlight>\"}\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        <SpotlightBase\n          {...(componentProps as unknown as ComponentProps<typeof SpotlightBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const Spotlight = Interactive.withSchema({\n  Component: SpotlightInner,\n  componentName: \"<Spotlight>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 50,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 50
}