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": "Long Shadow",
    "initialProps": null,
    "slug": "long-shadow",
    "sourceCode": "import { forwardRef, useId, useImperativeHandle, useRef, type ComponentProps, type ReactNode } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, interpolate, spring, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ntype LongShadowProps = {\n  /** Flat icon or mark that casts the shadow. Defaults to a red check badge. */\n  children?: ReactNode;\n  /** Shadow direction in degrees at the start (0 = right, 90 = down). */\n  angleFrom?: number;\n  /** Shadow direction after the light swings. */\n  angleTo?: number;\n  shadowColor?: string;\n  background?: string;\n  /** Fade the shadow toward its far end, 0 to 1. */\n  fade?: number;\n  /** Colors of the default badge. */\n  iconColor?: string;\n  checkColor?: string;\n  speed?: number;\n};\n\nconst longShadowDefaults = {\n  angleFrom: 45,\n  angleTo: 135,\n  shadowColor: \"#ECEAE6\",\n  background: \"#FFFFFF\",\n  fade: 0,\n  iconColor: \"#E5322D\",\n  checkColor: \"#FFFFFF\",\n  speed: 1,\n};\n\nconst longShadowLength = 48;\n\n// Log-doubling extrusion: each pass unions the silhouette with itself shifted by\n// twice the previous distance, so 10 passes stack 1024 copies.\nconst PASSES = 10;\nconst COPIES = 2 ** PASSES - 1;\n\nconst clamp = {\n  extrapolateLeft: \"clamp\" as const,\n  extrapolateRight: \"clamp\" as const,\n};\n\nfunction Badge({\n  size,\n  iconColor,\n  checkColor,\n}: {\n  size: number;\n  iconColor: string;\n  checkColor: string;\n}) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 100 100\" aria-hidden>\n      <circle cx={50} cy={50} r={49} fill={iconColor} />\n      <path\n        d=\"M 29 52 L 44 66 L 72 36\"\n        fill=\"none\"\n        stroke={checkColor}\n        strokeWidth={11}\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      />\n    </svg>\n  );\n}\n\nfunction LongShadowBase({\n  children,\n  angleFrom = longShadowDefaults.angleFrom,\n  angleTo = longShadowDefaults.angleTo,\n  shadowColor = longShadowDefaults.shadowColor,\n  background = longShadowDefaults.background,\n  fade = longShadowDefaults.fade,\n  iconColor = longShadowDefaults.iconColor,\n  checkColor = longShadowDefaults.checkColor,\n  speed = longShadowDefaults.speed,\n}: LongShadowProps) {\n  const frame = useCurrentFrame();\n  const { width, height, fps } = useVideoConfig();\n  const id = useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const t = (frame / fps) * 30 * speed;\n  const unit = Math.min(width, height) / 720;\n\n  const pop = spring({\n    frame: t,\n    fps: 30,\n    config: { damping: 13, stiffness: 170, mass: 0.8 },\n  });\n  const grow = interpolate(t, [7, 22], [0, 1], {\n    ...clamp,\n    easing: Easing.out(Easing.cubic),\n  });\n  const swing = spring({\n    frame: t - 24,\n    fps: 30,\n    config: { damping: 11, stiffness: 70, mass: 0.9 },\n  });\n  const angle = angleFrom + (angleTo - angleFrom) * swing;\n  const rad = (angle * Math.PI) / 180;\n  const length = Math.hypot(width, height) * grow;\n  const step = length / COPIES;\n  const dx = Math.cos(rad) * step;\n  const dy = Math.sin(rad) * step;\n\n  const content = children ?? (\n    <Badge\n      size={Math.min(width, height) * 0.34}\n      iconColor={iconColor}\n      checkColor={checkColor}\n    />\n  );\n  const mask =\n    fade > 0\n      ? `linear-gradient(${angle + 90}deg, #000 50%, rgba(0,0,0,${1 - Math.min(1, fade)}) 100%)`\n      : undefined;\n\n  return (\n    <AbsoluteFill style={{ backgroundColor: background, overflow: \"hidden\" }}>\n      <svg width={0} height={0} style={{ position: \"absolute\" }} aria-hidden>\n        <filter\n          id={`${id}-extrude`}\n          x=\"0\"\n          y=\"0\"\n          width=\"1\"\n          height=\"1\"\n          colorInterpolationFilters=\"sRGB\"\n        >\n          <feColorMatrix\n            in=\"SourceAlpha\"\n            type=\"matrix\"\n            values=\"0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 3 0\"\n            result=\"m0\"\n          />\n          {Array.from({ length: PASSES }, (_, i) => (\n            <feOffset\n              key={`o${i}`}\n              in={`m${i}`}\n              dx={dx * 2 ** i}\n              dy={dy * 2 ** i}\n              result={`o${i}`}\n            />\n          )).flatMap((offset, i) => [\n            offset,\n            <feMerge key={`m${i}`} result={`m${i + 1}`}>\n              <feMergeNode in={`m${i}`} />\n              <feMergeNode in={`o${i}`} />\n            </feMerge>,\n          ])}\n          {/* feOffset snaps to whole pixels; a hair of blur hides the steps. */}\n          <feGaussianBlur\n            in={`m${PASSES}`}\n            stdDeviation={0.8 * unit}\n            result=\"soft\"\n          />\n          <feFlood floodColor={shadowColor} />\n          <feComposite in2=\"soft\" operator=\"in\" />\n        </filter>\n      </svg>\n      <AbsoluteFill\n        style={{\n          transform: `scale(${pop})`,\n          alignItems: \"center\",\n          justifyContent: \"center\",\n        }}\n      >\n        <AbsoluteFill\n          style={{\n            alignItems: \"center\",\n            justifyContent: \"center\",\n            filter: `url(#${id}-extrude)`,\n            opacity: grow > 0 ? 1 : 0,\n            maskImage: mask,\n            WebkitMaskImage: mask,\n          }}\n        >\n          {content}\n        </AbsoluteFill>\n        <AbsoluteFill\n          style={{ alignItems: \"center\", justifyContent: \"center\" }}\n        >\n          {content}\n        </AbsoluteFill>\n      </AbsoluteFill>\n    </AbsoluteFill>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  angleFrom: {\n    type: \"number\",\n    default: 45,\n    min: 0,\n    max: 360,\n    step: 5,\n    description: \"Start angle\",\n    hiddenFromList: false,\n  },\n  angleTo: {\n    type: \"number\",\n    default: 135,\n    min: 0,\n    max: 360,\n    step: 5,\n    description: \"End angle\",\n    hiddenFromList: false,\n  },\n  fade: {\n    type: \"number\",\n    default: 0,\n    min: 0,\n    max: 1,\n    step: 0.05,\n    description: \"Fade\",\n    hiddenFromList: false,\n  },\n  shadowColor: {\n    type: \"color\",\n    default: \"#ECEAE6\",\n    description: \"Shadow\",\n  },\n  background: {\n    type: \"color\",\n    default: \"#FFFFFF\",\n    description: \"Background\",\n  },\n  iconColor: {\n    type: \"color\",\n    default: \"#E5322D\",\n    description: \"Badge\",\n  },\n  checkColor: {\n    type: \"color\",\n    default: \"#FFFFFF\",\n    description: \"Check\",\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([\"angleFrom\",\"angleTo\",\"fade\",\"shadowColor\",\"background\",\"iconColor\",\"checkColor\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  angleFrom: 45,\n  angleTo: 135,\n  fade: 0,\n  shadowColor: \"#ECEAE6\",\n  background: \"#FFFFFF\",\n  iconColor: \"#E5322D\",\n  checkColor: \"#FFFFFF\",\n  speed: 1,\n};\n\ntype LongShadowElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof LongShadowBase>;\n\nconst LongShadowInner = forwardRef<\n  HTMLDivElement,\n  LongShadowElementProps & { 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 ?? \"<LongShadow>\"}\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        <LongShadowBase\n          {...(componentProps as unknown as ComponentProps<typeof LongShadowBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const LongShadow = Interactive.withSchema({\n  Component: LongShadowInner,\n  componentName: \"<LongShadow>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 48,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 48
}