Skip to content

🎥 Make videos programmatically with React

start writing custom presentation docs

Jonny Burger00f6de5c478f
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 1f01ca736a..29d5f9f985 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -7,6 +7,7 @@ import styles from "./styles.module.css";
import type { DemoType } from "./types";
import {
circleDemo,
+ customPresentationDemo,
ellipseDemo,
fadePresentationDemo,
noiseDemo,
@@ -47,6 +48,7 @@ const demos: DemoType[] = [
fadePresentationDemo,
wipePresentationDemo,
slidePresentationDemo,
+ customPresentationDemo,
];
export const Demo: React.FC<{
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index e09c3999a0..da49e2cc4a 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -1,4 +1,9 @@
-import { FadeDemo, SlideDemo, WipeDemo } from "../transitions/previews";
+import {
+ CustomTransitionDemo,
+ FadeDemo,
+ SlideDemo,
+ WipeDemo,
+} from "../transitions/previews";
import { CircleDemo } from "./Circle";
import { EllipseDemo } from "./Ellipse";
import { NoiseComp } from "./NoiseDemo";
@@ -528,3 +533,14 @@ export const wipePresentationDemo: DemoType = {
},
],
};
+
+export const customPresentationDemo: DemoType = {
+ comp: CustomTransitionDemo,
+ compHeight: 280,
+ compWidth: 540,
+ durationInFrames: 60,
+ fps: 30,
+ id: "custom-presentation",
+ autoPlay: true,
+ options: [],
+};
diff --git a/packages/docs/components/transitions/custom-transition.tsx b/packages/docs/components/transitions/custom-transition.tsx
new file mode 100644
index 0000000000..b5cb0e9d74
--- /dev/null
+++ b/packages/docs/components/transitions/custom-transition.tsx
@@ -0,0 +1,75 @@
+import { getBoundingBox, translatePath } from "@remotion/paths";
+import { makeStar } from "@remotion/shapes";
+import type {
+ TransitionPresentation,
+ TransitionPresentationComponentProps,
+} from "@remotion/transitions";
+import React, { useMemo, useState } from "react";
+import { AbsoluteFill, random } from "remotion";
+
+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%",
+ justifyContent: "center",
+ alignItems: "center",
+ clipPath:
+ presentationDirection === "exiting" ? undefined : `url(#${clipId})`,
+ };
+ }, [clipId, presentationDirection]);
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill style={style}>{children}</AbsoluteFill>
+ <AbsoluteFill>
+ <svg>
+ <defs>
+ <clipPath id={clipId}>
+ <path d={translatedPath} fill="black" />
+ </clipPath>
+ </defs>
+ </svg>
+ </AbsoluteFill>
+ </AbsoluteFill>
+ );
+};
+
+export const customPresentation = (
+ props: CustomPresentationProps,
+): TransitionPresentation<CustomPresentationProps> => {
+ return { component: SlidePresentation, props };
+};
diff --git a/packages/docs/components/transitions/previews.tsx b/packages/docs/components/transitions/previews.tsx
index d134a61f0d..6a82d7b5ec 100644
--- a/packages/docs/components/transitions/previews.tsx
+++ b/packages/docs/components/transitions/previews.tsx
@@ -8,7 +8,8 @@ import { slide } from "@remotion/transitions/slide";
import type { WipeDirection } from "@remotion/transitions/wipe";
import { wipe } from "@remotion/transitions/wipe";
import React, { useEffect, useRef } from "react";
-import { AbsoluteFill } from "remotion";
+import { AbsoluteFill, useVideoConfig } from "remotion";
+import { customPresentation } from "./custom-transition";
export const PresentationPreview: React.FC<{
effect: TransitionPresentation<Record<string, unknown>>;
@@ -96,7 +97,7 @@ export const SampleTransition: React.FC<{
}> = ({ effect }) => {
return (
<TransitionSeries>
- <TransitionSeries.Sequence durationInFrames={45}>
+ <TransitionSeries.Sequence durationInFrames={75}>
<SceneA />
</TransitionSeries.Sequence>
<TransitionSeries.Transition
@@ -105,7 +106,7 @@ export const SampleTransition: React.FC<{
config: {
damping: 200,
},
- durationInFrames: 45,
+ durationInFrames: 60,
durationRestThreshold: 0.001,
})}
/>
@@ -131,3 +132,9 @@ export const WipeDemo: React.FC<{
}> = ({ direction }) => {
return <SampleTransition effect={wipe({ direction })} />;
};
+
+export const CustomTransitionDemo: React.FC<{}> = () => {
+ const { width, height } = useVideoConfig();
+
+ return <SampleTransition effect={customPresentation({ height, width })} />;
+};
diff --git a/packages/docs/docs/transitions/presentations/custom.mdx b/packages/docs/docs/transitions/presentations/custom.mdx
index 12726d8c80..77e50cd258 100644
--- a/packages/docs/docs/transitions/presentations/custom.mdx
+++ b/packages/docs/docs/transitions/presentations/custom.mdx
@@ -2,3 +2,20 @@
crumb: "@remotion/transitions"
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:
+
+<Step>1</Step> The current <code>presentationProgress</code>, influenced by the
+timing of the transition
+<br />
+<Step>2</Step> The <code>presentationDirection</code>, either <code>
+ entering
+</code> or <code>exiting</code>
+<br />
+<Step>3</Step> Additional developer-defined <code>passedProps</code>
diff --git a/packages/example/src/Transitions/CustomTransition.tsx b/packages/example/src/Transitions/CustomTransition.tsx
index 0caf5f46dc..8adca958c5 100644
--- a/packages/example/src/Transitions/CustomTransition.tsx
+++ b/packages/example/src/Transitions/CustomTransition.tsx
@@ -46,7 +46,8 @@ const SlidePresentation: React.FC<
height: '100%',
justifyContent: 'center',
alignItems: 'center',
- clipPath: presentationDirection === 'out' ? undefined : `url(#${clipId})`,
+ clipPath:
+ presentationDirection === 'exiting' ? undefined : `url(#${clipId})`,
};
}, [clipId, presentationDirection]);
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index 1592f40df6..a19e16da0a 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -1,10 +1,11 @@
import type { FC, PropsWithChildren } from "react";
import { Children, useMemo } from "react";
import type { LayoutAndStyle, SequencePropsWithoutDuration } from "remotion";
-import { Internals, Sequence, useCurrentFrame, useVideoConfig } from "remotion";
+import { Sequence, useCurrentFrame, useVideoConfig } from "remotion";
import { slide } from "./presentations/slide.js";
import type { TransitionSeriesTransitionProps } from "./types.js";
import { flattenChildren } from "./flatten-children.js";
+import { validateDurationInFrames } from "./validate.js";
// eslint-disable-next-line react/function-component-definition
const TransitionSeriesTransition = function <
@@ -129,7 +130,7 @@ const TransitionSeriesChildren: FC<{ children: React.ReactNode }> = ({
children: _children,
...passedProps
} = castedChildAgain.props;
- Internals.validateDurationInFrames(durationInFramesProp, {
+ validateDurationInFrames(durationInFramesProp, {
component: `of a <TransitionSeries.Sequence /> component`,
allowFloats: true,
});
@@ -228,14 +229,14 @@ const TransitionSeriesChildren: FC<{ children: React.ReactNode }> = ({
// @ts-expect-error
<UppercaseNextPresentation
passedProps={nextPresentation.props ?? {}}
- presentationDirection="out"
+ presentationDirection="exiting"
presentationProgress={nextProgress}
>
{/**
// @ts-expect-error */}
<UppercasePrevPresentation
passedProps={prevPresentation.props ?? {}}
- presentationDirection="in"
+ presentationDirection="entering"
presentationProgress={prevProgress}
>
{inner}
@@ -253,7 +254,7 @@ const TransitionSeriesChildren: FC<{ children: React.ReactNode }> = ({
// @ts-expect-error
<UppercasePrevPresentation
passedProps={prevPresentation.props ?? {}}
- presentationDirection="in"
+ presentationDirection="entering"
presentationProgress={prevProgress}
>
{inner}
@@ -270,7 +271,7 @@ const TransitionSeriesChildren: FC<{ children: React.ReactNode }> = ({
// @ts-expect-error
<UppercaseNextPresentation
passedProps={nextPresentation.props ?? {}}
- presentationDirection="out"
+ presentationDirection="exiting"
presentationProgress={nextProgress}
>
{inner}
diff --git a/packages/transitions/src/presentations/fade.tsx b/packages/transitions/src/presentations/fade.tsx
index 62ffe4aca5..d05b832d69 100644
--- a/packages/transitions/src/presentations/fade.tsx
+++ b/packages/transitions/src/presentations/fade.tsx
@@ -13,7 +13,7 @@ const FadePresentation: React.FC<
> = ({ children, presentationDirection, presentationProgress }) => {
const style: React.CSSProperties = useMemo(() => {
return {
- opacity: presentationDirection === "in" ? presentationProgress : 1,
+ opacity: presentationDirection === "entering" ? presentationProgress : 1,
};
}, [presentationDirection, presentationProgress]);
diff --git a/packages/transitions/src/presentations/slide.tsx b/packages/transitions/src/presentations/slide.tsx
index 15845ba5ee..9147f7983b 100644
--- a/packages/transitions/src/presentations/slide.tsx
+++ b/packages/transitions/src/presentations/slide.tsx
@@ -24,7 +24,7 @@ const SlidePresentation: React.FC<
passedProps: { direction = "from-left" },
}) => {
const directionStyle = useMemo((): React.CSSProperties => {
- if (presentationDirection === "out") {
+ if (presentationDirection === "exiting") {
switch (direction) {
case "from-left":
return {
diff --git a/packages/transitions/src/presentations/wipe.tsx b/packages/transitions/src/presentations/wipe.tsx
index dd1acc066f..f93ab5a5be 100644
--- a/packages/transitions/src/presentations/wipe.tsx
+++ b/packages/transitions/src/presentations/wipe.tsx
@@ -164,12 +164,12 @@ const WipePresentation: React.FC<
const [clipId] = useState(() => String(random(null)));
const progressInDirection =
- presentationDirection === "in"
+ presentationDirection === "entering"
? presentationProgress
: 1 - presentationProgress;
const path =
- presentationDirection === "in"
+ presentationDirection === "entering"
? makePathIn(progressInDirection, direction)
: makePathOut(progressInDirection, direction);
diff --git a/packages/transitions/src/types.ts b/packages/transitions/src/types.ts
index 38db304e5c..fdb0582eef 100644
--- a/packages/transitions/src/types.ts
+++ b/packages/transitions/src/types.ts
@@ -1,35 +1,35 @@
-import type {ComponentType} from 'react';
+import type { ComponentType } from "react";
-export type TransitionDirection = 'in' | 'out';
+export type PresentationDirection = "entering" | "exiting";
export type TransitionTiming = {
- getDurationInFrames: (options: {fps: number}) => number;
- getProgress: (options: {frame: number; fps: number}) => number;
+ getDurationInFrames: (options: { fps: number }) => number;
+ getProgress: (options: { frame: number; fps: number }) => number;
};
export type TransitionSeriesTransitionProps<
- PresentationProps extends Record<string, unknown>
+ PresentationProps extends Record<string, unknown>
> = {
- timing: TransitionTiming;
- presentation?: TransitionPresentation<PresentationProps>;
+ timing: TransitionTiming;
+ presentation?: TransitionPresentation<PresentationProps>;
};
type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
export type TransitionPresentation<
- PresentationProps extends Record<string, unknown>
+ PresentationProps extends Record<string, unknown>
> = {
- component: LooseComponentType<
- TransitionPresentationComponentProps<PresentationProps>
- >;
- props: PresentationProps;
+ component: LooseComponentType<
+ TransitionPresentationComponentProps<PresentationProps>
+ >;
+ props: PresentationProps;
};
export type TransitionPresentationComponentProps<
- PresentationProps extends Record<string, unknown>
+ PresentationProps extends Record<string, unknown>
> = {
- presentationProgress: number;
- children: React.ReactNode;
- presentationDirection: TransitionDirection;
- passedProps: PresentationProps;
+ presentationProgress: number;
+ children: React.ReactNode;
+ presentationDirection: PresentationDirection;
+ passedProps: PresentationProps;
};
diff --git a/packages/transitions/src/validate.ts b/packages/transitions/src/validate.ts
new file mode 100644
index 0000000000..1f3ad56fd3
--- /dev/null
+++ b/packages/transitions/src/validate.ts
@@ -0,0 +1,5 @@
+/* eslint-disable prefer-destructuring */
+import { Internals } from "remotion";
+
+export const validateDurationInFrames: typeof Internals.validateDurationInFrames =
+ Internals.validateDurationInFrames;