🎥 Make videos programmatically with React
scaffold out transitions
Jonny Burger97150657b8c0
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 0a29f71348..5b01cf960b 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -60,7 +60,7 @@ export {interpolateColors} from './interpolateColors';export {Loop} from './loop';export * from './random';export {registerRoot} from './register-root';-export {Sequence} from './sequencing';+export {Sequence, SequenceProps} from './sequencing';export {Series} from './series';export * from './spring';export {staticFile} from './static-file';diff --git a/packages/example/package.json b/packages/example/package.jsonindex 4201c961e7..d1c81e3ac7 100644--- a/packages/example/package.json+++ b/packages/example/package.json@@ -36,6 +36,7 @@"@remotion/media-utils": "3.0.18","@remotion/renderer": "3.0.18","@remotion/three": "3.0.18",+ "@remotion/transitions": "3.0.18","@types/express": "^4.17.11","@types/jest": "^27.4.0","@types/node": "^16.7.5",diff --git a/packages/example/src/Transitions/index.tsx b/packages/example/src/Transitions/index.tsxnew file mode 100644index 0000000000..0047f8018d--- /dev/null+++ b/packages/example/src/Transitions/index.tsx@@ -0,0 +1,42 @@+import {TransitionSeries} from '@remotion/transitions';+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';++export const BasicTransition: React.FC = () => {+ const frame = useCurrentFrame();+ const {fps} = useVideoConfig();++ const spr = spring({+ fps,+ frame,+ config: {+ damping: 200,+ },+ });+ return (+ <TransitionSeries>+ <TransitionSeries.Sequence durationInFrames={60}>+ <AbsoluteFill+ style={{+ backgroundColor: 'orange',+ }}+ />+ </TransitionSeries.Sequence>+ <TransitionSeries.Transition />+ <TransitionSeries.Sequence durationInFrames={60}>+ <AbsoluteFill+ style={{+ backgroundColor: 'green',+ }}+ />+ </TransitionSeries.Sequence>+ <TransitionSeries.Transition />+ <TransitionSeries.Sequence durationInFrames={60}>+ <AbsoluteFill+ style={{+ backgroundColor: 'red',+ }}+ />+ </TransitionSeries.Sequence>+ </TransitionSeries>+ );+};diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex f4628f3717..de6e4e7fae 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -21,6 +21,7 @@ import {SeriesTesting} from './StaggerTesting';import {StaticDemo} from './StaticServer';import {TenFrameTester} from './TenFrameTester';import ThreeBasic from './ThreeBasic';+import {BasicTransition} from './Transitions';import {VideoOnCanvas} from './VideoOnCanvas';import {Greenscreen} from './VideoOnCanvas/greenscreen';import {VideoSpeed} from './VideoSpeed';@@ -397,6 +398,16 @@ export const Index: React.FC = () => {durationInFrames={600}/></Folder>+ <Folder name="Transitions">+ <Composition+ id="basic"+ component={BasicTransition}+ height={1080}+ width={1080}+ durationInFrames={300}+ fps={30}+ />+ </Folder></>);};diff --git a/packages/transitions/.eslintrc b/packages/transitions/.eslintrcnew file mode 100644index 0000000000..d4cc0fb226--- /dev/null+++ b/packages/transitions/.eslintrc@@ -0,0 +1,3 @@+{+ "extends": "@jonny"+}diff --git a/packages/transitions/.npmignore b/packages/transitions/.npmignorenew file mode 100644index 0000000000..6fdeec6a7f--- /dev/null+++ b/packages/transitions/.npmignore@@ -0,0 +1,8 @@+dist/test/**+src+.eslintrc+tsconfig.tsbuildinfo+.prettierrc+jest.config.js+*.tgz+.turbodiff --git a/packages/transitions/.prettierrc.js b/packages/transitions/.prettierrc.jsnew file mode 100644index 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/package.json b/packages/transitions/package.jsonnew file mode 100644index 0000000000..b5f3fd9ef6--- /dev/null+++ b/packages/transitions/package.json@@ -0,0 +1,48 @@+{+ "name": "@remotion/transitions",+ "version": "3.0.18",+ "description": "Transition presets for Remotion",+ "main": "dist/index.js",+ "types": "dist/index.d.ts",+ "sideEffects": false,+ "scripts": {+ "lint": "eslint src --ext ts,tsx",+ "watch": "tsc -w",+ "build": "tsc -d"+ },+ "author": "Jonny Burger",+ "contributors": [],+ "license": "MIT",+ "repository": {+ "url": "https://github.com/remotion-dev/remotion"+ },+ "bugs": {+ "url": "https://github.com/remotion-dev/remotion/issues"+ },+ "dependencies": {+ "remotion": "3.0.18"+ },+ "devDependencies": {+ "@jonny/eslint-config": "3.0.259",+ "eslint": "8.13.0",+ "prettier": "^2.0.5",+ "prettier-plugin-organize-imports": "^2.3.4",+ "typescript": "^4.5.5",+ "remotion": "3.0.18",+ "react": "18.0.0",+ "react-dom": "18.0.0",+ "@types/react": "^18.0.12",+ "@types/react-dom": "^18.0.5"+ },+ "peerDependencies": {+ "react": ">=16.8.0",+ "react-dom": ">=16.8.0"+ },+ "keywords": [+ "remotion",+ "transitions"+ ],+ "publishConfig": {+ "access": "public"+ }+}diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsxnew file mode 100644index 0000000000..4fb59c2c1f--- /dev/null+++ b/packages/transitions/src/TransitionSeries.tsx@@ -0,0 +1,176 @@+import {Children, FC, PropsWithChildren, useMemo} from 'react';+import {+ Internals,+ measureSpring,+ Sequence,+ SequenceProps,+ spring,+ SpringConfig,+ useCurrentFrame,+ useVideoConfig,+} from 'remotion';+import {flattenChildren} from './flatten-children';+import {TransitionSeriesTransition} from './TransitionSeriesTransition';+import {TriangleEntrace} from './Triangle';++type SeriesSequenceProps = PropsWithChildren<+ {+ durationInFrames: number;+ offset?: number;+ } & Pick<SequenceProps, 'layout' | 'name'>+>;++const SeriesSequence = ({children}: SeriesSequenceProps) => {+ // eslint-disable-next-line react/jsx-no-useless-fragment+ return <>{children}</>;+};++const springConfig: Partial<SpringConfig> = {+ damping: 200,+};++const TransitionSeries: FC<{+ children: React.ReactNode;+}> & {+ Sequence: typeof SeriesSequence;+ Transition: typeof TransitionSeriesTransition;+} = ({children}) => {+ const {fps} = useVideoConfig();+ const frame = useCurrentFrame();+ let transitionOffsets = 0;+ const childrenValue = useMemo(() => {+ let startFrame = 0;+ const flattedChildren = flattenChildren(children);+ return Children.map(flattedChildren, (child, i) => {+ const castedChild = child as unknown as+ | {+ props: SeriesSequenceProps;+ type: typeof SeriesSequence;+ }+ | string;+ if (typeof castedChild === 'string') {+ // Don't throw if it's just some accidential whitespace+ if (castedChild.trim() === '') {+ return null;+ }++ throw new TypeError(+ `The <Series /> component only accepts a list of <Series.Sequence /> components as it's children, but you passed a string "${castedChild}"`+ );+ }++ if (castedChild.type === TransitionSeriesTransition) {+ return null;+ }++ if (castedChild.type !== SeriesSequence) {+ throw new TypeError(+ `The <Series /> component only accepts a list of <Series.Sequence /> and <Series.Transition /> components as it's children, but got ${castedChild} instead`+ );+ }++ const debugInfo = `index = ${i}, duration = ${castedChild.props.durationInFrames}`;++ if (!castedChild || !castedChild.props.children) {+ throw new TypeError(+ `A <Series.Sequence /> component (${debugInfo}) was detected to not have any children. Delete it to fix this error.`+ );+ }++ const durationInFramesProp = castedChild.props.durationInFrames;+ const {+ durationInFrames,+ children: _children,+ ...passedProps+ } = castedChild.props;+ Internals.validateDurationInFrames(+ durationInFramesProp,+ `of a <Series.Sequence /> component`+ );+ const offset = castedChild.props.offset ?? 0;+ if (Number.isNaN(offset)) {+ throw new TypeError(+ `The "offset" property of a <Series.Sequence /> must not be NaN, but got NaN (${debugInfo}).`+ );+ }++ if (!Number.isFinite(offset)) {+ throw new TypeError(+ `The "offset" property of a <Series.Sequence /> must be finite, but got ${offset} (${debugInfo}).`+ );+ }++ if (offset % 1 !== 0) {+ throw new TypeError(+ `The "offset" property of a <Series.Sequence /> must be finite, but got ${offset} (${debugInfo}).`+ );+ }++ const prev = flattedChildren[i - 1];+ const next = flattedChildren[i + 1];+ const currentStartFrame = startFrame + offset;+ const duration = measureSpring({+ fps,+ config: springConfig,+ });++ if (prev) {+ transitionOffsets -= duration;+ }++ const actualStartFrame = currentStartFrame + transitionOffsets;++ startFrame += durationInFramesProp + offset;++ const inner = (+ <Sequence+ from={Math.floor(actualStartFrame)}+ durationInFrames={durationInFramesProp}+ {...passedProps}+ >+ {child}+ </Sequence>+ );++ if (prev) {+ return (+ <TriangleEntrace+ type="in"+ progress={spring({+ fps,+ frame: frame - actualStartFrame,+ config: springConfig,+ })}+ >+ {inner}+ </TriangleEntrace>+ );+ }++ if (next) {+ return (+ <TriangleEntrace+ type="out"+ progress={spring({+ fps,+ frame: frame - actualStartFrame - durationInFrames + duration,+ config: springConfig,+ })}+ >+ {inner}+ </TriangleEntrace>+ );+ }++ return inner;+ });+ }, [children, fps, frame]);++ /* eslint-disable react/jsx-no-useless-fragment */+ return <>{childrenValue}</>;+};++TransitionSeries.Sequence = SeriesSequence;+TransitionSeries.Transition = TransitionSeriesTransition;++export {TransitionSeries};diff --git a/packages/transitions/src/TransitionSeriesTransition.tsx b/packages/transitions/src/TransitionSeriesTransition.tsxnew file mode 100644index 0000000000..fc97c4388a--- /dev/null+++ b/packages/transitions/src/TransitionSeriesTransition.tsx@@ -0,0 +1,5 @@+import React from 'react';++export const TransitionSeriesTransition: React.FC = () => {+ return <></>;+};diff --git a/packages/transitions/src/Triangle.tsx b/packages/transitions/src/Triangle.tsxnew file mode 100644index 0000000000..b12a02c7e4--- /dev/null+++ b/packages/transitions/src/Triangle.tsx@@ -0,0 +1,64 @@+import React, {useRef, useState} from 'react';+import {AbsoluteFill, random} from 'remotion';++type TransitionType = 'in' | 'out';++export const TriangleEntrace: React.FC<{+ progress: number;+ children: React.ReactNode;+ type: TransitionType;+}> = ({children, progress, type}) => {+ const ref = useRef<SVGSVGElement>(null);+ const width = 1;+ const height = 1;+ const [clipId] = useState(() => String(random(null)));++ const progressInDirection = type === 'in' ? progress : 1 - progress;++ const pathIn = `+ M 0 0+ L ${progressInDirection * width * 2} 0+ L ${0} ${height * 2 * progressInDirection}+ Z`;++ const pathOut = `+ M ${width} ${height}+ L ${width - 2 * progressInDirection * width} ${height}+ L ${width} ${height - 2 * progressInDirection * height}+ Z+ `;++ const path = type === 'in' ? pathIn : pathOut;++ return (+ <AbsoluteFill>+ <AbsoluteFill+ style={{+ width: '100%',+ height: '100%',+ justifyContent: 'center',+ alignItems: 'center',+ clipPath: `url(#${clipId})`,+ }}+ >+ {children}+ </AbsoluteFill>+ <AbsoluteFill>+ <svg+ ref={ref}+ viewBox={`0 0 ${width} ${height}`}+ style={{+ width: '100%',+ height: '100%',+ }}+ >+ <defs>+ <clipPath id={clipId} clipPathUnits="objectBoundingBox">+ <path d={path} fill="red" />+ </clipPath>+ </defs>+ </svg>+ </AbsoluteFill>+ </AbsoluteFill>+ );+};diff --git a/packages/transitions/src/flatten-children.ts b/packages/transitions/src/flatten-children.tsnew file mode 100644index 0000000000..7db6bcb868--- /dev/null+++ b/packages/transitions/src/flatten-children.ts@@ -0,0 +1,20 @@+import React, {PropsWithChildren} from 'react';++type ReactChildArray = ReturnType<typeof React.Children.toArray>;++export const flattenChildren = (children: React.ReactNode): ReactChildArray => {+ const childrenArray = React.Children.toArray(children);+ return childrenArray.reduce((flatChildren: ReactChildArray, child) => {+ if ((child as React.ReactElement<unknown>).type === React.Fragment) {+ return flatChildren.concat(+ flattenChildren(+ (child as React.ReactElement<PropsWithChildren<unknown>>).props+ .children+ )+ );+ }++ flatChildren.push(child);+ return flatChildren;+ }, []);+};diff --git a/packages/transitions/src/index.ts b/packages/transitions/src/index.tsnew file mode 100644index 0000000000..47b98eccab--- /dev/null+++ b/packages/transitions/src/index.ts@@ -0,0 +1,2 @@+export * from './TransitionSeries';+export * from './Triangle';diff --git a/packages/transitions/tsconfig.json b/packages/transitions/tsconfig.jsonnew file mode 100644index 0000000000..f314771498--- /dev/null+++ b/packages/transitions/tsconfig.json@@ -0,0 +1,10 @@+{+ "extends": "../tsconfig.settings.json",+ "compilerOptions": {+ "composite": true,+ "rootDir": "src",+ "outDir": "dist"+ },+ "include": ["src"],+ "references": [{"path": "../core"}]+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b3389cb4ff..df1cfd28db 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -400,6 +400,7 @@ importers:'@remotion/media-utils': 3.0.18'@remotion/renderer': 3.0.18'@remotion/three': 3.0.18+ '@remotion/transitions': 3.0.18'@types/babel__core': ^7.1.12'@types/babel__template': ^7.4.0'@types/express': ^4.17.11@@ -443,6 +444,7 @@ importers:'@remotion/media-utils': link:../media-utils'@remotion/renderer': link:../renderer'@remotion/three': link:../three+ '@remotion/transitions': link:../transitions'@types/express': 4.17.13'@types/jest': 27.4.0'@types/node': 16.10.2@@ -806,6 +808,31 @@ importers:three: 0.134.0typescript: 4.7.2+ packages/transitions:+ specifiers:+ '@jonny/eslint-config': 3.0.259+ '@types/react': ^18.0.12+ '@types/react-dom': ^18.0.5+ eslint: 8.13.0+ prettier: ^2.0.5+ prettier-plugin-organize-imports: ^2.3.4+ react: 18.0.0+ react-dom: 18.0.0+ remotion: 3.0.18+ typescript: ^4.5.5+ dependencies:+ remotion: link:../core+ devDependencies:+ '@jonny/eslint-config': [email protected][email protected]+ '@types/react': 18.0.12+ '@types/react-dom': 18.0.5+ eslint: 8.13.0+ prettier: 2.6.2+ prettier-plugin-organize-imports: [email protected][email protected]+ react: 18.0.0+ react-dom: [email protected]+ typescript: 4.7.2+packages:/@algolia/autocomplete-core/1.5.2:@@ -5785,7 +5812,7 @@ packages:peerDependencies:react: '*'dependencies:- '@types/react': 18.0.1+ '@types/react': 18.0.12prop-types: 15.8.1react: 18.0.0dev: false@@ -7854,6 +7881,12 @@ packages:dependencies:'@types/react': 18.0.1+ /@types/react-dom/18.0.5:+ resolution: {integrity: sha512-OWPWTUrY/NIrjsAPkAk1wW9LZeIjSvkXRhclsFO8CZcZGCOg2G0YZy4ft+rOyYxy8B7ui5iZzi9OkDebZ7/QSA==}+ dependencies:+ '@types/react': 18.0.12+ dev: true+/@types/react-reconciler/0.26.4:resolution: {integrity: sha512-bdx4aIBkQRDAnzc23JBFeZmVpmfLJHfHikmQukEt9qs4bQtq9f+PDbNwhR9u74FkIUyIDz1I1qJ8OF6RwadKpw==}dependencies:@@ -7864,7 +7897,7 @@ packages:resolution: {integrity: sha512-38vpjXic0+E2sIBEKUe+RrCmbc8RqcQhNV8OmU3KUcwgy/yzTeo67MhllP+0zjZWNr7Lhw+RnUkL0hzkf63nUQ==}dependencies:'@types/history': 4.7.9- '@types/react': 18.0.1+ '@types/react': 18.0.12'@types/react-router': 5.1.17dev: false@@ -7872,7 +7905,7 @@ packages:resolution: {integrity: sha512-UvyRy73318QI83haXlaMwmklHHzV9hjl3u71MmM6wYNu0hOVk9NLTa0vGukf8zXUqnwz4O06ig876YSPpeK28A==}dependencies:'@types/history': 4.7.9- '@types/react': 18.0.1+ '@types/react': 18.0.12'@types/react-router': 5.1.17dev: false@@ -7880,7 +7913,7 @@ packages:resolution: {integrity: sha512-RNSXOyb3VyRs/EOGmjBhhGKTbnN6fHWvy5FNLzWfOWOGjgVUKqJZXfpKzLmgoU8h6Hj8mpALj/mbXQASOb92wQ==}dependencies:'@types/history': 4.7.9- '@types/react': 18.0.1+ '@types/react': 18.0.12dev: false/@types/react/18.0.1:@@ -7890,6 +7923,13 @@ packages:'@types/scheduler': 0.16.2csstype: 3.0.9+ /@types/react/18.0.12:+ resolution: {integrity: sha512-duF1OTASSBQtcigUvhuiTB1Ya3OvSy+xORCiEf20H0P0lzx+/KeVsA99U5UjLXSbyo1DRJDlLKqTeM1ngosqtg==}+ dependencies:+ '@types/prop-types': 15.7.4+ '@types/scheduler': 0.16.2+ csstype: 3.0.9+/@types/retry/0.12.1:resolution: {integrity: sha512-xoDlM2S4ortawSWORYqsdU+2rxdh4LRW9ytc3zmT37RIKQh6IHyKwwtKhKis9ah8ol07DCkZxPt8BBvPjC6v4g==}dev: false@@ -11574,7 +11614,7 @@ packages:resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==}/fast-levenshtein/2.0.6:- resolution: {integrity: sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=}+ resolution: {integrity: sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==}/fast-url-parser/1.1.3:resolution: {integrity: sha1-9K8+qfNNiicc9YrSs3WfQx8LMY0=}@@ -11944,7 +11984,7 @@ packages:resolution: {integrity: sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==}/functional-red-black-tree/1.0.1:- resolution: {integrity: sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=}+ resolution: {integrity: sha512-dsKNQNdj6xA3T+QlADDA7mOSlX0qiMINjn0cgr+eGHGsbSHzTabcIogz2+p/iqP1Xs6EP/sS2SbqH+brGTbq0g==}/gauge/2.7.4:resolution: {integrity: sha512-14x4kjc6lkD3ltw589k0NrPD6cCNTD6CWoVUNpB85+DrtONoZn+Rug6xZU5RvSC4+TZPxA5AnBibQYAvZn41Hg==}@@ -12781,7 +12821,7 @@ packages:resolve-cwd: 3.0.0/imurmurhash/0.1.4:- resolution: {integrity: sha1-khi5srkoojixPcT7a21XbyMUU+o=}+ resolution: {integrity: sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==}engines: {node: '>=0.8.19'}/indent-string/4.0.0:@@ -13032,7 +13072,7 @@ packages:dev: false/is-extglob/2.1.1:- resolution: {integrity: sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=}+ resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}engines: {node: '>=0.10.0'}/is-fullwidth-code-point/1.0.0:@@ -13252,7 +13292,7 @@ packages:resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==}/isexe/2.0.0:- resolution: {integrity: sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=}+ resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}/isobject/2.1.0:resolution: {integrity: sha1-8GVWEJaj8dou9GJy+BXIQNh+DIk=}@@ -13959,7 +13999,7 @@ packages:dev: false/json-stable-stringify-without-jsonify/1.0.1:- resolution: {integrity: sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=}+ resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==}/json-stringify-safe/5.0.1:resolution: {integrity: sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=}@@ -15005,7 +15045,7 @@ packages:dev: true/natural-compare/1.4.0:- resolution: {integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=}+ resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}/negotiator/0.6.2:resolution: {integrity: sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==}