@remotion/motion-blur 4.0.544 → 4.0.545

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CameraMotionBlur = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const remotion_1 = require("remotion");
6
+ const motion_blur_context_1 = require("./motion-blur-context");
6
7
  /**
7
8
  * If the current frame is 0, then it is rendered with low opacity,
8
9
  * and the trailing elements have frame numbers -1, -2, -3, -4, etc.
@@ -25,6 +26,7 @@ const getNumberOfSamples = ({ shutterFraction, samples, currentFrame, }) => {
25
26
  */
26
27
  const CameraMotionBlur = ({ children, shutterAngle = 180, samples = 10, }) => {
27
28
  const currentFrame = (0, remotion_1.useCurrentFrame)();
29
+ const registry = (0, motion_blur_context_1.useMotionBlurRegistry)('CameraMotionBlur');
28
30
  if (typeof samples !== 'number' ||
29
31
  Number.isNaN(samples) ||
30
32
  !Number.isFinite(samples)) {
@@ -50,13 +52,16 @@ const CameraMotionBlur = ({ children, shutterAngle = 180, samples = 10, }) => {
50
52
  samples,
51
53
  shutterFraction,
52
54
  });
53
- return ((0, jsx_runtime_1.jsx)(remotion_1.AbsoluteFill, { style: { isolation: 'isolate' }, children: new Array(actualSamples).fill(true).map((_, i) => {
54
- const sample = i + 1;
55
- const sampleFrameOffset = shutterFraction * (sample / actualSamples);
56
- return ((0, jsx_runtime_1.jsx)(remotion_1.AbsoluteFill, { style: {
57
- mixBlendMode: 'plus-lighter',
58
- filter: `opacity(${1 / actualSamples})`,
59
- }, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: currentFrame - sampleFrameOffset + 1, children: children }) }, `frame-${i.toString()}`));
60
- }) }));
55
+ if (registry === null)
56
+ return null;
57
+ return ((0, jsx_runtime_1.jsxs)(remotion_1.AbsoluteFill, { style: { isolation: 'isolate' }, children: [(0, jsx_runtime_1.jsx)(remotion_1.AbsoluteFill, { style: { opacity: actualSamples > 1 ? 0 : 1 }, children: (0, jsx_runtime_1.jsx)(motion_blur_context_1.MotionBlurSampleProvider, { sampleIndex: 0, registry: registry, children: children }) }), new Array(actualSamples > 1 ? actualSamples : 0)
58
+ .fill(true)
59
+ .map((_, i) => {
60
+ const sampleFrameOffset = shutterFraction * ((i + 1) / actualSamples);
61
+ return ((0, jsx_runtime_1.jsx)(remotion_1.AbsoluteFill, { style: {
62
+ mixBlendMode: 'plus-lighter',
63
+ filter: `opacity(${1 / actualSamples})`,
64
+ }, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: currentFrame - sampleFrameOffset + 1, children: (0, jsx_runtime_1.jsx)(motion_blur_context_1.MotionBlurSampleProvider, { sampleIndex: i + 1, registry: registry, children: children }) }) }, `frame-${i.toString()}`));
65
+ })] }));
61
66
  };
62
67
  exports.CameraMotionBlur = CameraMotionBlur;
@@ -7,6 +7,7 @@ export type HtmlInCanvasMotionBlurProps = InteractiveBaseProps & InteractivePrem
7
7
  readonly shutterAngle?: number;
8
8
  readonly samples?: number;
9
9
  readonly disabled?: boolean;
10
+ readonly disabledInPreview?: boolean;
10
11
  };
