Skip to content

🎥 Make videos programmatically with React

`@remotion/transitions`: flip transition

Jonny Burger20f0b1d2012a
diff --git a/packages/transitions/.prettierrc.js b/packages/transitions/.prettierrc.js
new file mode 100644
index 0000000000..32eb24f3bf
--- /dev/null
+++ b/packages/transitions/.prettierrc.js
@@ -0,0 +1,14 @@
+module.exports = {
+ singleQuote: true,
+ bracketSpacing: false,
+ useTabs: true,
+ overrides: [
+ {
+ files: ['*.yml'],
+ options: {
+ singleQuote: false,
+ },
+ },
+ ],
+ plugins: [require.resolve('prettier-plugin-organize-imports')],
+};
diff --git a/packages/transitions/.prettierrc.mjs b/packages/transitions/.prettierrc.mjs
deleted file mode 100644
index 7129f6a51c..0000000000
--- a/packages/transitions/.prettierrc.mjs
+++ /dev/null
@@ -1,14 +0,0 @@
-module.exports = {
- singleQuote: true,
- bracketSpacing: false,
- useTabs: true,
- overrides: [
- {
- files: ["*.yml"],
- options: {
- singleQuote: false,
- },
- },
- ],
- plugins: [require.resolve("prettier-plugin-organize-imports")],
-};
diff --git a/packages/transitions/package.json b/packages/transitions/package.json
index ae87dbd6da..801281cc19 100644
--- a/packages/transitions/package.json
+++ b/packages/transitions/package.json
@@ -75,6 +75,12 @@
"require": "./dist/cjs/presentations/wipe.js",
"types": "./dist/presentations/wipe.d.ts"
},
+ "./flip": {
+ "module": "./dist/presentations/flip.js",
+ "import": "./dist/presentations/flip.js",
+ "require": "./dist/cjs/presentations/flip.js",
+ "types": "./dist/presentations/flip.d.ts"
+ },
"./package.json": "./package.json"
},
"typesVersions": {
@@ -85,6 +91,9 @@
"slide": [
"dist/presentations/slide.d.ts"
],
+ "flip": [
+ "dist/presentations/flip.d.ts"
+ ],
"fade": [
"dist/presentations/fade.d.ts"
]
diff --git a/packages/transitions/rollup.config.js b/packages/transitions/rollup.config.js
index 002eda4bb4..9932c71a08 100644
--- a/packages/transitions/rollup.config.js
+++ b/packages/transitions/rollup.config.js
@@ -1,6 +1,8 @@
// rollup.config.js
import typescript from '@rollup/plugin-typescript';
+const presentations = ['slide', 'flip', 'wipe', 'fade'];
+
export default [
{
input: 'src/index.ts',
@@ -20,58 +22,24 @@ export default [
}),
],
},
- {
- input: 'src/presentations/slide.tsx',
- output: [
- {
- file: 'dist/cjs/slide.js',
- format: 'cjs',
- sourcemap: false,
- },
- ],
- external: ['remotion', 'react', 'react/jsx-runtime'],
- plugins: [
- typescript({
- tsconfig: 'tsconfig-cjs.json',
- sourceMap: false,
- outputToFilesystem: true,
- }),
- ],
- },
- {
- input: 'src/presentations/fade.tsx',
- output: [
- {
- file: 'dist/cjs/fade.js',
- format: 'cjs',
- sourcemap: false,
- },
- ],
- external: ['remotion', 'react', 'react/jsx-runtime'],
- plugins: [
- typescript({
- tsconfig: 'tsconfig-cjs.json',
- sourceMap: false,
- outputToFilesystem: true,
- }),
- ],
- },
- {
- input: 'src/presentations/wipe.tsx',
- output: [
- {
- file: 'dist/cjs/wipe.js',
- format: 'cjs',
- sourcemap: false,
- },
- ],
- external: ['remotion', 'react', 'react/jsx-runtime'],
- plugins: [
- typescript({
- tsconfig: 'tsconfig-cjs.json',
- sourceMap: false,
- outputToFilesystem: true,
- }),
- ],
- },
+ ...presentations.map((p) => {
+ return {
+ input: `src/presentations/${p}.tsx`,
+ output: [
+ {
+ file: `dist/cjs/${p}.js`,
+ format: 'cjs',
+ sourcemap: false,
+ },
+ ],
+ external: ['remotion', 'react', 'react/jsx-runtime'],
+ plugins: [
+ typescript({
+ tsconfig: 'tsconfig-cjs.json',
+ sourceMap: false,
+ outputToFilesystem: true,
+ }),
+ ],
+ };
+ }),
];
diff --git a/packages/transitions/src/presentations/flip.tsx b/packages/transitions/src/presentations/flip.tsx
new file mode 100644
index 0000000000..2ed4c55f13
--- /dev/null
+++ b/packages/transitions/src/presentations/flip.tsx
@@ -0,0 +1,72 @@
+
+import React, {useMemo} from 'react';
+import {AbsoluteFill, interpolate} from 'remotion';
+import type { TransitionPresentation, TransitionPresentationComponentProps } from '../types.js';
+
+export type FlipDirection = 'from-left' | 'from-right' | 'from-top' | 'from-bottom'
+
+type FlipPresentationProps = {
+ direction?: FlipDirection;
+ perspective?: number;
+};
+
+const Flip: React.FC<
+ TransitionPresentationComponentProps<FlipPresentationProps>
+> = ({children, presentationDirection, presentationProgress, passedProps: {
+ direction = "from-left",
+ perspective= 1000,
+}}) => {
+ const style: React.CSSProperties = useMemo(() => {
+ const startRotationEntering =
+ direction === 'from-left' ||
+ direction === 'from-bottom'
+ ? 180
+ : -180;
+ const endRotationEntering =
+ direction === 'from-left' ||
+ direction === 'from-bottom'
+ ? -180
+ : 180;
+
+ const rotation =
+ presentationDirection === 'entering'
+ ? interpolate(presentationProgress, [0, 1], [startRotationEntering, 0])
+ : interpolate(presentationProgress, [0, 1], [0, endRotationEntering]);
+
+ const rotateProperty =
+ direction === 'from-top' ||
+ direction === 'from-bottom'
+ ? 'rotateX'
+ : 'rotateY';
+
+ return {
+ width: '100%',
+ height: '100%',
+ transform: `${rotateProperty}(${rotation}deg)`,
+ backfaceVisibility: 'hidden',
+ WebkitBackfaceVisibility: 'hidden',
+ };
+ }, [direction, presentationDirection, presentationProgress]);
+
+ const outer: React.CSSProperties = useMemo(() => {
+ return {
+ perspective,
+ // Make children also their backface hidden
+ transformStyle: 'preserve-3d',
+ }
+ }, [perspective])
+
+ return (
+ <AbsoluteFill
+ style={outer}
+ >
+ <AbsoluteFill style={style}>{children}</AbsoluteFill>
+ </AbsoluteFill>
+ );
+};
+
+export const flip = (
+ props: FlipPresentationProps
+): TransitionPresentation<FlipPresentationProps> => {
+ return {component: Flip, props};
+};