Skip to content

🎥 Make videos programmatically with React

`@remotion/effects`: Add region blur effect (#10226)

Jonny Burger3e1ff3e49802
diff --git a/packages/docs/components/effects-demos/registry.ts b/packages/docs/components/effects-demos/registry.ts
index 11f0191f77..a67e411274 100644
--- a/packages/docs/components/effects-demos/registry.ts
+++ b/packages/docs/components/effects-demos/registry.ts
@@ -39,6 +39,7 @@ import {pixelDissolve} from '@remotion/effects/pixel-dissolve';
import {pixelate} from '@remotion/effects/pixelate';
import {radialProgressiveBlur} from '@remotion/effects/radial-progressive-blur';
import {radialProgressivePixelate} from '@remotion/effects/radial-progressive-pixelate';
+import {regionBlur} from '@remotion/effects/region-blur';
import {rings} from '@remotion/effects/rings';
import {roughenEdges} from '@remotion/effects/roughen-edges';
import {saturation} from '@remotion/effects/saturation';
@@ -117,6 +118,7 @@ import {
RADIAL_PROGRESSIVE_BLUR_PREVIEW_PARAMS,
} from '../effects/effects-radial-progressive-blur-preview';
import {EffectsRadialProgressivePixelatePreview} from '../effects/effects-radial-progressive-pixelate-preview';
+import {EffectsRegionBlurPreview} from '../effects/effects-region-blur-preview';
import {EffectsRingsPreview} from '../effects/effects-rings-preview';
import {EffectsRoughenEdgesPreview} from '../effects/effects-roughen-edges-preview';
import {EffectsSaturationPreview} from '../effects/effects-saturation-preview';
@@ -553,6 +555,21 @@ export const effectsDemos: EffectsDemoType[] = [
schema: radialProgressiveBlur().definition.schema,
initialValues: RADIAL_PROGRESSIVE_BLUR_PREVIEW_PARAMS,
},
+ {
+ ...defaults,
+ id: 'effects-region-blur',
+ effectName: 'regionBlur',
+ effectImportPath: '@remotion/effects/region-blur',
+ comp: EffectsRegionBlurPreview,
+ schema: regionBlur({
+ topLeft: [0.28, 0.18],
+ bottomRight: [0.72, 0.82],
+ }).definition.schema,
+ initialValues: {
+ topLeft: [0.28, 0.18],
+ bottomRight: [0.72, 0.82],
+ },
+ },
{
...defaults,
id: 'effects-light-trail',
diff --git a/packages/docs/components/effects/effects-region-blur-preview.tsx b/packages/docs/components/effects/effects-region-blur-preview.tsx
new file mode 100644
index 0000000000..44a84e0065
--- /dev/null
+++ b/packages/docs/components/effects/effects-region-blur-preview.tsx
@@ -0,0 +1,33 @@
+import {
+ regionBlur,
+ type RegionBlurUvCoordinate,
+} from '@remotion/effects/region-blur';
+import React from 'react';
+import {CanvasImage} from 'remotion';
+import {EFFECTS_PREVIEW_IMAGE_SRC} from './effects-preview-image';
+
+export const EffectsRegionBlurPreview: React.FC<{
+ readonly topLeft: RegionBlurUvCoordinate;
+ readonly bottomRight: RegionBlurUvCoordinate;
+ readonly blurRadius: number;
+ readonly feather: number;
+ readonly roundness: number;
+}> = ({topLeft, bottomRight, blurRadius, feather, roundness}) => {
+ return (
+ <CanvasImage
+ src={EFFECTS_PREVIEW_IMAGE_SRC}
+ width={1280}
+ height={720}
+ fit="cover"
+ effects={[
+ regionBlur({
+ topLeft,
+ bottomRight,
+ blurRadius,
+ feather,
+ roundness,
+ }),
+ ]}
+ />
+ );
+};
diff --git a/packages/docs/docs/effects/region-blur.mdx b/packages/docs/docs/effects/region-blur.mdx
new file mode 100644
index 0000000000..1539a84d49
--- /dev/null
+++ b/packages/docs/docs/effects/region-blur.mdx
@@ -0,0 +1,94 @@
+---
+image: /generated/articles-docs-effects-region-blur.png
+slug: /effects/region-blur
+title: regionBlur()
+sidebar_label: regionBlur()
+crumb: '@remotion/effects'
+---
+
+# regionBlur()<AvailableFrom v="4.0.507" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+Applies a Gaussian blur inside a rectangular or rounded region.
+
+Use it to obscure faces, license plates, documents, and other sensitive parts of canvas-based components such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas), and [`<Solid>`](/docs/solid).
+
+## Preview
+
+<EffectsDemo type="effects-region-blur" />
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill} from 'remotion';
+import {Video} from '@remotion/media';
+import {regionBlur} from '@remotion/effects/region-blur';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Video
+ src="https://remotion.media/video.mp4"
+ effects={[
+ regionBlur({
+ topLeft: [0.3, 0.2],
+ bottomRight: [0.7, 0.8],
+ blurRadius: 60,
+ feather: 8,
+ roundness: 1,
+ }),
+ ]}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+## API
+
+Pass an object with the following properties.
+
+### `topLeft`
+
+Top-left corner of the blurred region in UV coordinates.
+
+Pass a `[number, number]` tuple where `[0, 0]` is the top-left of the canvas and `[1, 1]` is the bottom-right. Values outside this range place the corner outside the visible area.
+
+### `bottomRight`
+
+Bottom-right corner of the blurred region in UV coordinates.
+
+Both values must be greater than their corresponding `topLeft` values. Values outside the `[0, 1]` range are accepted.
+
+### `blurRadius?`
+
+Gaussian blur radius in pixels. Defaults to `40`. Must be at least `0`.
+
+### `feather?`
+
+Width of the softened region edge in pixels. Defaults to `0`. Must be at least `0`.
+
+Feathering blends the original image into the edge of the blurred region. For anonymization, make the region large enough that recognizable details remain inside the fully blurred area.
+
+### `roundness?`
+
+Corner roundness from `0` to `1`. Defaults to `0`.
+
+`0` creates a rectangle. `1` creates a circle for a square region or a pill for a rectangular region.
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`.
+
+## Requirements
+
+`regionBlur()` uses a WebGL2 backend. See [Using WebGL and WebGPU during renders](/docs/webgl).
+
+## See also
+
+- [`@remotion/effects`](/docs/effects/api)
+- [`blur()`](/docs/effects/blur)
+- [`radialProgressiveBlur()`](/docs/effects/radial-progressive-blur)
+- [HTML-in-canvas guide](/docs/html-in-canvas)
+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/region-blur/index.ts)
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 84ab832631..8a2cd276f6 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -314,6 +314,7 @@ const sidebars: SidebarsConfig = {
'effects/pixelate',
'effects/radial-progressive-blur',
'effects/radial-progressive-pixelate',
+ 'effects/region-blur',
'effects/rings',
'effects/roughen-edges',
'effects/saturation',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 39f7257ad8..5e82ab7b5e 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2150,6 +2150,15 @@ export const articles = [
noAi: false,
slug: 'effects/radial-progressive-pixelate',
},
+ {
+ id: 'effects/region-blur',
+ title: 'regionBlur()',
+ relativePath: 'docs/effects/region-blur.mdx',
+ compId: 'articles-docs-effects-region-blur',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/region-blur',
+ },
{
id: 'effects/rings',
title: 'rings()',
diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsx
index 9c7efe9ce8..d85e3fd046 100644
--- a/packages/docs/src/remotion/Root.tsx
+++ b/packages/docs/src/remotion/Root.tsx
@@ -63,6 +63,7 @@ import {
EffectsRadialProgressivePixelatePreview,
RADIAL_PROGRESSIVE_PIXELATE_PREVIEW_PARAMS,
} from '../../components/effects/effects-radial-progressive-pixelate-preview';
+import {EffectsRegionBlurPreview} from '../../components/effects/effects-region-blur-preview';
import {EffectsRingsPreview} from '../../components/effects/effects-rings-preview';
import {
EffectsRoughenEdgesPreview,
@@ -500,6 +501,19 @@ export const RemotionRoot: React.FC = () => {
height={720}
defaultProps={RADIAL_PROGRESSIVE_BLUR_PREVIEW_PARAMS}
/>
+ <Still
+ id="effects-region-blur-preview"
+ component={EffectsRegionBlurPreview}
+ width={1280}
+ height={720}
+ defaultProps={{
+ topLeft: [0.28, 0.18],
+ bottomRight: [0.72, 0.82],
+ blurRadius: 60,
+ feather: 8,
+ roundness: 0.5,
+ }}
+ />
<Still
id="effects-radial-progressive-pixelate-preview"
component={EffectsRadialProgressivePixelatePreview}
diff --git a/packages/docs/static/generated/articles-docs-effects-region-blur.png b/packages/docs/static/generated/articles-docs-effects-region-blur.png
new file mode 100644
index 0000000000..9753fa2707
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-region-blur.png differ
diff --git a/packages/docs/static/img/effects-region-blur-preview.png b/packages/docs/static/img/effects-region-blur-preview.png
new file mode 100644
index 0000000000..121fe35d2e
Binary files /dev/null and b/packages/docs/static/img/effects-region-blur-preview.png differ
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 4c0f1060da..2a8a0372c5 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -49,6 +49,7 @@ const effectEntrypoints = [
'src/pattern.ts',
'src/radial-progressive-blur.ts',
'src/radial-progressive-pixelate.ts',
+ 'src/region-blur.ts',
'src/rings.ts',
'src/saturation.ts',
'src/scanlines.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index f5faeab029..8fed807c28 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -244,6 +244,11 @@
"module": "./dist/esm/radial-progressive-pixelate.mjs",
"import": "./dist/esm/radial-progressive-pixelate.mjs"
},
+ "./region-blur": {
+ "types": "./dist/region-blur.d.ts",
+ "module": "./dist/esm/region-blur.mjs",
+ "import": "./dist/esm/region-blur.mjs"
+ },
"./rings": {
"types": "./dist/rings.d.ts",
"module": "./dist/esm/rings.mjs",
@@ -474,6 +479,9 @@
"radial-progressive-pixelate": [
"dist/radial-progressive-pixelate.d.ts"
],
+ "region-blur": [
+ "dist/region-blur.d.ts"
+ ],
"rings": [
"dist/rings.d.ts"
],
diff --git a/packages/effects/src/region-blur.ts b/packages/effects/src/region-blur.ts
new file mode 100644
index 0000000000..e59420311f
--- /dev/null
+++ b/packages/effects/src/region-blur.ts
@@ -0,0 +1,5 @@
+export {
+ regionBlur,
+ type RegionBlurParams,
+ type RegionBlurUvCoordinate,
+} from './region-blur/index.js';
diff --git a/packages/effects/src/region-blur/index.ts b/packages/effects/src/region-blur/index.ts
new file mode 100644
index 0000000000..07c9f94d12
--- /dev/null
+++ b/packages/effects/src/region-blur/index.ts
@@ -0,0 +1,156 @@
+import type {InteractivitySchema} from 'remotion';
+import {Internals} from 'remotion';
+import {
+ assertOptionalFiniteNumber,
+ validateNonNegative,
+ validateUnitInterval,
+} from '../color-utils.js';
+import {assertEffectParamsObject} from '../validate-effect-param.js';
+import {
+ applyRegionBlur,
+ cleanupRegionBlur,
+ setupRegionBlur,
+ type RegionBlurState,
+} from './region-blur-runtime.js';
+
+const {createEffect} = Internals;
+
+const DEFAULT_BLUR_RADIUS = 40 as const;
+const DEFAULT_FEATHER = 0 as const;
+const DEFAULT_ROUNDNESS = 0 as const;
+
+export type RegionBlurUvCoordinate = readonly [number, number];
+
+export type RegionBlurParams = {
+ /** Top-left corner of the region in UV coordinates. */
+ readonly topLeft: RegionBlurUvCoordinate;
+ /** Bottom-right corner of the region in UV coordinates. */
+ readonly bottomRight: RegionBlurUvCoordinate;
+ /** Gaussian blur radius in pixels. Defaults to `40`. */
+ readonly blurRadius?: number;
+ /** Width of the softened region edge in pixels. Defaults to `0`. */
+ readonly feather?: number;
+ /** Corner roundness from rectangular (`0`) to fully rounded (`1`). Defaults to `0`. */
+ readonly roundness?: number;
+};
+
+type RegionBlurResolved = {
+ readonly topLeft: RegionBlurUvCoordinate;
+ readonly bottomRight: RegionBlurUvCoordinate;
+ readonly blurRadius: number;
+ readonly feather: number;
+ readonly roundness: number;
+};
+
+export const regionBlurSchema = {
+ topLeft: {
+ type: 'uv-coordinate',
+ min: -1,
+ max: 2,
+ step: 0.01,
+ default: undefined,
+ description: 'Top left',
+ },
+ bottomRight: {
+ type: 'uv-coordinate',
+ min: -1,
+ max: 2,
+ step: 0.01,
+ default: undefined,
+ description: 'Bottom right',
+ },
+ blurRadius: {
+ type: 'number',
+ min: 0,
+ max: 200,
+ step: 1,
+ default: DEFAULT_BLUR_RADIUS,
+ description: 'Blur radius',
+ hiddenFromList: false,
+ },
+ feather: {
+ type: 'number',
+ min: 0,
+ max: 200,
+ step: 1,
+ default: DEFAULT_FEATHER,
+ description: 'Feather',
+ hiddenFromList: false,
+ },
+ roundness: {
+ type: 'number',
+ min: 0,
+ max: 1,
+ step: 0.01,
+ default: DEFAULT_ROUNDNESS,
+ description: 'Roundness',
+ hiddenFromList: false,
+ },
+} as const satisfies InteractivitySchema;
+
+const resolve = (params: RegionBlurParams): RegionBlurResolved => ({
+ topLeft: [...params.topLeft] as RegionBlurUvCoordinate,
+ bottomRight: [...params.bottomRight] as RegionBlurUvCoordinate,
+ blurRadius: params.blurRadius ?? DEFAULT_BLUR_RADIUS,
+ feather: params.feather ?? DEFAULT_FEATHER,
+ roundness: params.roundness ?? DEFAULT_ROUNDNESS,
+});
+
+const assertRequiredUvCoordinate = (value: unknown, name: string): void => {
+ if (
+ !Array.isArray(value) ||
+ value.length !== 2 ||
+ value.some((item) => typeof item !== 'number' || !Number.isFinite(item))
+ ) {
+ throw new TypeError(`"${name}" must be a [number, number] tuple`);
+ }
+};
+
+const validateRegionBlurParams = (params: RegionBlurParams): void => {
+ assertEffectParamsObject(params, 'Region blur');
+ assertRequiredUvCoordinate(params.topLeft, 'topLeft');
+ assertRequiredUvCoordinate(params.bottomRight, 'bottomRight');
+ assertOptionalFiniteNumber(params.blurRadius, 'blurRadius');
+ assertOptionalFiniteNumber(params.feather, 'feather');
+ assertOptionalFiniteNumber(params.roundness, 'roundness');
+
+ const resolved = resolve(params);
+ validateNonNegative(resolved.blurRadius, 'blurRadius');
+ validateNonNegative(resolved.feather, 'feather');
+ validateUnitInterval(resolved.roundness, 'roundness');
+
+ if (
+ resolved.topLeft[0] >= resolved.bottomRight[0] ||
+ resolved.topLeft[1] >= resolved.bottomRight[1]
+ ) {
+ throw new TypeError(
+ '"topLeft" must be above and to the left of "bottomRight"',
+ );
+ }
+};
+
+export const regionBlur = createEffect<RegionBlurParams, RegionBlurState>({
+ type: 'remotion/region-blur',
+ label: 'regionBlur()',
+ documentationLink: 'https://www.remotion.dev/docs/effects/region-blur',
+ backend: 'webgl2',
+ calculateKey: (params) => {
+ const resolved = resolve(params);
+ return `region-blur-${resolved.topLeft.join(':')}-${resolved.bottomRight.join(':')}-${resolved.blurRadius}-${resolved.feather}-${resolved.roundness}`;
+ },
+ setup: (target) => setupRegionBlur(target),
+ apply: ({source, width, height, params, state, flipSourceY}) => {
+ const resolved = resolve(params);
+ applyRegionBlur({
+ state,
+ source,
+ width,
+ height,
+ flipSourceY,
+ ...resolved,
+ });
+ },
+ cleanup: (state) => cleanupRegionBlur(state),
+ schema: regionBlurSchema,
+ validateParams: validateRegionBlurParams,
+});
diff --git a/packages/effects/src/region-blur/region-blur-runtime.ts b/packages/effects/src/region-blur/region-blur-runtime.ts
new file mode 100644
index 0000000000..e246db3684
--- /dev/null
+++ b/packages/effects/src/region-blur/region-blur-runtime.ts
@@ -0,0 +1,394 @@
+import {Internals} from 'remotion';
+import {
+ BLUR_FS_HORIZONTAL,
+ BLUR_FS_VERTICAL,
+ BLUR_VS,
+} from '../blur/blur-shaders.js';
+import {publicUvToShaderUv} from '../uv-coordinate.js';
+import type {RegionBlurUvCoordinate} from './index.js';
+
+const {createWebGL2ContextError} = Internals;
+
+const COMPOSITE_FRAGMENT_SHADER = /* glsl */ `#version 300 es
+precision highp float;
+
+in vec2 vUv;
+out vec4 fragColor;
+
+uniform sampler2D uSource;
+uniform sampler2D uBlurred;
+uniform vec2 uResolution;
+uniform vec2 uTopLeft;
+uniform vec2 uBottomRight;
+uniform float uFeather;
+uniform float uRoundness;
+
+float roundedBoxDistance(vec2 point, vec2 center, vec2 halfSize, float radius) {
+ vec2 offset = abs(point - center) - halfSize + vec2(radius);
+ return length(max(offset, vec2(0.0))) + min(max(offset.x, offset.y), 0.0) - radius;
+}
+
+void main() {
+ vec2 minimum = vec2(uTopLeft.x, uBottomRight.y) * uResolution;
+ vec2 maximum = vec2(uBottomRight.x, uTopLeft.y) * uResolution;
+ vec2 halfSize = (maximum - minimum) * 0.5;
+ vec2 center = (minimum + maximum) * 0.5;
+ float cornerRadius = min(halfSize.x, halfSize.y) * uRoundness;
+ float distanceToRegion = roundedBoxDistance(
+ vUv * uResolution,
+ center,
+ halfSize,
+ cornerRadius
+ );
+ float mask = uFeather <= 0.0
+ ? (distanceToRegion <= 0.0 ? 1.0 : 0.0)
+ : 1.0 - smoothstep(-uFeather * 0.5, uFeather * 0.5, distanceToRegion);
+
+ fragColor = mix(texture(uSource, vUv), texture(uBlurred, vUv), mask);
+}
+`;
+
+type BlurUniforms = {
+ readonly source: WebGLUniformLocation | null;
+ readonly radius: WebGLUniformLocation | null;
+ readonly texelSize: WebGLUniformLocation | null;
+};
+
+export type RegionBlurState = {
+ readonly gl: WebGL2RenderingContext;
+ readonly horizontalProgram: WebGLProgram;
+ readonly verticalProgram: WebGLProgram;
+ readonly compositeProgram: WebGLProgram;
+ readonly vao: WebGLVertexArrayObject;
+ readonly vbo: WebGLBuffer;
+ readonly sourceTexture: WebGLTexture;
+ readonly horizontalTexture: WebGLTexture;
+ readonly blurredTexture: WebGLTexture;
+ readonly framebuffer: WebGLFramebuffer;
+ readonly horizontalUniforms: BlurUniforms;
+ readonly verticalUniforms: BlurUniforms;
+ readonly compositeUniforms: {
+ readonly source: WebGLUniformLocation | null;
+ readonly blurred: WebGLUniformLocation | null;
+ readonly resolution: WebGLUniformLocation | null;
+ readonly topLeft: WebGLUniformLocation | null;
+ readonly bottomRight: WebGLUniformLocation | null;
+ readonly feather: WebGLUniformLocation | null;
+ readonly roundness: WebGLUniformLocation | null;
+ };
+};
+
+const compileShader = (
+ gl: WebGL2RenderingContext,
+ type: number,
+ source: string,
+): WebGLShader => {
+ const shader = gl.createShader(type);
+ if (!shader) {
+ throw new Error('Failed to create WebGL shader');
+ }
+
+ gl.shaderSource(shader, source);
+ gl.compileShader(shader);
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
+ const log = gl.getShaderInfoLog(shader);
+ gl.deleteShader(shader);
+ throw new Error(`Shader compile failed: ${log ?? '(no log)'}`);
+ }
+
+ return shader;
+};
+
+const createProgram = (
+ gl: WebGL2RenderingContext,
+ vertexSource: string,
+ fragmentSource: string,
+): WebGLProgram => {
+ const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexSource);
+ const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
+ const program = gl.createProgram();
+ if (!program) {
+ throw new Error('Failed to create WebGL program');
+ }
+
+ gl.attachShader(program, vertexShader);
+ gl.attachShader(program, fragmentShader);
+ gl.linkProgram(program);
+ gl.deleteShader(vertexShader);
+ gl.deleteShader(fragmentShader);
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
+ const log = gl.getProgramInfoLog(program);
+ gl.deleteProgram(program);
+ throw new Error(`Program link failed: ${log ?? '(no log)'}`);
+ }
+
+ return program;
+};
+
+const createTexture = (gl: WebGL2RenderingContext): WebGLTexture => {
+ const texture = gl.createTexture();
+ if (!texture) {
+ throw new Error('Failed to create WebGL texture');
+ }
+
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ return texture;
+};
+
+const getBlurUniforms = (
+ gl: WebGL2RenderingContext,
+ program: WebGLProgram,
+): BlurUniforms => ({
+ source: gl.getUniformLocation(program, 'uSource'),
+ radius: gl.getUniformLocation(program, 'uRadius'),
+ texelSize: gl.getUniformLocation(program, 'uTexelSize'),
+});
+
+export const setupRegionBlur = (target: HTMLCanvasElement): RegionBlurState => {
+ const gl = target.getContext('webgl2', {
+ premultipliedAlpha: true,
+ alpha: true,
+ preserveDrawingBuffer: true,
+ });
+ if (!gl) {
+ throw createWebGL2ContextError('region blur effect');
+ }
+
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
+
+ const horizontalProgram = createProgram(gl, BLUR_VS, BLUR_FS_HORIZONTAL);
+ const verticalProgram = createProgram(gl, BLUR_VS, BLUR_FS_VERTICAL);
+ const compositeProgram = createProgram(
+ gl,
+ BLUR_VS,
+ COMPOSITE_FRAGMENT_SHADER,
+ );
+ const vao = gl.createVertexArray();
+ const vbo = gl.createBuffer();
+ const framebuffer = gl.createFramebuffer();
+ if (!vao || !vbo || !framebuffer) {
+ throw new Error('Failed to create region blur WebGL resources');
+ }
+
+ gl.bindVertexArray(vao);
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
+ gl.bufferData(
+ gl.ARRAY_BUFFER,
+ new Float32Array([-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1]),
+ gl.STATIC_DRAW,
+ );
+ gl.enableVertexAttribArray(0);
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
+ gl.enableVertexAttribArray(1);
+ gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 16, 8);
+ gl.bindVertexArray(null);
+
+ return {
+ gl,
+ horizontalProgram,
+ verticalProgram,
+ compositeProgram,
+ vao,
+ vbo,
+ sourceTexture: createTexture(gl),
+ horizontalTexture: createTexture(gl),
+ blurredTexture: createTexture(gl),
+ framebuffer,
+ horizontalUniforms: getBlurUniforms(gl, horizontalProgram),
+ verticalUniforms: getBlurUniforms(gl, verticalProgram),
+ compositeUniforms: {
+ source: gl.getUniformLocation(compositeProgram, 'uSource'),
+ blurred: gl.getUniformLocation(compositeProgram, 'uBlurred'),
+ resolution: gl.getUniformLocation(compositeProgram, 'uResolution'),
+ topLeft: gl.getUniformLocation(compositeProgram, 'uTopLeft'),
+ bottomRight: gl.getUniformLocation(compositeProgram, 'uBottomRight'),
+ feather: gl.getUniformLocation(compositeProgram, 'uFeather'),
+ roundness: gl.getUniformLocation(compositeProgram, 'uRoundness'),
+ },
+ };
+};
+
+const draw = (state: RegionBlurState): void => {
+ state.gl.bindVertexArray(state.vao);
+ state.gl.drawArrays(state.gl.TRIANGLE_STRIP, 0, 4);
+ state.gl.bindVertexArray(null);
+};
+
+const allocateTexture = (
+ gl: WebGL2RenderingContext,
+ texture: WebGLTexture,
+ width: number,
+ height: number,
+): void => {
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ width,
+ height,
+ 0,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ null,
+ );
+};
+
+const renderBlurPass = ({
+ state,
+ program,
+ uniforms,
+ input,
+ output,
+ width,
+ height,
+ radius,
+}: {
+ readonly state: RegionBlurState;
+ readonly program: WebGLProgram;
+ readonly uniforms: BlurUniforms;
+ readonly input: WebGLTexture;
+ readonly output: WebGLTexture;
+ readonly width: number;
+ readonly height: number;
+ readonly radius: number;
+}): void => {
+ const {gl} = state;
+ gl.bindFramebuffer(gl.FRAMEBUFFER, state.framebuffer);
+ gl.framebufferTexture2D(
+ gl.FRAMEBUFFER,
+ gl.COLOR_ATTACHMENT0,
+ gl.TEXTURE_2D,
+ output,
+ 0,
+ );
+ if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
+ throw new Error('Region blur framebuffer is incomplete');
+ }
+
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, input);
+ gl.useProgram(program);
+ if (uniforms.source) gl.uniform1i(uniforms.source, 0);
+ if (uniforms.radius) gl.uniform1f(uniforms.radius, radius);
+ if (uniforms.texelSize) {
+ gl.uniform2f(uniforms.texelSize, 1 / width, 1 / height);
+ }
+
+ draw(state);
+};
+
+export const applyRegionBlur = ({
+ state,
+ source,
+ width,
+ height,
+ flipSourceY,
+ topLeft,
+ bottomRight,
+ blurRadius,
+ feather,
+ roundness,
+}: {
+ readonly state: RegionBlurState;
+ readonly source: CanvasImageSource;
+ readonly width: number;
+ readonly height: number;
+ readonly flipSourceY: boolean;
+ readonly topLeft: RegionBlurUvCoordinate;
+ readonly bottomRight: RegionBlurUvCoordinate;
+ readonly blurRadius: number;
+ readonly feather: number;
+ readonly roundness: number;
+}): void => {
+ const {gl} = state;
+ gl.viewport(0, 0, width, height);
+ gl.clearColor(0, 0, 0, 0);
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
+ gl.bindTexture(gl.TEXTURE_2D, state.sourceTexture);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ source as TexImageSource,
+ );
+ allocateTexture(gl, state.horizontalTexture, width, height);
+ allocateTexture(gl, state.blurredTexture, width, height);
+
+ renderBlurPass({
+ state,
+ program: state.horizontalProgram,
+ uniforms: state.horizontalUniforms,
+ input: state.sourceTexture,
+ output: state.horizontalTexture,
+ width,
+ height,
+ radius: blurRadius,
+ });
+ renderBlurPass({
+ state,
+ program: state.verticalProgram,
+ uniforms: state.verticalUniforms,
+ input: state.horizontalTexture,
+ output: state.blurredTexture,
+ width,
+ height,
+ radius: blurRadius,
+ });
+
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.useProgram(state.compositeProgram);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, state.sourceTexture);
+ gl.activeTexture(gl.TEXTURE1);
+ gl.bindTexture(gl.TEXTURE_2D, state.blurredTexture);
+ const topLeftShader = publicUvToShaderUv(topLeft);
+ const bottomRightShader = publicUvToShaderUv(bottomRight);
+ const uniforms = state.compositeUniforms;
+ if (uniforms.source) gl.uniform1i(uniforms.source, 0);
+ if (uniforms.blurred) gl.uniform1i(uniforms.blurred, 1);
+ if (uniforms.resolution) gl.uniform2f(uniforms.resolution, width, height);
+ if (uniforms.topLeft) {
+ gl.uniform2f(uniforms.topLeft, topLeftShader[0], topLeftShader[1]);
+ }
+
+ if (uniforms.bottomRight) {
+ gl.uniform2f(
+ uniforms.bottomRight,
+ bottomRightShader[0],
+ bottomRightShader[1],
+ );
+ }
+
+ if (uniforms.feather) gl.uniform1f(uniforms.feather, feather);
+ if (uniforms.roundness) gl.uniform1f(uniforms.roundness, roundness);
+ draw(state);
+
+ gl.activeTexture(gl.TEXTURE1);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.useProgram(null);
+};
+
+export const cleanupRegionBlur = (state: RegionBlurState): void => {
+ const {gl} = state;
+ gl.deleteFramebuffer(state.framebuffer);
+ gl.deleteTexture(state.sourceTexture);
+ gl.deleteTexture(state.horizontalTexture);
+ gl.deleteTexture(state.blurredTexture);
+ gl.deleteBuffer(state.vbo);
+ gl.deleteVertexArray(state.vao);
+ gl.deleteProgram(state.horizontalProgram);
+ gl.deleteProgram(state.verticalProgram);
+ gl.deleteProgram(state.compositeProgram);
+};
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 2c461ea4d6..c799499ca7 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -43,6 +43,11 @@ import {pixelDissolve} from '../pixel-dissolve.js';
import {pixelate} from '../pixelate.js';
import {radialProgressiveBlur} from '../radial-progressive-blur/index.js';
import {radialProgressivePixelate} from '../radial-progressive-pixelate/index.js';
+import {
+ regionBlur,
+ regionBlurSchema,
+ type RegionBlurParams,
+} from '../region-blur/index.js';
import {rings} from '../rings.js';
import {roughenEdges} from '../roughen-edges.js';
import {saturation} from '../saturation.js';
@@ -209,6 +214,10 @@ test('@remotion/effects expose documentation links', () => {
expect(radialProgressiveBlur().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/radial-progressive-blur',
);
+ expect(
+ regionBlur({topLeft: [0.25, 0.25], bottomRight: [0.75, 0.75]}).definition
+ .documentationLink,
+ ).toBe('https://www.remotion.dev/docs/effects/region-blur');
expect(radialProgressivePixelate().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/radial-progressive-pixelate',
);
@@ -3075,6 +3084,85 @@ test('linearProgressiveBlur() parameters produce distinct effect keys', () => {
).toBe(5);
});
+test('regionBlur() accepts required params and defaults', () => {
+ const effect = regionBlur({
+ topLeft: [0.2, 0.3],
+ bottomRight: [0.8, 0.9],
+ });
+
+ expect(effect).toBeDefined();
+ expect(regionBlurSchema.blurRadius.default).toBe(40);
+ expect(regionBlurSchema.feather.default).toBe(0);
+ expect(regionBlurSchema.roundness.default).toBe(0);
+});
+
+test('regionBlur() rejects missing coordinates', () => {
+ expect(() =>
+ regionBlur({bottomRight: [0.8, 0.9]} as unknown as RegionBlurParams),
+ ).toThrow('"topLeft" must be a [number, number] tuple');
+ expect(() =>
+ regionBlur({topLeft: [0.2, 0.3]} as unknown as RegionBlurParams),
+ ).toThrow('"bottomRight" must be a [number, number] tuple');
+});
+
+test('regionBlur() rejects invalid coordinates', () => {
+ expect(() =>
+ regionBlur({
+ topLeft: [0.2, Number.NaN],
+ bottomRight: [0.8, 0.9],
+ }),
+ ).toThrow('"topLeft" must be a [number, number] tuple');
+ expect(() =>
+ regionBlur({topLeft: [0.8, 0.3], bottomRight: [0.2, 0.9]}),
+ ).toThrow('"topLeft" must be above and to the left of "bottomRight"');
+});
+
+test('regionBlur() accepts coordinates outside the canvas', () => {
+ expect(() =>
+ regionBlur({topLeft: [-0.2, -0.1], bottomRight: [1.2, 1.1]}),
+ ).not.toThrow();
+});
+
+test('regionBlur() rejects invalid styling params', () => {
+ expect(() =>
+ regionBlur({
+ topLeft: [0.2, 0.3],
+ bottomRight: [0.8, 0.9],
+ blurRadius: -1,
+ }),
+ ).toThrow('"blurRadius" must be >= 0');
+ expect(() =>
+ regionBlur({
+ topLeft: [0.2, 0.3],
+ bottomRight: [0.8, 0.9],
+ feather: Number.NaN,
+ }),
+ ).toThrow('"feather" must be a finite number');
+ expect(() =>
+ regionBlur({
+ topLeft: [0.2, 0.3],
+ bottomRight: [0.8, 0.9],
+ roundness: 1.1,
+ }),
+ ).toThrow('"roundness" must be <= 1');
+});
+
+test('regionBlur() parameters produce distinct effect keys', () => {
+ const params = {topLeft: [0.2, 0.3], bottomRight: [0.8, 0.9]} as const;
+ const effects = [
+ regionBlur(params),
+ regionBlur({...params, topLeft: [0.1, 0.3]}),
+ regionBlur({...params, bottomRight: [0.9, 0.9]}),
+ regionBlur({...params, blurRadius: 80}),
+ regionBlur({...params, feather: 12}),
+ regionBlur({...params, roundness: 1}),
+ ];
+
+ expect(new Set(effects.map((effect) => effect.effectKey)).size).toBe(
+ effects.length,
+ );
+});
+
test('radialProgressiveBlur() accepts default params', () => {
expect(() => radialProgressiveBlur()).not.toThrow();
});
diff --git a/packages/studio-shared/src/effect-catalog.ts b/packages/studio-shared/src/effect-catalog.ts
index 040d85fd00..3e9e446bea 100644
--- a/packages/studio-shared/src/effect-catalog.ts
+++ b/packages/studio-shared/src/effect-catalog.ts
@@ -232,6 +232,20 @@ export const EFFECT_CATALOG: readonly EffectCatalogItem[] = [
config: {},
},
},
+ {
+ id: 'effects-region-blur',
+ category: 'Blur & Shadow',
+ label: 'regionBlur()',
+ description: 'Blur a rectangular or rounded region',
+ effect: {
+ name: 'regionBlur',
+ importPath: '@remotion/effects/region-blur',
+ config: {
+ topLeft: [0.28, 0.18],
+ bottomRight: [0.72, 0.82],
+ },
+ },
+ },
{
id: 'effects-zoom-blur',
category: 'Blur & Shadow',