@remotion/motion-blur 4.0.540 → 4.0.541

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.
@@ -10,6 +10,11 @@ const remotion_1 = require("remotion");
10
10
  */
11
11
  const getNumberOfSamples = ({ shutterFraction, samples, currentFrame, }) => {
12
12
  const maxOffset = shutterFraction * samples;
13
+ // A shutter angle of 0 means no blur at all, which makes maxOffset 0 and
14
+ // the division below produce NaN. Render a single sample instead.
15
+ if (maxOffset === 0) {
16
+ return 1;
17
+ }
13
18
  const maxTimeReverse = currentFrame - maxOffset;
14
19
  const factor = Math.min(1, Math.max(0, maxTimeReverse / maxOffset + 1));
15
20
  return Math.max(1, Math.round(Math.min(factor * samples, samples)));
@@ -54,6 +54,7 @@ const MotionBlurSample = ({ children, width, height, index, count, shutterInFram
54
54
  };
55
55
  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, }) => {
56
56
  const { durationInFrames: compositionDurationInFrames } = (0, remotion_1.useVideoConfig)();
57
+ const accumulationCanvasRef = (0, react_1.useRef)(null);
57
58
  if (!Number.isInteger(samples) || samples < 1 || samples > 64) {
58
59
  throw new TypeError(`"samples" must be an integer between 1 and 64, but got ${String(samples)}`);
59
60
  }
@@ -87,15 +88,32 @@ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 1
87
88
  captureElements[0] !== element) {
88
89
  throw new Error('Motion blur samples are not ready to paint');
89
90
  }
90
- context.reset();
91
- context.globalCompositeOperation = 'lighter';
92
- context.globalAlpha = 1 / actualSamples;
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);
95
+ }
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');
104
+ }
105
+ accumulationContext.reset();
106
+ accumulationContext.globalCompositeOperation = 'lighter';
107
+ accumulationContext.globalAlpha = 1 / actualSamples;
93
108
  for (const [index, sampleElement] of captureElements.entries()) {
94
109
  const image = index === 0
95
110
  ? elementImage
96
111
  : layoutCanvas.captureElementImage(sampleElement);
97
112
  try {
113
+ // ElementImage can only be drawn back to its originating canvas.
114
+ context.reset();
98
115
  context.drawElementImage(image, 0, 0);
116
+ accumulationContext.drawImage(canvas, 0, 0);
99
117
  }
100
118
  finally {
101
119
  if (index !== 0) {
@@ -103,6 +121,8 @@ const HtmlInCanvasMotionBlurInner = ({ children, width, height, shutterAngle = 1
103
121
  }
104
122
  }
105
123
  }
124
+ context.reset();
125
+ context.drawImage(accumulationCanvas, 0, 0);
106
126
  }, [actualSamples]);
107
127
  const passThrough = remotion_1.Internals.useHtmlInCanvasUnsupportedMessage() !== null;
108
128
  const sampleElements = Array.from({ length: passThrough ? 0 : actualSamples }, (_, index) => {
@@ -7,6 +7,9 @@ var getNumberOfSamples = ({
7
7
  currentFrame
8
8
  }) => {
9
9
  const maxOffset = shutterFraction * samples;
10
+ if (maxOffset === 0) {
11
+ return 1;
12
+ }
10
13
  const maxTimeReverse = currentFrame - maxOffset;
11
14
  const factor = Math.min(1, Math.max(0, maxTimeReverse / maxOffset + 1));
12
15
  return Math.max(1, Math.round(Math.min(factor * samples, samples)));
@@ -57,7 +60,7 @@ var CameraMotionBlur = ({
57
60
  });
58
61
  };
59
62
  // src/HtmlInCanvasMotionBlur.tsx
60
- import { useCallback } from "react";
63
+ import { useCallback, useRef } from "react";
61
64
  import {
62
65
  Freeze as Freeze2,
63
66
  HtmlInCanvas,
@@ -153,6 +156,7 @@ var HtmlInCanvasMotionBlurInner = ({
153
156
  controls
154
157
  }) => {
155
158
  const { durationInFrames: compositionDurationInFrames } = useVideoConfig();
159
+ const accumulationCanvasRef = useRef(null);
156
160
  if (!Number.isInteger(samples) || samples < 1 || samples > 64) {
157
161
  throw new TypeError(`"samples" must be an integer between 1 and 64, but got ${String(samples)}`);
158
162
  }
@@ -181,19 +185,35 @@ var HtmlInCanvasMotionBlurInner = ({
181
185
  if (captureElements.length !== actualSamples || captureElements[0] !== element) {
182
186
  throw new Error("Motion blur samples are not ready to paint");
183
187
  }
184
- context.reset();
185
- context.globalCompositeOperation = "lighter";
186
- context.globalAlpha = 1 / actualSamples;
188
+ if (accumulationCanvasRef.current === null || accumulationCanvasRef.current.width !== canvas.width || accumulationCanvasRef.current.height !== canvas.height) {
189
+ accumulationCanvasRef.current = new OffscreenCanvas(canvas.width, canvas.height);
190
+ }
191
+ const accumulationCanvas = accumulationCanvasRef.current;
192
+ const accumulationSettings = {
193
+ colorSpace: "srgb",
194
+ colorType: "float16"
195
+ };
196
+ const accumulationContext = accumulationCanvas.getContext("2d", accumulationSettings);
197
+ if (!accumulationContext) {
198
+ throw new Error("Failed to acquire 2D context for motion blur samples");
199
+ }
200
+ accumulationContext.reset();
201
+ accumulationContext.globalCompositeOperation = "lighter";
202
+ accumulationContext.globalAlpha = 1 / actualSamples;
187
203
  for (const [index, sampleElement] of captureElements.entries()) {
188
204
  const image = index === 0 ? elementImage : layoutCanvas.captureElementImage(sampleElement);
189
205
  try {
206
+ context.reset();
190
207
  context.drawElementImage(image, 0, 0);
208
+ accumulationContext.drawImage(canvas, 0, 0);
191
209
  } finally {
192
210
  if (index !== 0) {
193
211
  image.close();
194
212
  }
195
213
  }
196
214
  }
215
+ context.reset();
216
+ context.drawImage(accumulationCanvas, 0, 0);
197
217
  }, [actualSamples]);
198
218
  const passThrough = Internals.useHtmlInCanvasUnsupportedMessage() !== null;
199
219
  const sampleElements = Array.from({ length: passThrough ? 0 : actualSamples }, (_, index) => {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "url": "https://github.com/remotion-dev/remotion/tree/main/packages/motion-blur"
4
4
  },
5
5
  "name": "@remotion/motion-blur",
6
- "version": "4.0.540",
6
+ "version": "4.0.541",
7
7
  "description": "Motion blur effect for Remotion",
8
8
  "main": "dist/cjs/index.js",
9
9
  "types": "dist/cjs/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/remotion-dev/remotion/issues"
22
22
  },
23
23
  "dependencies": {
24
- "remotion": "4.0.540"
24
+ "remotion": "4.0.541"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": ">=16.8.0",
@@ -39,8 +39,8 @@
39
39
  "devDependencies": {
40
40
  "react": "19.3.1",
41
41
  "react-dom": "19.3.1",
42
- "remotion": "4.0.540",
43
- "@remotion/eslint-config-internal": "4.0.540",
42
+ "remotion": "4.0.541",
43
+ "@remotion/eslint-config-internal": "4.0.541",
44
44
  "eslint": "9.19.0",
45
45
  "typescript": "7.1.0-beta"
46
46
  },