Skip to content

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

7.8 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": null,
    "displayName": "Confetti",
    "initialProps": null,
    "slug": "confetti",
    "sourceCode": "import { AbsoluteFill, Interactive, Sequence, interpolate, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\nimport { forwardRef, useImperativeHandle, useRef, type ComponentProps } from \"react\";\n\ninterface ConfettiProps {\n  /** Number of confetti pieces. */\n  particleCount?: number;\n  colors?: string[];\n  /** Burst origin as a 0..1 fraction of the canvas. */\n  originX?: number;\n  originY?: number;\n  /** Frame the burst fires on. */\n  startFrame?: number;\n  /** Lifetime of the burst, in frames. */\n  lifetime?: number;\n  /** Initial launch speed (px/frame at the 720px reference height). */\n  power?: number;\n  /** Downward acceleration (px/frame² at the 720px reference height). */\n  gravity?: number;\n  /** Base piece size in reference px. */\n  size?: number;\n  /** PRNG seed — same seed renders an identical burst every time. */\n  seed?: number;\n}\n\ninterface Particle {\n  angle: number;\n  speed: number;\n  color: string;\n  size: number;\n  rot0: number;\n  spin: number;\n  drift: number;\n}\n\nconst DEFAULT_COLORS = [\n  \"#1d9bf0\",\n  \"#ff5da2\",\n  \"#ffd23f\",\n  \"#22c55e\",\n  \"#a855f7\",\n  \"#ff7a45\",\n];\n\n// --- Pure helpers (unit-tested) -------------------------------------------\n\n/** Deterministic PRNG (mulberry32) — returns a function yielding [0, 1). */\nfunction mulberry32(seed: number): () => number {\n  let a = seed >>> 0;\n  return () => {\n    a = (a + 0x6d2b79f5) | 0;\n    let t = Math.imul(a ^ (a >>> 15), 1 | a);\n    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\n/** Build a deterministic particle set for a seed (stable across renders). */\nfunction makeParticles({\n  count,\n  seed,\n  colors,\n}: {\n  count: number;\n  seed: number;\n  colors: string[];\n}): Particle[] {\n  const rand = mulberry32(seed);\n  const palette = colors.length > 0 ? colors : DEFAULT_COLORS;\n  const out: Particle[] = [];\n  for (let i = 0; i < count; i++) {\n    const angle = rand() * Math.PI * 2;\n    const speed = 0.35 + 0.65 * rand();\n    out.push({\n      angle,\n      speed,\n      color: palette[Math.floor(rand() * palette.length)],\n      size: 0.7 + 0.6 * rand(),\n      rot0: rand() * Math.PI * 2,\n      spin: (rand() - 0.5) * 0.6,\n      drift: (rand() - 0.5) * 2,\n    });\n  }\n  return out;\n}\n\n/** Opacity envelope over a particle's lifetime: quick fade-in, slow fade-out. */\nfunction particleOpacity(tt: number, lifetime: number): number {\n  if (tt < 0 || tt > lifetime) return 0;\n  const fadeIn = interpolate(tt, [0, 4], [0, 1], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n  });\n  const fadeOut = interpolate(tt, [lifetime * 0.7, lifetime], [1, 0], {\n    extrapolateLeft: \"clamp\",\n    extrapolateRight: \"clamp\",\n  });\n  return Math.min(fadeIn, fadeOut);\n}\n\n// --- Component -------------------------------------------------------------\n\nfunction ConfettiBase({\n  particleCount = 140,\n  colors = DEFAULT_COLORS,\n  originX = 0.5,\n  originY = 0.5,\n  startFrame = 0,\n  lifetime = 90,\n  power = 17,\n  gravity = 0.45,\n  size = 13,\n  seed = 1,\n}: ConfettiProps) {\n  const frame = useCurrentFrame();\n  const { width, height } = useVideoConfig();\n\n  const tt = frame - startFrame;\n  if (tt < 0 || tt > lifetime) return null;\n\n  const sc = height / 720;\n  const ox = width * originX;\n  const oy = height * originY;\n  const particles = makeParticles({ count: particleCount, seed, colors });\n\n  return (\n    <AbsoluteFill style={{ pointerEvents: \"none\" }}>\n      {particles.map((p, i) => {\n        const opacity = particleOpacity(tt, lifetime);\n        if (opacity <= 0) return null;\n\n        const vx = Math.cos(p.angle) * p.speed * power + p.drift;\n        const vy = Math.sin(p.angle) * p.speed * power;\n        const x = ox + vx * tt * sc;\n        const y = oy + (vy * tt + 0.5 * gravity * tt * tt) * sc;\n        const rot = p.rot0 + p.spin * tt;\n        const flutter = Math.cos(p.rot0 + p.spin * tt * 1.6);\n        const w = size * p.size * sc;\n\n        return (\n          <div\n            key={i}\n            style={{\n              position: \"absolute\",\n              left: x,\n              top: y,\n              width: w,\n              height: w * 0.62,\n              borderRadius: 2,\n              background: p.color,\n              opacity,\n              translate: \"-50% -50%\",\n              rotate: `${rot}rad`,\n              scale: `${flutter} 1`,\n            }}\n          />\n        );\n      })}\n    </AbsoluteFill>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  particleCount: {\n    type: \"number\",\n    min: 10,\n    max: 400,\n    step: 10,\n    default: 140,\n    description: \"Particles\",\n    hiddenFromList: false,\n  },\n  power: {\n    type: \"number\",\n    min: 4,\n    max: 40,\n    step: 1,\n    default: 17,\n    description: \"Power\",\n    hiddenFromList: false,\n  },\n  gravity: {\n    type: \"number\",\n    min: 0,\n    max: 1.5,\n    step: 0.05,\n    default: 0.45,\n    description: \"Gravity\",\n    hiddenFromList: false,\n  },\n  size: {\n    type: \"number\",\n    min: 4,\n    max: 30,\n    step: 1,\n    default: 13,\n    description: \"Size\",\n    hiddenFromList: false,\n  },\n  seed: {\n    type: \"number\",\n    min: 1,\n    step: 1,\n    default: 1,\n    description: \"Seed\",\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  ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"particleCount\",\"power\",\"gravity\",\"size\",\"seed\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  particleCount: 140,\n  power: 17,\n  gravity: 0.45,\n  size: 13,\n  seed: 1,\n  speed: 1,\n};\n\ntype ConfettiElementProps = InteractiveBaseProps &\n  InteractiveTransformProps &\n  ComponentProps<typeof ConfettiBase>;\n\nconst ConfettiInner = forwardRef<\n  HTMLDivElement,\n  ConfettiElementProps & { 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 ?? \"<Confetti>\"}\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        <ConfettiBase\n          {...(componentProps as unknown as ComponentProps<typeof ConfettiBase>)}\n        />\n      </div>\n    </Sequence>\n  );\n});\n\nexport const Confetti = Interactive.withSchema({\n  Component: ConfettiInner,\n  componentName: \"<Confetti>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 90,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 90
}