11
12
  /**
12
13
  * Experimental motion blur that averages separately captured HTML-in-canvas
@@ -4,6 +4,8 @@ exports.HtmlInCanvasMotionBlur = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const remotion_1 = require("remotion");
7
+ const motion_blur_compositor_1 = require("./motion-blur-compositor");
8
+ const motion_blur_context_1 = require("./motion-blur-context");
7
9
  const htmlInCanvasMotionBlurSchema = {
8
10
  ...remotion_1.Interactive.baseSchema,
9
11
  shutterAngle: {
@@ -31,11 +33,19 @@ const htmlInCanvasMotionBlurSchema = {
31
33
  default: false,
32
34
  description: 'Disabled',
33
35
  },
36
+ disabledInPreview: {
37
+ type: 'boolean',
38
+ default: false,
39
+ description: 'Disabled in preview',
40
+ keyframable: false,
41
+ },
34
42
  };
35
- const MotionBlurSample = ({ children, width, height, index, count, shutterInFrames, firstFrame, lastFrame, }) => {
43
+ const MotionBlurSample = ({ children, width, height, index, count, shutterInFrames, firstFrame, lastFrame, registry, }) => {
36
44
  const frame = (0, remotion_1.useCurrentFrame)();
37
- const sampleFrame = Math.max(firstFrame, Math.min(lastFrame, frame + ((index + 0.5) / count - 0.5) * shutterInFrames));
38
- const isRepresentativeSample = index === Math.floor(count / 2);
45
+ const isRepresentativeSample = index === 0;
46
+ const sampleFrame = isRepresentativeSample
47
+ ? frame
48
+ : Math.max(firstFrame, Math.min(lastFrame, frame + ((index - 0.5) / count - 0.5) * shutterInFrames));
39
49
  return ((0, jsx_runtime_1.jsx)("div", { "aria-hidden": !isRepresentativeSample, ...(index === 0 ? {} : { drawable: '' }), style: {
40
50
  position: 'absolute',
41
51
  inset: 0,
@@ -48,12 +58,16 @@ const MotionBlurSample = ({ children, width, height, index, count, shutterInFram
48
58
  // Test: https://github.com/remotion-dev/remotion/blob/5d2ff05db91c1e5a02fc3d01f359638e98807882/packages/it-tests/src/ssr/render-frames.test.ts#L77
49
59
  visibility: 'visible',
50
60
  pointerEvents: isRepresentativeSample ? 'auto' : 'none',
51
- }, children: (0, jsx_runtime_1.jsx)(remotion_1.Internals.DisableSequenceRegistrationProvider, { disabled: !isRepresentativeSample, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: sampleFrame, children: children }) }) }));
61
+ }, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: sampleFrame, active: !isRepresentativeSample, children: (0, jsx_runtime_1.jsx)(motion_blur_context_1.MotionBlurSampleProvider, { sampleIndex: index, registry: registry, children: children }) }) }));
52
62
  };
53
- const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 180, samples = 8, disabled = false, from, durationInFrames, trimBefore, playbackRate, loop, freeze, hidden, name, premountFor, postmountFor, showInTimeline, styleWhilePremounted, styleWhilePostmounted, controls, }) => {
63
+ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 180, samples = 8, disabled = false, disabledInPreview = false, from, durationInFrames, trimBefore, playbackRate, loop, freeze, hidden, name, premountFor, postmountFor, showInTimeline, styleWhilePremounted, styleWhilePostmounted, controls, }) => {
54
64
  const { durationInFrames: compositionDurationInFrames } = (0, remotion_1.useVideoConfig)();
65
+ const { isRendering } = (0, remotion_1.useRemotionEnvironment)();
55
66
  const videoEnabled = remotion_1.Internals.useVideoEnabled();
56
- const accumulationCanvasRef = (0, react_1.useRef)(null);
67
+ const preferredBackend = (0, react_1.useContext)(remotion_1.Internals.PreferredEffectBackendContext);
68
+ const registry = (0, motion_blur_context_1.useMotionBlurRegistry)('HtmlInCanvasMotionBlur');
69
+ const gpuDeviceRef = (0, react_1.useRef)(null);
70
+ const compositorRef = (0, react_1.useRef)(null);
57
71
  if (!Number.isInteger(samples) || samples < 1 || samples > 64) {
58
72
  throw new TypeError(`"samples" must be an integer between 1 and 64, but got ${String(samples)}`);
59
73
  }
@@ -65,7 +79,13 @@ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 1
65
79
  }
66
80
  // Keep the first sample mounted when blur is disabled so the canvas stays visible.
67
81
  // Audio-only frames mount the children once so their audio is captured once.
68
- const actualSamples = disabled || shutterAngle === 0 || !videoEnabled ? 1 : samples;
82
+ const actualSamples = disabled ||
83
+ (disabledInPreview && !isRendering) ||
84
+ shutterAngle === 0 ||
85
+ !videoEnabled
86
+ ? 1
87
+ : samples;
88
+ const renderedSampleCount = actualSamples === 1 ? 1 : actualSamples + 1;
69
89
  const shutterInFrames = (shutterAngle / 360) * (playbackRate !== null && playbackRate !== void 0 ? playbackRate : 1);
70
90
  const firstFrame = trimBefore !== null && trimBefore !== void 0 ? trimBefore : 0;
71
91
  const visibleDuration = Math.max(0, Math.min(remotion_1.Internals.resolveSequenceDuration({
@@ -74,7 +94,17 @@ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 1
74
94
  loop,
75
95
  }), compositionDurationInFrames - (from !== null && from !== void 0 ? from : 0)));
76
96
  const lastFrame = firstFrame + Math.max(0, visibleDuration - 1) * (playbackRate !== null && playbackRate !== void 0 ? playbackRate : 1);
97
+ const onInit = (0, react_1.useCallback)(async () => {
98
+ gpuDeviceRef.current = await (0, motion_blur_compositor_1.getMotionBlurGpuDevice)();
99
+ return () => {
100
+ var _a;
101
+ gpuDeviceRef.current = null;
102
+ (_a = compositorRef.current) === null || _a === void 0 ? void 0 : _a.compositor.dispose();
103
+ compositorRef.current = null;
104
+ };
105
+ }, []);
77
106
  const onPaint = (0, react_1.useCallback)(({ canvas, element, elementImage }) => {
107
+ var _a, _b, _c;
78
108
  const context = canvas.getContext('2d');
79
109
  if (!context) {
80
110
  throw new Error('Failed to acquire 2D context for motion blur');
@@ -84,53 +114,72 @@ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 1
84
114
  throw new Error('Failed to find the HTML-in-canvas layout canvas');
85
115
  }
86
116
  const captureElements = Array.from(layoutCanvas.children);
87
- if (captureElements.length !== actualSamples ||
117
+ if (captureElements.length !== renderedSampleCount ||
88
118
  captureElements[0] !== element) {
89
119
  throw new Error('Motion blur samples are not ready to paint');
90
120
  }
91
- if (accumulationCanvasRef.current === null ||
92
- accumulationCanvasRef.current.width !== canvas.width ||
93
- accumulationCanvasRef.current.height !== canvas.height) {
94
- accumulationCanvasRef.current = new OffscreenCanvas(canvas.width, canvas.height);
121
+ if (actualSamples === 1) {
122
+ (_a = compositorRef.current) === null || _a === void 0 ? void 0 : _a.compositor.dispose();
123
+ compositorRef.current = null;
124
+ context.reset();
125
+ context.drawElementImage(elementImage, 0, 0);
126
+ return;
95
127
  }
96
- const accumulationCanvas = accumulationCanvasRef.current;
97
- const accumulationSettings = {
98
- colorSpace: 'srgb',
99
- colorType: 'float16',
100
- };
101
- const accumulationContext = accumulationCanvas.getContext('2d', accumulationSettings);
102
- if (!accumulationContext) {
103
- throw new Error('Failed to acquire 2D context for motion blur samples');
128
+ const preferWebGl = (preferredBackend !== null && preferredBackend !== void 0 ? preferredBackend : window.remotion_preferredEffectBackend) ===
129
+ 'webgl2' || Boolean(window.remotion_disableWebGpuEffects);
130
+ if ((_b = compositorRef.current) === null || _b === void 0 ? void 0 : _b.compositor.isLost()) {
131
+ if (compositorRef.current.compositor.backend === 'webgpu') {
132
+ gpuDeviceRef.current = null;
133
+ }
134
+ compositorRef.current.compositor.dispose();
135
+ compositorRef.current = null;
136
+ }
137
+ if (compositorRef.current === null ||
138
+ compositorRef.current.width !== canvas.width ||
139
+ compositorRef.current.height !== canvas.height ||
140
+ compositorRef.current.preferWebGl !== preferWebGl) {
141
+ (_c = compositorRef.current) === null || _c === void 0 ? void 0 : _c.compositor.dispose();
142
+ compositorRef.current = null;
143
+ const createdCompositor = (0, motion_blur_compositor_1.createMotionBlurCompositor)({
144
+ width: canvas.width,
145
+ height: canvas.height,
146
+ device: preferWebGl ? null : gpuDeviceRef.current,
147
+ requestPaint: () => { var _a; return (_a = layoutCanvas.requestPaint) === null || _a === void 0 ? void 0 : _a.call(layoutCanvas); },
148
+ });
149
+ compositorRef.current = {
150
+ compositor: createdCompositor,
151
+ width: canvas.width,
152
+ height: canvas.height,
153
+ preferWebGl,
154
+ };
104
155
  }
105
- accumulationContext.reset();
106
- accumulationContext.globalCompositeOperation = 'lighter';
107
- accumulationContext.globalAlpha = 1 / actualSamples;
108
- for (const [index, sampleElement] of captureElements.entries()) {
109
- const image = index === 0
110
- ? elementImage
111
- : layoutCanvas.captureElementImage(sampleElement);
156
+ const { compositor } = compositorRef.current;
157
+ // Canvas2D quantizes globalAlpha to eight bits, including float16 targets.
158
+ const sampleWeight = Math.round(255 / actualSamples) / 255;
159
+ for (const [index, sampleElement] of captureElements.slice(1).entries()) {
160
+ const image = layoutCanvas.captureElementImage(sampleElement);
112
161
  try {
113
162
  // ElementImage can only be drawn back to its originating canvas.
114
163
  context.reset();
115
164
  context.drawElementImage(image, 0, 0);
116
- accumulationContext.drawImage(canvas, 0, 0);
165
+ compositor.addSample(canvas, index, sampleWeight);
117
166
  }
118
167
  finally {
119
- if (index !== 0) {
120
- image.close();
121
- }
168
+ image.close();
122
169
  }
123
170
  }
124
171
  context.reset();
125
- context.drawImage(accumulationCanvas, 0, 0);
126
- }, [actualSamples]);
172
+ context.drawImage(compositor.finish(), 0, 0);
173
+ }, [actualSamples, preferredBackend, renderedSampleCount]);
127
174
  const passThrough = remotion_1.Internals.useHtmlInCanvasUnsupportedMessage() !== null;
128
- const sampleElements = Array.from({ length: passThrough ? 0 : actualSamples }, (_, index) => {
129
- return ((0, jsx_runtime_1.jsx)(MotionBlurSample, { width: width, height: height, index: index, count: actualSamples, shutterInFrames: shutterInFrames, firstFrame: firstFrame, lastFrame: lastFrame, children: children }, index));
175
+ if (registry === null)
176
+ return null;
177
+ const sampleElements = Array.from({ length: passThrough ? 0 : renderedSampleCount }, (_, index) => {
178
+ return ((0, jsx_runtime_1.jsx)(MotionBlurSample, { width: width, height: height, index: index, count: actualSamples, shutterInFrames: shutterInFrames, firstFrame: firstFrame, lastFrame: lastFrame, registry: registry, children: children }, index));
130
179
  });
131
180
  // The root wrapper holds the first sample. The rest must be canvas children.
132
181
  // Reuse HtmlInCanvas's Sequence for the motion blur controls.
133
- return ((0, jsx_runtime_1.jsx)(remotion_1.HtmlInCanvas, { width: width, height: height, from: from, durationInFrames: durationInFrames, trimBefore: trimBefore, playbackRate: playbackRate, loop: loop, freeze: freeze, hidden: hidden, name: name !== null && name !== void 0 ? name : '<HtmlInCanvasMotionBlur>', premountFor: premountFor, postmountFor: postmountFor, showInTimeline: showInTimeline, styleWhilePremounted: styleWhilePremounted, styleWhilePostmounted: styleWhilePostmounted, controls, onPaint: onPaint, _remotionInternalCanvasSiblings: sampleElements.slice(1), _remotionInternalFallbackLabel: "Motion blur is not visible", children: passThrough ? children : sampleElements[0] }));
182
+ return ((0, jsx_runtime_1.jsx)(remotion_1.HtmlInCanvas, { width: width, height: height, from: from, durationInFrames: durationInFrames, trimBefore: trimBefore, playbackRate: playbackRate, loop: loop, freeze: freeze, hidden: hidden, name: name !== null && name !== void 0 ? name : '<HtmlInCanvasMotionBlur>', premountFor: premountFor, postmountFor: postmountFor, showInTimeline: showInTimeline, styleWhilePremounted: styleWhilePremounted, styleWhilePostmounted: styleWhilePostmounted, controls, onPaint: onPaint, onInit: onInit, _remotionInternalCanvasSiblings: sampleElements.slice(1), _remotionInternalFallbackLabel: "Motion blur is not visible", children: passThrough ? children : sampleElements[0] }));
134
183
  };
135
184
  /**
136
185
  * Experimental motion blur that averages separately captured HTML-in-canvas
package/dist/cjs/Trail.js CHANGED
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Trail = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const remotion_1 = require("remotion");
6
+ const motion_blur_context_1 = require("./motion-blur-context");
6
7
  /*
7
8
  * @description The <Trail> component duplicates its children and adds a time offset to each layer in order to create a trail effect.
8
9
  * @see [Documentation](https://www.remotion.dev/docs/motion-blur/trail)
9
10
  */
