Skip to content

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

10.4 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/paths",
        "version": null
      }
    ],
    "dimensions": {
      "width": 48,
      "height": 48
    },
    "displayName": "Sun",
    "initialProps": null,
    "slug": "icon-sun",
    "sourceCode": "import { Easing, Interactive, Sequence, interpolate, spring, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\nimport { evolvePath } from \"@remotion/paths\";\nimport { forwardRef, useImperativeHandle, useRef, type ComponentProps } from \"react\";\n\ninterface IconAnimationProps {\n  animation?: \"draw\" | \"action\" | \"both\";\n  loop?: boolean;\n  speed?: number;\n  size?: number;\n  color?: string;\n  strokeWidth?: number;\n  className?: string;\n}\n\ninterface IconTimings {\n  drawDurationInFrames?: number;\n  actionDelayInFrames?: number;\n  actionDurationInFrames?: number;\n  loop?: boolean;\n}\n\ninterface IconTimeline {\n  drawProgress: number;\n  scaleIn: number;\n  actionFrame: number;\n  actionProgress: number;\n  cycleIndex: number;\n}\n\nfunction clamp01(t: number): number {\n  return t < 0 ? 0 : t > 1 ? 1 : t;\n}\n\nfunction easeOut(t: number): number {\n  return 1 - (1 - t) ** 3;\n}\n\nfunction iconTimeline(\n  frame: number,\n  fps: number,\n  props: IconAnimationProps,\n  timings: IconTimings,\n): IconTimeline {\n  const animation = props.animation ?? \"both\";\n  const loop = props.loop ?? timings.loop ?? false;\n  const drawDuration = timings.drawDurationInFrames ?? 14;\n  const actionDelay = timings.actionDelayInFrames ?? 2;\n  const actionDuration = timings.actionDurationInFrames ?? 20;\n\n  const drawProgress =\n    animation === \"action\"\n      ? 1\n      : easeOut(clamp01(drawDuration > 0 ? frame / drawDuration : 1));\n\n  const scaleIn = clamp01(\n    spring({\n      frame,\n      fps,\n      config: { damping: 18, stiffness: 220, mass: 0.7 },\n      durationInFrames: Math.max(1, drawDuration),\n    }),\n  );\n\n  const actionStart = animation === \"action\" ? 0 : drawDuration + actionDelay;\n  const sinceStart = frame - actionStart;\n\n  if (animation === \"draw\" || sinceStart < 0) {\n    return {\n      drawProgress,\n      scaleIn,\n      actionFrame: sinceStart,\n      actionProgress: 0,\n      cycleIndex: 0,\n    };\n  }\n\n  if (actionDuration <= 0) {\n    return {\n      drawProgress,\n      scaleIn,\n      actionFrame: sinceStart,\n      actionProgress: 1,\n      cycleIndex: 0,\n    };\n  }\n\n  if (loop) {\n    const cycleIndex = Math.floor(sinceStart / actionDuration);\n    const local = sinceStart - cycleIndex * actionDuration;\n    return {\n      drawProgress,\n      scaleIn,\n      actionFrame: local,\n      actionProgress: local / actionDuration,\n      cycleIndex,\n    };\n  }\n\n  return {\n    drawProgress,\n    scaleIn,\n    actionFrame: sinceStart,\n    actionProgress: clamp01(sinceStart / actionDuration),\n    cycleIndex: 0,\n  };\n}\n\nfunction useIconAnimation(\n  props: IconAnimationProps,\n  timings: IconTimings,\n): IconTimeline {\n  const frame = useCurrentFrame();\n  const { fps } = useVideoConfig();\n  const speed = props.speed ?? 1;\n  return iconTimeline(frame * speed, fps, props, timings);\n}\n\nfunction staggeredProgress(\n  progress: number,\n  index: number,\n  count: number,\n  overlap = 0.5,\n): number {\n  if (count <= 1) return clamp01(progress);\n  const o = clamp01(overlap);\n  const width = 1 / (count - (count - 1) * o);\n  const start = index * width * (1 - o);\n  return clamp01((progress - start) / width);\n}\n\nfunction drawnPathProps(\n  d: string,\n  progress: number,\n): { strokeDasharray: string; strokeDashoffset: number } {\n  return evolvePath(clamp01(progress), d);\n}\n\nconst CORE_PATH = \"M12 8a4 4 0 1 0 0 8 4 4 0 1 0 0-8Z\";\n\nconst RAYS = [\n  \"M12 4 12 2\",\n  \"M17.66 6.34 19.07 4.93\",\n  \"M20 12 22 12\",\n  \"M17.66 17.66 19.07 19.07\",\n  \"M12 20 12 22\",\n  \"M6.34 17.66 4.93 19.07\",\n  \"M4 12 2 12\",\n  \"M6.34 6.34 4.93 4.93\",\n];\n\nconst CENTER = 12;\n\nconst TIMINGS = {\n  drawDurationInFrames: 14,\n  actionDelayInFrames: 2,\n  actionDurationInFrames: 30,\n  loop: false,\n} as const;\n\nfunction SunIconBase({\n  animation = \"both\",\n  loop,\n  speed,\n  size = 48,\n  color = \"currentColor\",\n  strokeWidth = 2,\n  className,\n}: IconAnimationProps) {\n  const { drawProgress, scaleIn, actionProgress, actionFrame } =\n    useIconAnimation({ animation, loop, speed }, TIMINGS);\n\n  const acting = animation !== \"draw\" && actionFrame >= 0;\n\n  const corePop = acting\n    ? interpolate(actionProgress, [0.55, 0.75, 1], [1, 1.08, 1], {\n        extrapolateLeft: \"clamp\",\n        extrapolateRight: \"clamp\",\n        easing: Easing.inOut(Easing.quad),\n      })\n    : 1;\n\n  const core = drawnPathProps(CORE_PATH, drawProgress);\n\n  const rays = RAYS.map((d, i) => {\n    const start = i * 0.07;\n    const rayFan = interpolate(actionProgress, [start, start + 0.28], [0, 1], {\n      extrapolateLeft: \"clamp\",\n      extrapolateRight: \"clamp\",\n      easing: Easing.out(Easing.quad),\n    });\n    const progress = animation === \"draw\" ? 1 : acting ? rayFan : 0;\n    return { d, ...drawnPathProps(d, progress) };\n  });\n\n  const scale = 0.85 + 0.15 * scaleIn;\n  const coreTransform = `translate(${CENTER * (1 - corePop)} ${CENTER * (1 - corePop)}) scale(${corePop})`;\n\n  return (\n    <svg\n      className={className}\n      width={size}\n      height={size}\n      viewBox=\"0 0 24 24\"\n      fill=\"none\"\n      stroke={color}\n      strokeWidth={strokeWidth}\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n      style={{\n        overflow: \"visible\",\n        transformOrigin: \"center\",\n        transform: `scale(${scale})`,\n      }}\n    >\n      {rays.map((ray) => (\n        <path\n          key={ray.d}\n          d={ray.d}\n          strokeDasharray={ray.strokeDasharray}\n          strokeDashoffset={ray.strokeDashoffset}\n        />\n      ))}\n      <g transform={coreTransform}>\n        <path\n          d={CORE_PATH}\n          strokeDasharray={core.strokeDasharray}\n          strokeDashoffset={core.strokeDashoffset}\n        />\n      </g>\n    </svg>\n  );\n}\n\nfunction SunIconStatic({\n  size = 48,\n  color = \"currentColor\",\n  strokeWidth = 2,\n  className,\n}: {\n  size?: number;\n  color?: string;\n  strokeWidth?: number;\n  className?: string;\n}) {\n  return (\n    <svg\n      className={className}\n      width={size}\n      height={size}\n      viewBox=\"0 0 24 24\"\n      fill=\"none\"\n      stroke={color}\n      strokeWidth={strokeWidth}\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n    >\n      <circle cx=\"12\" cy=\"12\" r=\"4\" />\n      <path d=\"M12 2v2\" />\n      <path d=\"M12 20v2\" />\n      <path d=\"m4.93 4.93 1.41 1.41\" />\n      <path d=\"m17.66 17.66 1.41 1.41\" />\n      <path d=\"M2 12h2\" />\n      <path d=\"M20 12h2\" />\n      <path d=\"m6.34 17.66-1.41 1.41\" />\n      <path d=\"m19.07 4.93-1.41 1.41\" />\n    </svg>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  animation: {\n    type: \"enum\",\n    default: \"both\",\n    variants: {\n      draw: {},\n      action: {},\n      both: {},\n    },\n    description: \"Animation\",\n  },\n  loop: {\n    type: \"boolean\",\n    default: false,\n    description: \"Loop\",\n  },\n  size: {\n    type: \"number\",\n    min: 24,\n    max: 200,\n    step: 4,\n    default: 48,\n    description: \"Size\",\n    hiddenFromList: false,\n  },\n  color: {\n    type: \"color\",\n    default: \"#171717\",\n    description: \"Color\",\n  },\n  strokeWidth: {\n    type: \"number\",\n    min: 1,\n    max: 3,\n    step: 0.25,\n    default: 2,\n    description: \"Stroke width\",\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  width: {\n    type: \"number\",\n    default: 48,\n    min: 10,\n    step: 10,\n    description: \"Width\",\n  },\n  height: {\n    type: \"number\",\n    default: 48,\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([\"animation\",\"loop\",\"size\",\"color\",\"strokeWidth\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  animation: \"both\",\n  loop: false,\n  size: 48,\n  color: \"#171717\",\n  strokeWidth: 2,\n  speed: 1,\n};\n\ntype SunIconElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof SunIconBase>;\n\nconst SunIconInner = forwardRef<\n  HTMLDivElement,\n  SunIconElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, width = 48, height = 48, ...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 ?? \"<SunIcon>\"}\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          <SunIconBase\n            {...(componentProps as unknown as ComponentProps<typeof SunIconBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const SunIcon = Interactive.withSchema({\n  Component: SunIconInner,\n  componentName: \"<SunIcon>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 85,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 85
}