🎥 Make videos programmatically with React
Support timing props in HtmlInCanvasMotionBlur
JonnyBurger5f06c793e09e
diff --git a/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx b/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdxindex 4c3cf916ca..f57d137063 100644--- a/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx+++ b/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx@@ -68,6 +68,24 @@ The canvas width in pixels. Must be a positive integer. Use the composition widtThe canvas height in pixels. Must be a positive integer. Use the composition height from [`useVideoConfig()`](/docs/use-video-config) to cover the full frame.+### `from?`++Moves the blurred content on its parent timeline. Default: `0`.++### `durationInFrames?`++Limits how long the blurred content appears on its parent timeline, starting at `from`. By default, it lasts until the composition ends.++### `trimBefore?`++Skips the beginning of the content without moving its `from` position. Default: `0`.++### `playbackRate?`++Changes how quickly the child frames advance. Default: `1`. The motion blur samples follow this speed.++See [Timing and trimming](/docs/timing) for how these props combine.+### `samples?`How many snapshots are averaged. Default: `8`, min: `1`, max: `64`.diff --git a/packages/docs/docs/timing.mdx b/packages/docs/docs/timing.mdxindex dd590cc8d6..39c9ecba2e 100644--- a/packages/docs/docs/timing.mdx+++ b/packages/docs/docs/timing.mdx@@ -11,6 +11,7 @@ The way timing works is standardized across these components:- [`Interactive` elements](/docs/interactive), such as `<Interactive.Div>`, and [`<AbsoluteFill>`](/docs/absolute-fill).- [`<Img>`](/docs/img), [`<CanvasImage>`](/docs/canvasimage), [`<AnimatedImage>`](/docs/animatedimage), [`<Solid>`](/docs/solid) and [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas).+- [`<HtmlInCanvasMotionBlur>`](/docs/motion-blur/html-in-canvas-motion-blur) from `@remotion/motion-blur`.- [`<Gif>`](/docs/gif/gif), [`<Lottie>`](/docs/lottie), [`<ThreeCanvas>`](/docs/three-canvas) and [`<RemotionRiveCanvas>`](/docs/rive/remotionrivecanvas).- [`@remotion/shapes`](/docs/shapes), [`@remotion/rough-notation`](/docs/rough-notation/api), [`<MacOSCursor>`](/docs/mac-cursors/mac-os-cursor).- [`<Audio>`](/docs/media/audio) and [`<Video>`](/docs/media/video) from `@remotion/media`.@@ -34,8 +35,6 @@ export const MyComposition = () => ();```-At composition frame `30`, the children of `<Interactive.Div>` start at their local frame `0`.-## `durationInFrames`: Limit an item`durationInFrames` sets how long an item stays on its parent timeline, starting at `from`.@@ -121,7 +120,7 @@ export const MyComposition = () => (`loop` on `<Audio>` and `<Video>` repeats the source range selected by `trimBefore` and `trimAfter`.-If `trimAfter` is omitted, it `loops` from `trimBefore` after the end of the source.+If `trimAfter` is omitted, it loops from `trimBefore` after the end of the source.<TimingTimeline from={30} durationInFrames={90} trimBefore={45} trimAfter={105} playbackRate={2} loop />@@ -148,20 +147,17 @@ At 30 FPS, this repeats the selected 60 source frames three times over 90 composThink of the timing props as applying to the content in this order:-1. Trim the beginning with `trimBefore`. For `<Audio>` and `<Video>`, `trimAfter` also sets where the selected range ends.-2. Place the trimmed content on the parent timeline with `from`. The first selected frame appears at `from`.-3. Change its speed with `playbackRate`. A rate of `2` plays two selected frames per timeline frame without moving the starting position.-4. For `<Audio>` and `<Video>`, `loop` repeats the selected range after it finishes. Without `loop`, playback stops at `trimAfter` or the end of the file.-5. Limit the item on the parent timeline with `durationInFrames`, measured from `from`. Video or audio can end sooner if it does not loop.+1. `from`, `trimBefore` and `trimAfter` defines the selected source range+2. `playbackRate` stretches or compresses the selected range+3. `loop` (on `<Audio>` and `<Video>`) loops the stretched range+4. `durationInFrames` is applied last and defines the duration of the item in the timeline.For an item with children, [`useCurrentFrame()`](/docs/use-current-frame) at parent frame `t` is:-```text+```trimBefore + (t - from) * playbackRate```-For the video examples above, frames `45` through `104` are selected. At `2×` speed, they play from composition frames `30` through `59`. With `loop` and `durationInFrames={90}`, the same range repeats at frames `60` and `90`, and the video occupies frames `30` through `119`.-## See also- [`<Sequence>`](/docs/sequence)diff --git a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsxindex 6d96c85187..fd2d16a68e 100644--- a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx+++ b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx@@ -2,12 +2,16 @@ import React, {useCallback} from 'react';import {Freeze,HtmlInCanvas,+ type HtmlInCanvasProps,type HtmlInCanvasOnPaint,useCurrentFrame,useVideoConfig,} from 'remotion';-export type HtmlInCanvasMotionBlurProps = {+export type HtmlInCanvasMotionBlurProps = Pick<+ HtmlInCanvasProps,+ 'from' | 'durationInFrames' | 'trimBefore' | 'playbackRate'+> & {readonly children: React.ReactNode;readonly width: number;readonly height: number;@@ -15,6 +19,55 @@ export type HtmlInCanvasMotionBlurProps = {readonly samples?: number;};+type MotionBlurSampleProps = {+ readonly children: React.ReactNode;+ readonly width: number;+ readonly height: number;+ readonly index: number;+ readonly count: number;+ readonly shutterInFrames: number;+ readonly firstFrame: number;+ readonly lastFrame: number;+};++const MotionBlurSample: React.FC<MotionBlurSampleProps> = ({+ children,+ width,+ height,+ index,+ count,+ shutterInFrames,+ firstFrame,+ lastFrame,+}) => {+ const frame = useCurrentFrame();+ const sampleFrame = Math.max(+ firstFrame,+ Math.min(+ lastFrame,+ frame + ((index + 0.5) / count - 0.5) * shutterInFrames,+ ),+ );++ return (+ <div+ aria-hidden={index !== Math.floor(count / 2)}+ // Each sample has its own paint record so it can be captured separately.+ {...{drawable: ''}}+ style={{+ position: 'absolute',+ inset: 0,+ width,+ height,+ isolation: 'isolate',+ pointerEvents: index === Math.floor(count / 2) ? 'auto' : 'none',+ }}+ >+ <Freeze frame={sampleFrame}>{children}</Freeze>+ </div>+ );+};+/*** Experimental motion blur that averages separately captured HTML-in-canvas* snapshots of the children at fractional frames. Preview requires Chrome with@@ -27,9 +80,12 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({height,shutterAngle = 180,samples = 8,+ from,+ durationInFrames,+ trimBefore,+ playbackRate,}) => {- const frame = useCurrentFrame();- const {durationInFrames} = useVideoConfig();+ const {durationInFrames: compositionDurationInFrames} = useVideoConfig();if (!Number.isInteger(samples) || samples < 1 || samples > 64) {throw new TypeError(@@ -49,7 +105,17 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({}const actualSamples = shutterAngle === 0 ? 1 : samples;- const shutterInFrames = shutterAngle / 360;+ const shutterInFrames = (shutterAngle / 360) * (playbackRate ?? 1);+ const firstFrame = trimBefore ?? 0;+ const visibleDuration = Math.max(+ 0,+ Math.min(+ durationInFrames ?? Infinity,+ compositionDurationInFrames - (from ?? 0),+ ),+ );+ const lastFrame =+ firstFrame + Math.max(0, visibleDuration - 1) * (playbackRate ?? 1);const onPaint: HtmlInCanvasOnPaint = useCallback(({canvas, element, elementImage}) => {@@ -97,32 +163,19 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({);const sampleElements = Array.from({length: actualSamples}, (_, index) => {- const sampleFrame = Math.max(- 0,- Math.min(- durationInFrames - 1,- frame + ((index + 0.5) / actualSamples - 0.5) * shutterInFrames,- ),- );-return (- <div+ <MotionBlurSamplekey={index}- aria-hidden={index !== Math.floor(actualSamples / 2)}- // Each sample has its own paint record so it can be captured separately.- {...{drawable: ''}}- style={{- position: 'absolute',- inset: 0,- width,- height,- isolation: 'isolate',- pointerEvents:- index === Math.floor(actualSamples / 2) ? 'auto' : 'none',- }}+ width={width}+ height={height}+ index={index}+ count={actualSamples}+ shutterInFrames={shutterInFrames}+ firstFrame={firstFrame}+ lastFrame={lastFrame}>- <Freeze frame={sampleFrame}>{children}</Freeze>- </div>+ {children}+ </MotionBlurSample>);});@@ -131,6 +184,11 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({<HtmlInCanvaswidth={width}height={height}+ from={from}+ durationInFrames={durationInFrames}+ trimBefore={trimBefore}+ playbackRate={playbackRate}+ name="<HtmlInCanvasMotionBlur>"onPaint={onPaint}_remotionInternalCanvasSiblings={sampleElements.slice(1)}>