10
11
  const Trail = ({ children, layers, lagInFrames, trailOpacity, }) => {
11
12
  const frame = (0, remotion_1.useCurrentFrame)();
13
+ const registry = (0, motion_blur_context_1.useMotionBlurRegistry)('Trail');
12
14
  if (typeof layers !== 'number' ||
13
15
  Number.isNaN(layers) ||
14
16
  !Number.isFinite(layers)) {
@@ -30,10 +32,12 @@ const Trail = ({ children, layers, lagInFrames, trailOpacity, }) => {
30
32
  !Number.isFinite(lagInFrames)) {
31
33
  throw new TypeError(`"lagInFrames" must be a number, but got ${JSON.stringify(lagInFrames)}`);
32
34
  }
35
+ if (registry === null)
36
+ return null;
33
37
  return ((0, jsx_runtime_1.jsxs)(remotion_1.AbsoluteFill, { children: [new Array(layers).fill(true).map((_, i) => {
34
38
  return ((0, jsx_runtime_1.jsx)(remotion_1.AbsoluteFill, { style: {
35
39
  opacity: trailOpacity - ((layers - i) / layers) * trailOpacity,
36
- }, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: frame - lagInFrames * (layers - i), children: children }) }, `frame-${i.toString()}`));
37
- }), children] }));
40
+ }, children: (0, jsx_runtime_1.jsx)(remotion_1.Freeze, { frame: frame - lagInFrames * (layers - i), children: (0, jsx_runtime_1.jsx)(motion_blur_context_1.MotionBlurSampleProvider, { sampleIndex: i + 1, registry: registry, children: children }) }) }, `frame-${i.toString()}`));
41
+ }), (0, jsx_runtime_1.jsx)(motion_blur_context_1.MotionBlurSampleProvider, { sampleIndex: 0, registry: registry, children: children })] }));
38
42
  };
