Skip to content

🎥 Make videos programmatically with React

example

Jonny Burger3328c5978b62
diff --git a/packages/docs/components/transitions/custom-transition.tsx b/packages/docs/components/transitions/custom-transition.tsx
index b5cb0e9d74..18eada0839 100644
--- a/packages/docs/components/transitions/custom-transition.tsx
+++ b/packages/docs/components/transitions/custom-transition.tsx
@@ -45,8 +45,6 @@ const SlidePresentation: React.FC<
return {
width: "100%",
height: "100%",
- justifyContent: "center",
- alignItems: "center",
clipPath:
presentationDirection === "exiting" ? undefined : `url(#${clipId})`,
};
@@ -55,15 +53,17 @@ const SlidePresentation: React.FC<
return (
<AbsoluteFill>
<AbsoluteFill style={style}>{children}</AbsoluteFill>
- <AbsoluteFill>
- <svg>
- <defs>
- <clipPath id={clipId}>
- <path d={translatedPath} fill="black" />
- </clipPath>
- </defs>
- </svg>
- </AbsoluteFill>
+ {presentationDirection === "exiting" ? null : (
+ <AbsoluteFill>
+ <svg>
+ <defs>
+ <clipPath id={clipId}>
+ <path d={translatedPath} fill="black" />
+ </clipPath>
+ </defs>
+ </svg>
+ </AbsoluteFill>
+ )}
</AbsoluteFill>
);
};
diff --git a/packages/docs/docs/transitions/presentations/custom.mdx b/packages/docs/docs/transitions/presentations/custom.mdx
index 77e50cd258..ac5c00d083 100644
--- a/packages/docs/docs/transitions/presentations/custom.mdx
+++ b/packages/docs/docs/transitions/presentations/custom.mdx
@@ -5,8 +5,6 @@ title: "Custom presentations"
This page describes how to create your own custom effect for [`<TransitionSeries>`](/docs/transitions/transitionseries).
-<Demo type="custom-presentation" />
-
## Concept
A presentation is a higher order component which wraps around the entering slide as well as the exiting slide and which implements an effect based on three parameters:
@@ -19,3 +17,172 @@ timing of the transition
</code> or <code>exiting</code>
<br />
<Step>3</Step> Additional developer-defined <code>passedProps</code>
+
+## Boilerplate
+
+A custom presentation is a function which returns an object of type `TransitionPresentation`.
+It is an object with a React component (`component`) and React props which are passed to the component as `passedProps`.
+
+```tsx twoslash title="custom-presentation.tsx"
+import type { TransitionPresentationComponentProps } from "@remotion/transitions";
+
+const SlidePresentation: React.FC<
+ TransitionPresentationComponentProps<CustomPresentationProps>
+> = ({ children, presentationDirection, presentationProgress, passedProps }) =>
+ null;
+
+// ---cut---
+
+import type { TransitionPresentation } from "@remotion/transitions";
+
+type CustomPresentationProps = {
+ width: number;
+ height: number;
+};
+
+export const customPresentation = (
+ props: CustomPresentationProps,
+): TransitionPresentation<CustomPresentationProps> => {
+ return { component: SlidePresentation, props };
+};
+```
+
+The `component` is a React component which receives the following props:
+
+<Step>1</Step> <code>children</code>: The slide to wrap around
+<br />
+<Step>2</Step> <code>presentationDirection</code>: Either <code>
+ "entering"
+</code> or <code>"exiting"</code>
+<br />
+<Step>3</Step> <code>presentationProgress</code>: A number between <code>
+ 0
+</code> and <code>1</code> which represents the progress of the transition.
+<br />
+<Step>4</Step> <code>passedProps</code>: The custom props passed to the presentation
+
+<br />
+<br />
+
+```tsx twoslash title="SlidePresentation.tsx"
+type CustomPresentationProps = {
+ width: number;
+ height: number;
+};
+
+// ---cut---
+import type { TransitionPresentationComponentProps } from "@remotion/transitions";
+import { AbsoluteFill } from "remotion";
+
+const SlidePresentation: React.FC<
+ TransitionPresentationComponentProps<CustomPresentationProps>
+> = ({
+ children,
+ presentationDirection,
+ presentationProgress,
+ passedProps,
+}) => {
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill>{children}</AbsoluteFill>
+ </AbsoluteFill>
+ );
+};
+```
+
+## Example
+
+<Demo type="custom-presentation" />
+
+The following example implements a star mask transition:
+
+<Step>1</Step> Based on the <code>passedProps</code> <code>height</code> and <code>
+ width
+</code>
+, the inner radius of the star is calculated that will completely fill the canvas.
+<br />
+<Step>2</Step> Using <a href="/docs/shapes">
+ <code>@remotion/shapes</code>
+</a>, an SVG path is calculated, and grown from zero to the full size of the canvas. <br />
+<Step>3</Step> The <code>presentationProgress</code> is used to interpolate the shape
+size. <br /> <Step>4</Step> <a href="/docs/paths">
+ <code>@remotion/paths</code>
+</a> is used to center the star in the middle of the canvas.
+<br />
+<Step>5</Step> A <code>clipPath</code> is used to clip the entering slide.
+Inside the container, the <code>children</code> get rendered.
+<br />
+<Step>6</Step> The effect is disabled if <code>presentationDirection</code> is set
+to <code>"exiting"</code>{" "}
+
+<br />
+<br />
+
+```tsx twoslash title="SlidePresentation.tsx"
+import { getBoundingBox, translatePath } from "@remotion/paths";
+import { makeStar } from "@remotion/shapes";
+import type { TransitionPresentationComponentProps } from "@remotion/transitions";
+import React, { useMemo, useState } from "react";
+import { AbsoluteFill, random } from "remotion";
+
+export type CustomPresentationProps = {
+ width: number;
+ height: number;
+};
+
+const SlidePresentation: React.FC<
+ TransitionPresentationComponentProps<CustomPresentationProps>
+> = ({
+ children,
+ presentationDirection,
+ presentationProgress,
+ passedProps,
+}) => {
+ const finishedRadius =
+ Math.sqrt(passedProps.width ** 2 + passedProps.height ** 2) / 2;
+ const innerRadius = finishedRadius * presentationProgress;
+ const outerRadius = finishedRadius * 2 * presentationProgress;
+
+ const { path } = makeStar({
+ innerRadius,
+ outerRadius,
+ points: 5,
+ });
+
+ const boundingBox = getBoundingBox(path);
+
+ const translatedPath = translatePath(
+ path,
+ passedProps.width / 2 - boundingBox.width / 2,
+ passedProps.height / 2 - boundingBox.height / 2,
+ );
+
+ const [clipId] = useState(() => String(random(null)));
+
+ const style: React.CSSProperties = useMemo(() => {
+ return {
+ width: "100%",
+ height: "100%",
+ clipPath:
+ presentationDirection === "exiting" ? undefined : `url(#${clipId})`,
+ };
+ }, [clipId, presentationDirection]);
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill style={style}>{children}</AbsoluteFill>
+ {presentationDirection === "exiting" ? null : (
+ <AbsoluteFill>
+ <svg>
+ <defs>
+ <clipPath id={clipId}>
+ <path d={translatedPath} fill="black" />
+ </clipPath>
+ </defs>
+ </svg>
+ </AbsoluteFill>
+ )}
+ </AbsoluteFill>
+ );
+};
+```