Skip to content

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

9.7 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [
      {
        "name": "@remotion/paths",
        "version": null
      }
    ],
    "dimensions": {
      "width": 48,
      "height": 48
    },
    "displayName": "Code",
    "initialProps": null,
    "slug": "icon-code",
    "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 RIGHT_PATH = \"M16 18 22 12 16 6\";\nconst LEFT_PATH = \"M8 6 2 12 8 18\";\n\nconst TIMINGS = {\n  drawDurationInFrames: 12,\n  actionDelayInFrames: 2,\n  actionDurationInFrames: 22,\n  loop: false,\n} as const;\n\nfunction CodeIconBase({\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 open = acting\n    ? interpolate(actionProgress, [0, 0.4, 1], [0, 2.5, 0], {\n        extrapolateLeft: \"clamp\",\n        extrapolateRight: \"clamp\",\n        easing: Easing.inOut(Easing.quad),\n      })\n    : 0;\n  const pop = acting\n    ? interpolate(actionProgress, [0.4, 0.7, 1], [1, 1.04, 1], {\n        extrapolateLeft: \"clamp\",\n        extrapolateRight: \"clamp\",\n        easing: Easing.inOut(Easing.quad),\n      })\n    : 1;\n\n  const right = drawnPathProps(RIGHT_PATH, drawProgress);\n  const left = drawnPathProps(LEFT_PATH, drawProgress);\n\n  const scale = (0.85 + 0.15 * scaleIn) * pop;\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      <g transform={`translate(${open} 0)`}>\n        <path\n          d={RIGHT_PATH}\n          strokeDasharray={right.strokeDasharray}\n          strokeDashoffset={right.strokeDashoffset}\n        />\n      </g>\n      <g transform={`translate(${-open} 0)`}>\n        <path\n          d={LEFT_PATH}\n          strokeDasharray={left.strokeDasharray}\n          strokeDashoffset={left.strokeDashoffset}\n        />\n      </g>\n    </svg>\n  );\n}\n\nfunction CodeIconStatic({\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      <path d=\"m16 18 6-6-6-6\" />\n      <path d=\"m8 6-6 6 6 6\" />\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 CodeIconElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof CodeIconBase>;\n\nconst CodeIconInner = forwardRef<\n  HTMLDivElement,\n  CodeIconElementProps & { 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 ?? \"<CodeIcon>\"}\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          <CodeIconBase\n            {...(componentProps as unknown as ComponentProps<typeof CodeIconBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const CodeIcon = Interactive.withSchema({\n  Component: CodeIconInner,\n  componentName: \"<CodeIcon>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 70,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 70
}