39
43
  exports.Trail = Trail;
@@ -1,3 +1,8 @@
1
+ import { createMotionBlurCompositor, getMotionBlurGpuDevice } from './motion-blur-compositor';
1
2
  export { CameraMotionBlur, CameraMotionBlurProps } from './CameraMotionBlur';
2
3
  export { HtmlInCanvasMotionBlur, HtmlInCanvasMotionBlurProps, } from './HtmlInCanvasMotionBlur';
3
4
  export { Trail, TrailProps } from './Trail';
5
+ export declare const MotionBlurInternals: {
6
+ createMotionBlurCompositor: typeof createMotionBlurCompositor;
7
+ getMotionBlurGpuDevice: typeof getMotionBlurGpuDevice;
8
+ };
package/dist/cjs/index.js CHANGED
@@ -1,9 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Trail = exports.HtmlInCanvasMotionBlur = exports.CameraMotionBlur = void 0;
3
+ exports.MotionBlurInternals = exports.Trail = exports.HtmlInCanvasMotionBlur = exports.CameraMotionBlur = void 0;
4
+ const motion_blur_compositor_1 = require("./motion-blur-compositor");
4
5
  var CameraMotionBlur_1 = require("./CameraMotionBlur");
5
6
  Object.defineProperty(exports, "CameraMotionBlur", { enumerable: true, get: function () { return CameraMotionBlur_1.CameraMotionBlur; } });
