Skip to content

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

10.3 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/paths",
        "version": null
      }
    ],
    "dimensions": {
      "width": 48,
      "height": 48
    },
    "displayName": "Clock",
    "initialProps": null,
    "slug": "icon-clock",
    "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 FACE_PATH = \"M12 2a10 10 0 1 0 0 20 10 10 0 1 0 0-20Z\";\nconst HANDS_PATH = \"M12 6v6l4 2\";\nconst MINUTE_PATH = \"M12 6V12\";\nconst HOUR_PATH = \"M12 12 16 14\";\n\nconst CENTER = 12;\nconst STEPS = 4;\n\nconst TIMINGS = {\n  drawDurationInFrames: 16,\n  actionDelayInFrames: 2,\n  actionDurationInFrames: 40,\n  loop: true,\n} as const;\n\nfunction ClockIconBase({\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 linearDraw = 1 - (1 - drawProgress) ** (1 / 3);\n  const acting = animation !== \"draw\" && actionFrame >= 0;\n\n  const faceDraw = interpolate(linearDraw, [0, 0.7], [0, 1], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing: Easing.out(Easing.quad),\n  });\n  const handsDraw = interpolate(linearDraw, [0.5, 1], [0, 1], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing: Easing.out(Easing.quad),\n  });\n\n  const scaled = actionProgress * STEPS;\n  const stepIndex = Math.floor(scaled);\n  const frac = scaled - stepIndex;\n  const eased = interpolate(frac, [0, 0.55, 1], [0, 1, 1], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing: Easing.inOut(Easing.quad),\n  });\n  const tick = acting ? ((stepIndex + eased) / STEPS) * 360 : 0;\n\n  const face = drawnPathProps(FACE_PATH, acting ? 1 : faceDraw);\n  const minute = drawnPathProps(MINUTE_PATH, acting ? 1 : handsDraw);\n  const hour = drawnPathProps(HOUR_PATH, acting ? 1 : handsDraw);\n\n  const scale = 0.85 + 0.15 * scaleIn;\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      <path\n        d={FACE_PATH}\n        strokeDasharray={face.strokeDasharray}\n        strokeDashoffset={face.strokeDashoffset}\n      />\n      <path\n        d={HOUR_PATH}\n        strokeDasharray={hour.strokeDasharray}\n        strokeDashoffset={hour.strokeDashoffset}\n      />\n      <g transform={`rotate(${tick} ${CENTER} ${CENTER})`}>\n        <path\n          d={MINUTE_PATH}\n          strokeDasharray={minute.strokeDasharray}\n          strokeDashoffset={minute.strokeDashoffset}\n        />\n      </g>\n    </svg>\n  );\n}\n\nfunction ClockIconStatic({\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=\"10\" />\n      <path d={HANDS_PATH} />\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: true,\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: true,\n  size: 48,\n  color: \"#171717\",\n  strokeWidth: 2,\n  speed: 1,\n};\n\ntype ClockIconElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof ClockIconBase>;\n\nconst ClockIconInner = forwardRef<\n  HTMLDivElement,\n  ClockIconElementProps & { 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 ?? \"<ClockIcon>\"}\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          <ClockIconBase\n            {...(componentProps as unknown as ComponentProps<typeof ClockIconBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const ClockIcon = Interactive.withSchema({\n  Component: ClockIconInner,\n  componentName: \"<ClockIcon>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 90,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 90
}