Skip to content

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

8.9 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": null,
    "displayName": "Parallax Depth",
    "initialProps": null,
    "slug": "parallax-depth",
    "sourceCode": "import React, { forwardRef, useImperativeHandle, useMemo, useRef, type ComponentProps } from \"react\";\nimport { AbsoluteFill, Easing, Interactive, Sequence, interpolate, random, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\n\ntype ParallaxDepthLayer = {\n  /** Full-frame content of the layer. Leave room past the edges for the truck. */\n  content: React.ReactNode;\n  /** Distance from the camera. 1 is nearest; larger values move less. */\n  depth: number;\n};\n\ntype ParallaxDepthColors = {\n  sky: string;\n  sun: string;\n  far: string;\n  mid: string;\n  near: string;\n};\n\ntype ParallaxDepthProps = {\n  /** Custom layers. Falls back to the built-in sun and hills landscape. */\n  layers?: ParallaxDepthLayer[];\n  /** Sideways camera travel at depth 1, as a fraction of the frame width. */\n  distance?: number;\n  /** Push-in toward the scene, in depth units. */\n  push?: number;\n  /** Truck direction: 1 moves the camera right, -1 left. */\n  direction?: 1 | -1;\n  colors?: Partial<ParallaxDepthColors>;\n  /** Sky color. Overrides `colors.sky`. */\n  skyColor?: string;\n  /** Sun color. Overrides `colors.sun`. */\n  sunColor?: string;\n  seed?: number;\n  speed?: number;\n};\n\nconst parallaxDepthDefaults = {\n  distance: 0.5,\n  push: 0.16,\n  direction: 1 as 1 | -1,\n  colors: {\n    sky: \"#111111\",\n    sun: \"#E5322D\",\n    far: \"#2C2C2C\",\n    mid: \"#555555\",\n    near: \"#8E8E8E\",\n  } satisfies ParallaxDepthColors,\n  seed: 3,\n  speed: 1,\n};\n\nconst DURATION = 60;\n\n// A ridge line built from a few seeded sine octaves, spanning three widths so it\n// never runs out while the camera trucks.\nfunction hillPath(\n  width: number,\n  height: number,\n  base: number,\n  amp: number,\n  waves: number,\n  seed: string,\n) {\n  const octaves = [0, 1, 2].map((o) => ({\n    f: (waves * (o + 1) * (0.8 + random(`${seed}-f${o}`) * 0.4)) / width,\n    a: amp / (o * 1.7 + 1),\n    p: random(`${seed}-p${o}`) * Math.PI * 2,\n  }));\n  const steps = 120;\n  const x0 = -width;\n  const x1 = width * 2;\n  let d = `M ${x0} ${height * 2}`;\n  for (let i = 0; i <= steps; i++) {\n    const x = x0 + ((x1 - x0) * i) / steps;\n    let y = base;\n    for (const o of octaves)\n      y -= o.a * (0.5 + 0.5 * Math.sin(x * o.f * 6.283 + o.p));\n    d += ` L ${x.toFixed(1)} ${y.toFixed(1)}`;\n  }\n  return `${d} L ${x1} ${height * 2} Z`;\n}\n\nfunction useLandscape(colors: ParallaxDepthColors, seed: number) {\n  const { width, height } = useVideoConfig();\n  return useMemo<ParallaxDepthLayer[]>(() => {\n    const svg = (children: React.ReactNode) => (\n      <svg\n        width={width}\n        height={height}\n        viewBox={`0 0 ${width} ${height}`}\n        style={{ overflow: \"visible\" }}\n        aria-hidden\n      >\n        {children}\n      </svg>\n    );\n    const hill = (\n      base: number,\n      amp: number,\n      waves: number,\n      fill: string,\n      key: string,\n    ) => (\n      <path\n        d={hillPath(\n          width,\n          height,\n          height * base,\n          height * amp,\n          waves,\n          `${seed}-${key}`,\n        )}\n        fill={fill}\n      />\n    );\n    return [\n      {\n        depth: 9,\n        content: svg(\n          <circle\n            cx={width * 0.6}\n            cy={height * 0.4}\n            r={height * 0.17}\n            fill={colors.sun}\n          />,\n        ),\n      },\n      { depth: 6, content: svg(hill(0.6, 0.24, 2.2, colors.far, \"far\")) },\n      { depth: 2.6, content: svg(hill(0.76, 0.16, 1.2, colors.mid, \"mid\")) },\n      { depth: 1.2, content: svg(hill(0.92, 0.14, 0.9, colors.near, \"near\")) },\n    ];\n  }, [width, height, colors.sun, colors.far, colors.mid, colors.near, seed]);\n}\n\nfunction ParallaxDepthBase({\n  layers,\n  distance = parallaxDepthDefaults.distance,\n  push = parallaxDepthDefaults.push,\n  direction = parallaxDepthDefaults.direction,\n  colors: colorOverrides,\n  skyColor,\n  sunColor,\n  seed = parallaxDepthDefaults.seed,\n  speed = parallaxDepthDefaults.speed,\n}: ParallaxDepthProps) {\n  const frame = useCurrentFrame();\n  const { width, fps } = useVideoConfig();\n  const baseColors = { ...parallaxDepthDefaults.colors, ...colorOverrides };\n  const colors = {\n    ...baseColors,\n    sky: skyColor ?? baseColors.sky,\n    sun: sunColor ?? baseColors.sun,\n  };\n  const landscape = useLandscape(colors, seed);\n  const stack = [...(layers ?? landscape)].sort((a, b) => b.depth - a.depth);\n\n  const t = (frame * speed * 30) / fps;\n  const move = interpolate(t, [0, DURATION], [0, 1], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n    easing: Easing.bezier(0.45, 0, 0.2, 1),\n  });\n  const camX = (move - 0.5) * distance * width * direction;\n  const camZ = move * push;\n\n  return (\n    <AbsoluteFill style={{ backgroundColor: colors.sky, overflow: \"hidden\" }}>\n      {stack.map((layer, i) => {\n        const depth = Math.max(0.2, layer.depth);\n        // Pinhole camera: scale and offset both fall off with 1 / depth.\n        const scale = depth / Math.max(0.05, depth - camZ);\n        const x = (-camX * scale) / depth;\n        return (\n          <AbsoluteFill\n            key={i}\n            style={{\n              transform: `translateX(${x}px) scale(${scale})`,\n              transformOrigin: \"50% 55%\",\n            }}\n          >\n            {layer.content}\n          </AbsoluteFill>\n        );\n      })}\n    </AbsoluteFill>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  distance: {\n    type: \"number\",\n    default: 0.5,\n    min: 0,\n    max: 1.2,\n    step: 0.05,\n    description: \"Truck distance\",\n    hiddenFromList: false,\n  },\n  push: {\n    type: \"number\",\n    default: 0.16,\n    min: 0,\n    max: 0.6,\n    step: 0.02,\n    description: \"Push-in\",\n    hiddenFromList: false,\n  },\n  direction: {\n    type: \"enum\",\n    default: \"right\",\n    variants: {\n      right: {},\n      left: {},\n    },\n    description: \"Direction\",\n  },\n  seed: {\n    type: \"number\",\n    default: 3,\n    min: 1,\n    max: 24,\n    step: 1,\n    description: \"Seed\",\n    hiddenFromList: false,\n  },\n  skyColor: {\n    type: \"color\",\n    default: \"#111111\",\n    description: \"Sky\",\n  },\n  sunColor: {\n    type: \"color\",\n    default: \"#E5322D\",\n    description: \"Sun\",\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([\"distance\",\"push\",\"direction\",\"seed\",\"skyColor\",\"sunColor\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  distance: 0.5,\n  push: 0.16,\n  direction: \"right\",\n  seed: 3,\n  skyColor: \"#111111\",\n  sunColor: \"#E5322D\",\n  speed: 1,\n};\n\ntype ParallaxDepthElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof ParallaxDepthBase>;\n\nconst ParallaxDepthInner = forwardRef<\n  HTMLDivElement,\n  ParallaxDepthElementProps & { 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 ?? \"<ParallaxDepth>\"}\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        <ParallaxDepthBase\n          {...(componentProps as unknown as ComponentProps<typeof ParallaxDepthBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const ParallaxDepth = Interactive.withSchema({\n  Component: ParallaxDepthInner,\n  componentName: \"<ParallaxDepth>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 60,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 60
}