6
7
  var HtmlInCanvasMotionBlur_1 = require("./HtmlInCanvasMotionBlur");
7
8
  Object.defineProperty(exports, "HtmlInCanvasMotionBlur", { enumerable: true, get: function () { return HtmlInCanvasMotionBlur_1.HtmlInCanvasMotionBlur; } });
8
9
  var Trail_1 = require("./Trail");
9
10
  Object.defineProperty(exports, "Trail", { enumerable: true, get: function () { return Trail_1.Trail; } });
11
+ exports.MotionBlurInternals = {
12
+ createMotionBlurCompositor: motion_blur_compositor_1.createMotionBlurCompositor,
13
+ getMotionBlurGpuDevice: motion_blur_compositor_1.getMotionBlurGpuDevice,
14
+ };
@@ -0,0 +1,15 @@
1
+ type Compositor = {
2
+ readonly backend: 'webgpu' | 'webgl2';
3
+ readonly addSample: (source: OffscreenCanvas, index: number, weight: number) => void;
4
+ readonly finish: () => HTMLCanvasElement;
5
+ readonly dispose: () => void;
6
+ readonly isLost: () => boolean;
7
+ };
8
+ export declare const getMotionBlurGpuDevice: () => Promise<GPUDevice | null>;
9
+ export declare const createMotionBlurCompositor: ({ width, height, device, requestPaint, }: {
10
+ readonly width: number;
11
+ readonly height: number;
12
+ readonly device: GPUDevice | null;
13
+ readonly requestPaint: () => void;
14
+ }) => Compositor;
15
+ export {};