🎥 Make videos programmatically with React
build demo
Jonny Burger6006fe07bb70
diff --git a/packages/docs/docs/player/api.mdx b/packages/docs/docs/player/api.mdxindex b9ebbc4e53..893bd50d66 100644--- a/packages/docs/docs/player/api.mdx+++ b/packages/docs/docs/player/api.mdx@@ -68,7 +68,7 @@ The height you would like the video to have when rendered as an MP4. Use `style=### `inputProps`-Pass props to the component that you have specified using the `component` prop. The Typescript definition takes the shape of the props that you have given to your `component`. Default `undefined`.+Pass props to the component that you have specified using the `component` prop. The Typescript definition takes the shape of the props that you have given to your `component`.### `loop`diff --git a/packages/docs/docs/player/drag-and-drop/Demo.tsx b/packages/docs/docs/player/drag-and-drop/Demo.tsxindex 8cc8f1b40d..c98eef7983 100644--- a/packages/docs/docs/player/drag-and-drop/Demo.tsx+++ b/packages/docs/docs/player/drag-and-drop/Demo.tsx@@ -1,22 +1,33 @@import {Player} from '@remotion/player';import React, {useCallback, useMemo, useState} from 'react';-import type {Item} from './DraggerHandle';import type {MainProps} from './Main';import {Main} from './Main';+import type {Item} from './item';export const DragAndDropDemo: React.FC = () => {const [items, setItems] = useState<Item[]>([{- left: 480,+ left: 395,top: 270,- width: 960,+ width: 540,durationInFrames: 100,from: 0,height: 540,id: 0,+ color: '#ccc',+ },+ {+ left: 985,+ top: 270,+ width: 540,+ durationInFrames: 100,+ from: 0,+ height: 540,+ id: 1,+ color: '#ccc',},]);- const [selectedItem, setSelectedItem] = useState<number | null>(0);+ const [selectedItem, setSelectedItem] = useState<number | null>(null);const changeItem = useCallback((itemId: number, updater: (item: Item) => Item) => {diff --git a/packages/docs/docs/player/drag-and-drop/DraggerHandle.tsx b/packages/docs/docs/player/drag-and-drop/DraggerHandle.tsxindex be4f40b001..6b20cac7bd 100644--- a/packages/docs/docs/player/drag-and-drop/DraggerHandle.tsx+++ b/packages/docs/docs/player/drag-and-drop/DraggerHandle.tsx@@ -1,18 +1,9 @@import React, {useCallback, useMemo} from 'react';import {useCurrentScale} from 'remotion';+import type {Item} from './item';const REAL_SIZE = 8;-export type Item = {- id: number;- durationInFrames: number;- from: number;- height: number;- left: number;- top: number;- width: number;-};-export const DraggerHandle: React.FC<{type: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';setItem: (itemId: number, updater: (item: Item) => Item) => void;diff --git a/packages/docs/docs/player/drag-and-drop/Layer.tsx b/packages/docs/docs/player/drag-and-drop/Layer.tsxnew file mode 100644index 0000000000..e02e6dd705--- /dev/null+++ b/packages/docs/docs/player/drag-and-drop/Layer.tsx@@ -0,0 +1,37 @@+import React, {useMemo} from 'react';+import {Sequence} from 'remotion';+import type {Item} from './item';++export const REMOTION_TIMELINE_NON_EMPTY_SPACE =+ '_remotion_timeline_non_empty_space';++const container: React.CSSProperties = {+ display: 'contents',+};++export const Layer: React.FC<{+ item: Item;+}> = ({item}) => {+ const style: React.CSSProperties = useMemo(() => {+ return {+ backgroundColor: item.color,+ position: 'absolute',+ left: item.left,+ top: item.top,+ width: item.width,+ height: item.height,+ };+ }, [item.color, item.height, item.left, item.top, item.width]);++ return (+ <Sequence+ key={item.id}+ from={item.from}+ durationInFrames={item.durationInFrames}+ className={REMOTION_TIMELINE_NON_EMPTY_SPACE}+ style={container}+ >+ <div style={style} />+ </Sequence>+ );+};diff --git a/packages/docs/docs/player/drag-and-drop/Main.tsx b/packages/docs/docs/player/drag-and-drop/Main.tsxindex f6c5dabf79..fc34bf1b66 100644--- a/packages/docs/docs/player/drag-and-drop/Main.tsx+++ b/packages/docs/docs/player/drag-and-drop/Main.tsx@@ -1,6 +1,7 @@-import React from 'react';+import React, {useCallback} from 'react';import {AbsoluteFill} from 'remotion';-import type {Item} from './DraggerHandle';+import type {Item} from './item';+import {Layer} from './Layer';import {SelectionOutlines} from './SelectionOutlines';export type MainProps = {@@ -10,18 +11,38 @@ export type MainProps = {changeItem: (itemId: number, updater: (item: Item) => Item) => void;};+const outer: React.CSSProperties = {+ backgroundColor: '#eee',+};++const layerContainer: React.CSSProperties = {+ overflow: 'hidden',+};+export const Main: React.FC<MainProps> = ({items,setSelectedItem,selectedItem,changeItem,}) => {+ const onPointerDown = useCallback(+ (e: React.PointerEvent) => {+ if (e.button !== 0) {+ return;+ }++ setSelectedItem(null);+ },+ [setSelectedItem],+ );+return (- <AbsoluteFill- style={{- backgroundColor: '#eee',- }}- >+ <AbsoluteFill style={outer} onPointerDown={onPointerDown}>+ <AbsoluteFill style={layerContainer}>+ {items.map((item) => {+ return <Layer key={item.id} item={item} />;+ })}+ </AbsoluteFill><SelectionOutlinesselectedItem={selectedItem}items={items}diff --git a/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx b/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsxindex 967e0ebbae..b275ebc9c8 100644--- a/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx+++ b/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx@@ -1,8 +1,8 @@import React, {useCallback, useMemo} from 'react';import {useCurrentScale} from 'remotion';-import type {Item} from './DraggerHandle';import {DraggerHandle} from './DraggerHandle';+import type {Item} from './item';export const SelectionOutline: React.FC<{item: Item;@@ -71,6 +71,7 @@ export const SelectionOutline: React.FC<{const onPointerDown = useCallback((e: React.MouseEvent) => {+ e.stopPropagation();if (e.button !== 0) {return;}diff --git a/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx b/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsxindex deb80076f6..90ef9864cc 100644--- a/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx+++ b/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx@@ -1,7 +1,7 @@import React from 'react';import {Sequence} from 'remotion';-import type {Item} from './DraggerHandle';import {SelectionOutline} from './SelectionOutline';+import type {Item} from './item';const displaySelectedItemOnTop = (items: Item[],diff --git a/packages/docs/docs/player/drag-and-drop/index.mdx b/packages/docs/docs/player/drag-and-drop/index.mdxindex daaa7eacec..a9ae614e67 100644--- a/packages/docs/docs/player/drag-and-drop/index.mdx+++ b/packages/docs/docs/player/drag-and-drop/index.mdx@@ -7,6 +7,19 @@ crumb: '@remotion/player'import {DragAndDropDemo} from './Demo';-The Remotion Player supports reacting to mouse events allowing for building interactions on the canvas.+The Remotion Player supports reacting to mouse events allowing for building interactions on the canvas.+Try to drag and resize the elements below.<DragAndDropDemo />+<br />+Pointer events work just like in regular React. Make these Remotion-specific+considerations:++- Disable the [`controls`](/docs/player/player#controls) prop to disable any obstructing elements. You can render Playback controls [outside of the Player](/docs/player/custom-controls).+- The Player might have CSS `scale()` applied to it.+ [If you measure elements](/docs/measuring), you need to divide by the scale obtained by [`useCurrentScale()`](/docs/use-current-scale).+- You can pass state update functions via [`inputProps`](/docs/player/player#inputprops) to the Player. Alternatively, using React Context also works.++## Example++Let's build the demo above.diff --git a/packages/docs/docs/player/drag-and-drop/item.ts b/packages/docs/docs/player/drag-and-drop/item.tsindex e69de29bb2..d6aabed8f7 100644--- a/packages/docs/docs/player/drag-and-drop/item.ts+++ b/packages/docs/docs/player/drag-and-drop/item.ts@@ -0,0 +1,10 @@+export type Item = {+ id: number;+ durationInFrames: number;+ from: number;+ height: number;+ left: number;+ top: number;+ width: number;+ color: string;+};