Skip to content

🎥 Make videos programmatically with React

`@remotion/effects`: Promote blur() to public API with axis control

Jonny Burger3f9a13b4b6b8
diff --git a/packages/docs/docs/effects/blur.mdx b/packages/docs/docs/effects/blur.mdx
new file mode 100644
index 0000000000..16c46582e1
--- /dev/null
+++ b/packages/docs/docs/effects/blur.mdx
@@ -0,0 +1,89 @@
+---
+image: /generated/articles-docs-effects-blur.png
+id: effects-blur
+slug: /effects/blur
+title: blur()
+sidebar_label: blur()
+crumb: '@remotion/effects'
+---
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+# `blur()`<AvailableFrom v="4.0.464" />
+
+Applies a separable Gaussian blur to a canvas-backed component using WebGL2.
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill, Solid} from 'remotion';
+import {blur} from '@remotion/effects/blur';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Solid
+ color="#3344ff"
+ width={1920}
+ height={1080}
+ effects={[blur({radius: 8})]}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+You can also import from the package root:
+
+```tsx
+import {blur} from '@remotion/effects';
+```
+
+## API
+
+Pass an object with the following properties.
+
+### `radius`
+
+The blur radius in pixels. Must be a finite number.
+
+### `horizontal?`
+
+Whether to blur along the horizontal axis. Defaults to `true`.
+
+### `vertical?`
+
+Whether to blur along the vertical axis. Defaults to `true`.
+
+Set one axis to `false` for a directional blur:
+
+```tsx
+effects={[blur({radius: 8, horizontal: true, vertical: false})]}
+```
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`. This field is added automatically for all effects and can be toggled in the Remotion Studio timeline.
+
+## Usage
+
+Add the effect to the `effects` prop of a component that supports canvas effects, for example [`<Solid>`](/docs/solid), [`<Img>`](/docs/img), [`<Video>`](/docs/video), or [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas).
+
+```tsx
+<Solid
+ color="red"
+ width={400}
+ height={400}
+ effects={[blur({radius: 12}), blur({radius: 4, horizontal: false})]}
+/>
+```
+
+## Requirements
+
+`blur()` uses a WebGL2 backend. During renders, enable WebGL via [`Config.setChromiumOpenGlRenderer()`](/docs/config#setchromiumopenglrenderer) (for example `'angle'`). See [Using WebGL during renders](/docs/remotion/html-in-canvas#using-webgl-during-renders).
+
+## See also
+
+- [`@remotion/effects`](/docs/effects/api)
+- [HTML-in-canvas guide](/docs/html-in-canvas)
+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/blur/index.ts)
diff --git a/packages/docs/docs/effects/index.mdx b/packages/docs/docs/effects/index.mdx
new file mode 100644
index 0000000000..d83c852912
--- /dev/null
+++ b/packages/docs/docs/effects/index.mdx
@@ -0,0 +1,22 @@
+---
+image: /generated/articles-docs-effects-index.png
+id: effects-api
+slug: /effects/api
+title: '@remotion/effects'
+---
+
+# @remotion/effects<AvailableFrom v="4.0.464" />
+
+Built-in [canvas effects](/docs/html-in-canvas) for Remotion components such as [`<Solid>`](/docs/solid), [`<Img>`](/docs/img), and [`<Video>`](/docs/video).
+
+import {TableOfContents} from './table-of-contents';
+
+<Installation pkg="@remotion/effects" />
+
+## Effects
+
+<TableOfContents />
+
+## License
+
+[Remotion License](/docs/license)
diff --git a/packages/docs/docs/effects/table-of-contents.tsx b/packages/docs/docs/effects/table-of-contents.tsx
new file mode 100644
index 0000000000..00f13a0625
--- /dev/null
+++ b/packages/docs/docs/effects/table-of-contents.tsx
@@ -0,0 +1,16 @@
+import React from 'react';
+import {Grid} from '../../components/TableOfContents/Grid';
+import {TOCItem} from '../../components/TableOfContents/TOCItem';
+
+export const TableOfContents: React.FC = () => {
+ return (
+ <div>
+ <Grid>
+ <TOCItem link="/docs/effects/blur">
+ <strong>{'blur()'}</strong>
+ <div>Gaussian blur for canvas components</div>
+ </TOCItem>
+ </Grid>
+ </div>
+ );
+};
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 348d06f1cc..11c0401f05 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -268,6 +268,15 @@ const sidebars: SidebarsConfig = {
},
items: ['enable-scss/enable-scss'],
},
+ {
+ type: 'category',
+ label: '@remotion/effects',
+ link: {
+ type: 'doc',
+ id: 'effects/effects-api',
+ },
+ items: ['effects/blur'],
+ },
{
type: 'category',
label: '@remotion/elevenlabs',
diff --git a/packages/effects/README.md b/packages/effects/README.md
index 5610e106c6..31aecf3b88 100644
--- a/packages/effects/README.md
+++ b/packages/effects/README.md
@@ -1,7 +1,14 @@
# @remotion/effects
-Internal presets for Remotion canvas effect hooks.
+Built-in effects for Remotion canvas components.
## Usage
-Presets are exposed via `EffectInternals` only. This package has no public effect documentation yet.
+```tsx
+import {blur} from '@remotion/effects/blur';
+import {Solid} from 'remotion';
+
+<Solid color="red" width={400} height={400} effects={[blur({radius: 8})]} />;
+```
+
+See the [documentation](https://www.remotion.dev/docs/effects/api).
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 6f291f7bae..f5f395d6f8 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -5,7 +5,7 @@ if (process.env.NODE_ENV !== 'production') {
throw new Error('This script must be run using NODE_ENV=production');
}
-const effectEntrypoints = ['src/index.ts'];
+const effectEntrypoints = ['src/index.ts', 'src/blur.ts'];
console.time('Generated.');
const output = await build({
diff --git a/packages/effects/package.json b/packages/effects/package.json
index 242d4be033..4b7899a502 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -1,7 +1,7 @@
{
"name": "@remotion/effects",
"version": "4.0.464",
- "description": "Internal presets for Remotion canvas effect hooks",
+ "description": "Built-in effects for Remotion canvas components",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/esm/index.mjs",
@@ -32,8 +32,14 @@
"module": "./dist/esm/index.mjs",
"import": "./dist/esm/index.mjs"
},
+ "./blur": {
+ "types": "./dist/blur.d.ts",
+ "module": "./dist/esm/blur.mjs",
+ "import": "./dist/esm/blur.mjs"
+ },
"./package.json": "./package.json"
},
+ "homepage": "https://www.remotion.dev/docs/effects",
"devDependencies": {
"@remotion/eslint-config-internal": "workspace:*",
"eslint": "catalog:",
diff --git a/packages/effects/src/blur.ts b/packages/effects/src/blur.ts
new file mode 100644
index 0000000000..d98425d880
--- /dev/null
+++ b/packages/effects/src/blur.ts
@@ -0,0 +1 @@
+export {blur, type BlurParams} from './blur/index.js';
diff --git a/packages/effects/src/blur/blur-runtime.ts b/packages/effects/src/blur/blur-runtime.ts
index 8bb2ddaf9c..db0634b711 100644
--- a/packages/effects/src/blur/blur-runtime.ts
+++ b/packages/effects/src/blur/blur-runtime.ts
@@ -256,26 +256,25 @@ type ApplyBlurParams = {
readonly width: number;
readonly height: number;
readonly radius: number;
+ readonly horizontal: boolean;
+ readonly vertical: boolean;
};
-export const applyBlur = ({
- state,
+const uploadBlurSource = ({
+ gl,
+ textureSource,
source,
+ textureIntermediate,
width,
height,
- radius,
-}: ApplyBlurParams): void => {
- const {
- gl,
- programHorizontal,
- programVertical,
- textureSource,
- textureIntermediate,
- framebuffer,
- } = state;
-
- gl.viewport(0, 0, width, height);
-
+}: {
+ gl: WebGL2RenderingContext;
+ textureSource: WebGLTexture;
+ source: CanvasImageSource;
+ textureIntermediate: WebGLTexture;
+ width: number;
+ height: number;
+}): void => {
gl.bindTexture(gl.TEXTURE_2D, textureSource);
gl.texImage2D(
gl.TEXTURE_2D,
@@ -299,38 +298,105 @@ export const applyBlur = ({
null,
);
gl.bindTexture(gl.TEXTURE_2D, null);
+};
- gl.clearColor(0, 0, 0, 0);
+export const applyBlur = ({
+ state,
+ source,
+ width,
+ height,
+ radius,
+ horizontal,
+ vertical,
+}: ApplyBlurParams): void => {
+ const {
+ gl,
+ programHorizontal,
+ programVertical,
+ textureSource,
+ textureIntermediate,
+ framebuffer,
+ } = state;
- // Pass 1: horizontal blur from `textureSource` into `textureIntermediate`.
- gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
- gl.clear(gl.COLOR_BUFFER_BIT);
- gl.activeTexture(gl.TEXTURE0);
- gl.bindTexture(gl.TEXTURE_2D, textureSource);
- gl.useProgram(programHorizontal);
- setBlurUniforms({
+ gl.viewport(0, 0, width, height);
+
+ uploadBlurSource({
gl,
- uniforms: state.horizontal,
- radius,
+ textureSource,
+ source,
+ textureIntermediate,
width,
height,
});
- drawFullscreenQuad(state);
- // Pass 2: vertical blur from `textureIntermediate` onto the default framebuffer.
+ gl.clearColor(0, 0, 0, 0);
+
+ if (!horizontal && !vertical) {
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, textureSource);
+ gl.useProgram(programVertical);
+ setBlurUniforms({
+ gl,
+ uniforms: state.vertical,
+ radius: 0,
+ width,
+ height,
+ });
+ drawFullscreenQuad(state);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.useProgram(null);
+ return;
+ }
+
+ if (horizontal && vertical) {
+ // Pass 1: horizontal blur from `textureSource` into `textureIntermediate`.
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, textureSource);
+ gl.useProgram(programHorizontal);
+ setBlurUniforms({
+ gl,
+ uniforms: state.horizontal,
+ radius,
+ width,
+ height,
+ });
+ drawFullscreenQuad(state);
+
+ // Pass 2: vertical blur from `textureIntermediate` onto the default framebuffer.
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.bindTexture(gl.TEXTURE_2D, textureIntermediate);
+ gl.useProgram(programVertical);
+ setBlurUniforms({
+ gl,
+ uniforms: state.vertical,
+ radius,
+ width,
+ height,
+ });
+ drawFullscreenQuad(state);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.useProgram(null);
+ return;
+ }
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.clear(gl.COLOR_BUFFER_BIT);
- gl.bindTexture(gl.TEXTURE_2D, textureIntermediate);
- gl.useProgram(programVertical);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, textureSource);
+ gl.useProgram(horizontal ? programHorizontal : programVertical);
setBlurUniforms({
gl,
- uniforms: state.vertical,
+ uniforms: horizontal ? state.horizontal : state.vertical,
radius,
width,
height,
});
drawFullscreenQuad(state);
-
gl.bindTexture(gl.TEXTURE_2D, null);
gl.useProgram(null);
};
diff --git a/packages/effects/src/blur/index.ts b/packages/effects/src/blur/index.ts
index 9e7c984226..bbd04e3da0 100644
--- a/packages/effects/src/blur/index.ts
+++ b/packages/effects/src/blur/index.ts
@@ -15,8 +15,24 @@ import {
export type BlurParams = {
readonly radius: number;
+ /** Apply blur along the horizontal axis. Defaults to `true`. */
+ readonly horizontal?: boolean;
+ /** Apply blur along the vertical axis. Defaults to `true`. */
+ readonly vertical?: boolean;
};
+type BlurResolved = {
+ readonly radius: number;
+ readonly horizontal: boolean;
+ readonly vertical: boolean;
+};
+
+const resolveBlurParams = (params: BlurParams): BlurResolved => ({
+ radius: params.radius,
+ horizontal: params.horizontal ?? true,
+ vertical: params.vertical ?? true,
+});
+
const blurSchema = {
radius: {
type: 'number',
@@ -26,6 +42,16 @@ const blurSchema = {
default: undefined,
description: 'Blur radius',
},
+ horizontal: {
+ type: 'boolean',
+ default: true,
+ description: 'Blur horizontally',
+ },
+ vertical: {
+ type: 'boolean',
+ default: true,
+ description: 'Blur vertically',
+ },
} as const satisfies SequenceSchema;
const validateBlurParams = (params: BlurParams): void => {
@@ -37,15 +63,21 @@ export const blur = createEffect<BlurParams, BlurState>({
type: 'remotion/blur',
label: 'Blur',
backend: 'webgl2',
- calculateKey: (params) => String(params.radius),
+ calculateKey: (params) => {
+ const r = resolveBlurParams(params);
+ return `${r.radius}-${r.horizontal ? 1 : 0}-${r.vertical ? 1 : 0}`;
+ },
setup: (target) => setupBlur(target),
apply: ({source, width, height, params, state}) => {
+ const r = resolveBlurParams(params);
applyBlur({
state,
source,
width,
height,
- radius: params.radius,
+ radius: r.radius,
+ horizontal: r.horizontal,
+ vertical: r.vertical,
});
},
cleanup: (state) => cleanupBlur(state),
diff --git a/packages/effects/src/effect-internals.ts b/packages/effects/src/effect-internals.ts
index 2a8a46007f..b214c5c514 100644
--- a/packages/effects/src/effect-internals.ts
+++ b/packages/effects/src/effect-internals.ts
@@ -1,10 +1,8 @@
-import {blur} from './blur/index.js';
import {halftone} from './halftone.js';
import {tint} from './tint.js';
import {wave} from './wave.js';
export const EffectInternals = {
- blur,
halftone,
tint,
wave,
diff --git a/packages/effects/src/index.ts b/packages/effects/src/index.ts
index 7a03d864ce..aaf9b952f3 100644
--- a/packages/effects/src/index.ts
+++ b/packages/effects/src/index.ts
@@ -1,5 +1,5 @@
+export {blur, type BlurParams} from './blur/index.js';
export {EffectInternals} from './effect-internals.js';
-export type {BlurParams} from './blur/index.js';
export {halftoneSchema, type HalftoneParams} from './halftone.js';
export {tintSchema, type TintParams} from './tint.js';
export {waveSchema, type WaveParams} from './wave.js';
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index a9ae748b2f..de1dec8740 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -21,3 +21,21 @@ test('blur() throws when radius is not passed', () => {
test('blur() accepts valid params', () => {
expect(() => blur({radius: 4})).not.toThrow();
});
+
+test('blur() accepts horizontal-only blur', () => {
+ expect(() =>
+ blur({radius: 4, horizontal: true, vertical: false}),
+ ).not.toThrow();
+});
+
+test('blur() accepts vertical-only blur', () => {
+ expect(() =>
+ blur({radius: 4, horizontal: false, vertical: true}),
+ ).not.toThrow();
+});
+
+test('blur() accepts both axes disabled', () => {
+ expect(() =>
+ blur({radius: 4, horizontal: false, vertical: false}),
+ ).not.toThrow();
+});
diff --git a/packages/example/src/AnimatedImage/Effects.tsx b/packages/example/src/AnimatedImage/Effects.tsx
index f8bf48b917..e059209ac3 100644
--- a/packages/example/src/AnimatedImage/Effects.tsx
+++ b/packages/example/src/AnimatedImage/Effects.tsx
@@ -1,4 +1,5 @@
import {EffectInternals} from '@remotion/effects';
+import {blur} from '@remotion/effects/blur';
import {StudioInternals} from '@remotion/studio';
import React from 'react';
import {
@@ -72,7 +73,7 @@ const Comp: React.FC = () => {
fit="contain"
style={{borderRadius: 16}}
effects={[
- EffectInternals.blur({
+ blur({
radius: blurRadius,
}),
EffectInternals.tint({
diff --git a/packages/example/src/EffectsTestbed/EffectsTestbed.tsx b/packages/example/src/EffectsTestbed/EffectsTestbed.tsx
index 558488b3c5..fcf77450c2 100644
--- a/packages/example/src/EffectsTestbed/EffectsTestbed.tsx
+++ b/packages/example/src/EffectsTestbed/EffectsTestbed.tsx
@@ -1,4 +1,5 @@
import {EffectInternals} from '@remotion/effects';
+import {blur} from '@remotion/effects/blur';
import {LightLeakInternals} from '@remotion/light-leaks';
import {Video} from '@remotion/media';
import {StarburstInternals} from '@remotion/starburst';
@@ -142,7 +143,7 @@ const AnimatedStackVideo: React.FC = () => {
colors: ['#ff5fa2', '#ff0000'],
rays: 12,
}),
- EffectInternals.blur({radius: 24}),
+ blur({radius: 24}),
EffectInternals.wave({
amplitude: 22,
wavelength: 180,
@@ -216,7 +217,7 @@ export const EffectsTestbed: React.FC = () => {
muted
loop
objectFit="cover"
- effects={[EffectInternals.blur({radius: 24})]}
+ effects={[blur({radius: 24})]}
/>
</Tile>
<Tile title="solid" subtitle="light leak, progress from frame">
diff --git a/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx b/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx
index 59734bacd6..5e74bd7a08 100644
--- a/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx
+++ b/packages/example/src/ErrorOverlayE2e/ErrorOverlayRepro.tsx
@@ -1,7 +1,7 @@
// Regression fixture for https://github.com/remotion-dev/remotion/issues/7447
// The error overlay does not dismiss after a runtime error is fixed.
// The e2e test toggles the radius argument below to reproduce.
-import {EffectInternals} from '@remotion/effects';
+import {blur} from '@remotion/effects/blur';
import React from 'react';
import {AbsoluteFill, Solid} from 'remotion';
@@ -15,7 +15,7 @@ export const ErrorOverlayRepro: React.FC = () => {
height={400}
color="#ff5fa2"
style={STYLE}
- effects={[EffectInternals.blur({radius: 24})]}
+ effects={[blur({radius: 24})]}
/>
</AbsoluteFill>
);
diff --git a/packages/example/src/Gif/GifEffects.tsx b/packages/example/src/Gif/GifEffects.tsx
index 3567c76a7b..166550ae83 100644
--- a/packages/example/src/Gif/GifEffects.tsx
+++ b/packages/example/src/Gif/GifEffects.tsx
@@ -1,4 +1,5 @@
import {EffectInternals} from '@remotion/effects';
+import {blur} from '@remotion/effects/blur';
import {Gif} from '@remotion/gif';
import {StudioInternals} from '@remotion/studio';
import React from 'react';
@@ -90,7 +91,7 @@ const AnimatedBlurGif: React.FC = () => {
src={GIF_SRC}
fit="contain"
style={tileGifStyle}
- effects={[EffectInternals.blur({radius: blurRadius})]}
+ effects={[blur({radius: blurRadius})]}
/>
);
};
@@ -135,7 +136,7 @@ const StackedGif: React.FC = () => {
sliceWidth: 4,
background: '#020617',
}),
- EffectInternals.blur({radius: 6}),
+ blur({radius: 6}),
]}
/>
);
diff --git a/packages/example/src/Rive/RiveEffects.tsx b/packages/example/src/Rive/RiveEffects.tsx
index f3b7a0d0d5..1299ed6770 100644
--- a/packages/example/src/Rive/RiveEffects.tsx
+++ b/packages/example/src/Rive/RiveEffects.tsx
@@ -1,4 +1,5 @@
import {EffectInternals} from '@remotion/effects';
+import {blur} from '@remotion/effects/blur';
import {RemotionRiveCanvas} from '@remotion/rive';
import {StudioInternals} from '@remotion/studio';
import React from 'react';
@@ -86,7 +87,7 @@ const AnimatedBlurRive: React.FC = () => {
<RemotionRiveCanvas
src={RIVE_SRC}
style={tileRiveStyle}
- effects={[EffectInternals.blur({radius: blurRadius})]}
+ effects={[blur({radius: blurRadius})]}
/>
);
};
@@ -129,7 +130,7 @@ const StackedRive: React.FC = () => {
sliceWidth: 4,
background: '#020617',
}),
- EffectInternals.blur({radius: 6}),
+ blur({radius: 6}),
]}
/>
);