Skip to content

🎥 Make videos programmatically with React

`@remotion/effects`: Add linearProgressiveBlur effect (#8048)

Jonny Burger22912c1af3b0
diff --git a/packages/docs/components/effects-demos/registry.ts b/packages/docs/components/effects-demos/registry.ts
index 38f419d4ca..5669e11225 100644
--- a/packages/docs/components/effects-demos/registry.ts
+++ b/packages/docs/components/effects-demos/registry.ts
@@ -15,6 +15,7 @@ import {halftone} from '@remotion/effects/halftone';
import {halftoneLinearGradient} from '@remotion/effects/halftone-linear-gradient';
import {hue} from '@remotion/effects/hue';
import {invert} from '@remotion/effects/invert';
+import {linearProgressiveBlur} from '@remotion/effects/linear-progressive-blur';
import {lines} from '@remotion/effects/lines';
import {mirror} from '@remotion/effects/mirror';
import {noise} from '@remotion/effects/noise';
@@ -52,6 +53,7 @@ import {EffectsHalftonePreview} from '../effects/effects-halftone-preview';
import {EffectsHuePreview} from '../effects/effects-hue-preview';
import {EffectsInvertPreview} from '../effects/effects-invert-preview';
import {EffectsLightLeakPreview} from '../effects/effects-light-leak-preview';
+import {EffectsLinearProgressiveBlurPreview} from '../effects/effects-linear-progressive-blur-preview';
import {EffectsLinesPreview} from '../effects/effects-lines-preview';
import {EffectsMirrorPreview} from '../effects/effects-mirror-preview';
import {EffectsNoisePreview} from '../effects/effects-noise-preview';
@@ -313,6 +315,14 @@ export const effectsDemos: EffectsDemoType[] = [
radius: 40,
},
},
+ {
+ ...defaults,
+ id: 'effects-linear-progressive-blur',
+ effectName: 'linearProgressiveBlur',
+ effectImportPath: '@remotion/effects/linear-progressive-blur',
+ comp: EffectsLinearProgressiveBlurPreview,
+ schema: linearProgressiveBlur().definition.schema,
+ },
{
...defaults,
id: 'effects-chromatic-aberration',
diff --git a/packages/docs/components/effects/effects-linear-progressive-blur-preview.tsx b/packages/docs/components/effects/effects-linear-progressive-blur-preview.tsx
new file mode 100644
index 0000000000..4fe233c68c
--- /dev/null
+++ b/packages/docs/components/effects/effects-linear-progressive-blur-preview.tsx
@@ -0,0 +1,29 @@
+import type {LinearProgressiveBlurUvCoordinate} from '@remotion/effects/linear-progressive-blur';
+import {linearProgressiveBlur} from '@remotion/effects/linear-progressive-blur';
+import React from 'react';
+import {CanvasImage} from 'remotion';
+import {EFFECTS_PREVIEW_IMAGE_SRC} from './effects-preview-image';
+
+export const EffectsLinearProgressiveBlurPreview: React.FC<{
+ readonly start: LinearProgressiveBlurUvCoordinate;
+ readonly end: LinearProgressiveBlurUvCoordinate;
+ readonly startBlur: number;
+ readonly endBlur: number;
+}> = ({start, end, startBlur, endBlur}) => {
+ return (
+ <CanvasImage
+ src={EFFECTS_PREVIEW_IMAGE_SRC}
+ width={1280}
+ height={720}
+ fit="cover"
+ effects={[
+ linearProgressiveBlur({
+ start,
+ end,
+ startBlur,
+ endBlur,
+ }),
+ ]}
+ />
+ );
+};
diff --git a/packages/docs/docs/effects/linear-progressive-blur.mdx b/packages/docs/docs/effects/linear-progressive-blur.mdx
new file mode 100644
index 0000000000..2a02ffe15c
--- /dev/null
+++ b/packages/docs/docs/effects/linear-progressive-blur.mdx
@@ -0,0 +1,96 @@
+---
+image: /generated/articles-docs-effects-linear-progressive-blur.png
+slug: /effects/linear-progressive-blur
+title: linearProgressiveBlur()
+sidebar_label: linearProgressiveBlur()
+crumb: '@remotion/effects'
+---
+
+# linearProgressiveBlur()<AvailableFrom v="4.0.472" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+A Gaussian blur whose radius changes across a two-stop linear gradient.
+
+Use it for depth-of-field, focus pulls, and directional softening on canvas-based components such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) and [`<Solid>`](/docs/solid).
+
+## Preview
+
+<EffectsDemo type="effects-linear-progressive-blur" />
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill} from 'remotion';
+import {Video} from '@remotion/media';
+import {linearProgressiveBlur} from '@remotion/effects/linear-progressive-blur';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Video
+ src="https://remotion.media/video.mp4"
+ effects={[
+ linearProgressiveBlur({
+ start: [0, 0.5],
+ end: [1, 0.5],
+ startBlur: 0,
+ endBlur: 50,
+ }),
+ ]}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+## API
+
+Pass an object with the following properties. You can also call `linearProgressiveBlur()` without arguments.
+
+### `start?`
+
+UV coordinate where `startBlur` is reached. Defaults to `[0, 0.5]`.
+
+Pass a `[number, number]` tuple where `[0, 0]` is top-left and `[1, 1]` is bottom-right.
+
+The accepted range in the visual editor is `[-1, 2]` for each coordinate. Values outside `[0, 1]` place the stop outside the visible area.
+
+### `end?`
+
+UV coordinate where `endBlur` is reached. Defaults to `[1, 0.5]`.
+
+Pass a `[number, number]` tuple.
+
+### `startBlur?`
+
+Blur radius in pixels at `start`. Defaults to `0`.
+
+Negative values are clamped to `0`.
+
+### `endBlur?`
+
+Blur radius in pixels at `end`. Defaults to `50`.
+
+Negative values are clamped to `0`.
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`.
+
+## Behavior
+
+The blur radius is interpolated between `startBlur` and `endBlur` along the `start` to `end` line.
+
+Before `start`, the radius is clamped to `startBlur`. After `end`, the radius is clamped to `endBlur`.
+
+## Requirements
+
+`linearProgressiveBlur()` uses a WebGL2 backend. During renders, enable WebGL via [`Config.setChromiumOpenGlRenderer('angle')`](/docs/config#setchromiumopenglrenderer). See [Using WebGL during renders](/docs/remotion/html-in-canvas#using-webgl-during-renders).
+
+## See also
+
+- [`@remotion/effects`](/docs/effects/api)
+- [`blur()`](/docs/effects/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/linear-progressive-blur/index.ts)
diff --git a/packages/docs/docs/effects/table-of-contents.tsx b/packages/docs/docs/effects/table-of-contents.tsx
index 66c77825a9..fc409378f2 100644
--- a/packages/docs/docs/effects/table-of-contents.tsx
+++ b/packages/docs/docs/effects/table-of-contents.tsx
@@ -98,6 +98,13 @@ const categories: {
name: 'blur()',
description: 'Gaussian blur effect',
},
+ {
+ link: '/docs/effects/linear-progressive-blur',
+ preview: '/img/effects-linear-progressive-blur-preview.png',
+ alt: 'linear progressive blur effect preview',
+ name: 'linearProgressiveBlur()',
+ description: 'Gradient-controlled blur effect',
+ },
{
link: '/docs/effects/drop-shadow',
preview: '/img/effects-drop-shadow-preview.png',
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index c4d8a32914..7b4ae27383 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -275,6 +275,7 @@ const sidebars: SidebarsConfig = {
'effects/chromatic-aberration',
'effects/color-key',
'effects/contrast',
+ 'effects/dot-grid',
'effects/drop-shadow',
'effects/duotone',
'effects/evolve',
@@ -283,13 +284,13 @@ const sidebars: SidebarsConfig = {
'effects/grayscale',
'effects/halftone',
'effects/halftone-linear-gradient',
- 'effects/pixel-dissolve',
'effects/hue',
- 'effects/dot-grid',
'effects/invert',
+ 'effects/linear-progressive-blur',
'effects/lines',
'effects/mirror',
'effects/noise',
+ 'effects/pixel-dissolve',
'effects/rings',
'effects/saturation',
'effects/scale',
@@ -301,9 +302,9 @@ const sidebars: SidebarsConfig = {
'effects/vignette',
'effects/wave',
'effects/waves',
- 'effects/zigzag',
'effects/white-noise',
'effects/xy-translate',
+ 'effects/zigzag',
],
},
{
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 13d5a3c086..26c4422c9a 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1889,6 +1889,15 @@ export const articles = [
noAi: false,
slug: 'effects/invert',
},
+ {
+ id: 'effects/linear-progressive-blur',
+ title: 'linearProgressiveBlur()',
+ relativePath: 'docs/effects/linear-progressive-blur.mdx',
+ compId: 'articles-docs-effects-linear-progressive-blur',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/linear-progressive-blur',
+ },
{
id: 'effects/lines',
title: 'lines()',
diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsx
index 0c7769d4a3..9902a71062 100644
--- a/packages/docs/src/remotion/Root.tsx
+++ b/packages/docs/src/remotion/Root.tsx
@@ -20,6 +20,7 @@ import {EffectsHalftonePreview} from '../../components/effects/effects-halftone-
import {EffectsHuePreview} from '../../components/effects/effects-hue-preview';
import {EffectsInvertPreview} from '../../components/effects/effects-invert-preview';
import {EffectsLightLeakPreview} from '../../components/effects/effects-light-leak-preview';
+import {EffectsLinearProgressiveBlurPreview} from '../../components/effects/effects-linear-progressive-blur-preview';
import {EffectsLinesPreview} from '../../components/effects/effects-lines-preview';
import {EffectsMirrorPreview} from '../../components/effects/effects-mirror-preview';
import {EffectsNoisePreview} from '../../components/effects/effects-noise-preview';
@@ -282,6 +283,18 @@ export const RemotionRoot: React.FC = () => {
height={720}
defaultProps={{radius: 40, horizontal: true, vertical: true}}
/>
+ <Still
+ id="effects-linear-progressive-blur-preview"
+ component={EffectsLinearProgressiveBlurPreview}
+ width={1280}
+ height={720}
+ defaultProps={{
+ start: [0, 0.5],
+ end: [1, 0.5],
+ startBlur: 0,
+ endBlur: 50,
+ }}
+ />
<Still
id="effects-wave-preview"
component={EffectsWavePreview}
diff --git a/packages/docs/static/generated/articles-docs-effects-linear-progressive-blur.png b/packages/docs/static/generated/articles-docs-effects-linear-progressive-blur.png
new file mode 100644
index 0000000000..d1c6c4198e
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-linear-progressive-blur.png differ
diff --git a/packages/docs/static/img/effects-linear-progressive-blur-preview.png b/packages/docs/static/img/effects-linear-progressive-blur-preview.png
new file mode 100644
index 0000000000..35f7e73a4a
Binary files /dev/null and b/packages/docs/static/img/effects-linear-progressive-blur-preview.png differ
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 8f5b08e070..294d879e02 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -26,6 +26,7 @@ const effectEntrypoints = [
'src/pixel-dissolve.ts',
'src/invert.ts',
'src/lines.ts',
+ 'src/linear-progressive-blur.ts',
'src/mirror.ts',
'src/noise.ts',
'src/rings.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index 4ed485892f..5737095631 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -124,6 +124,11 @@
"module": "./dist/esm/lines.mjs",
"import": "./dist/esm/lines.mjs"
},
+ "./linear-progressive-blur": {
+ "types": "./dist/linear-progressive-blur.d.ts",
+ "module": "./dist/esm/linear-progressive-blur.mjs",
+ "import": "./dist/esm/linear-progressive-blur.mjs"
+ },
"./dot-grid": {
"types": "./dist/dot-grid.d.ts",
"module": "./dist/esm/dot-grid.mjs",
@@ -262,6 +267,9 @@
"lines": [
"dist/lines.d.ts"
],
+ "linear-progressive-blur": [
+ "dist/linear-progressive-blur.d.ts"
+ ],
"dot-grid": [
"dist/dot-grid.d.ts"
],
diff --git a/packages/effects/src/linear-progressive-blur.ts b/packages/effects/src/linear-progressive-blur.ts
new file mode 100644
index 0000000000..4bb47a6b2d
--- /dev/null
+++ b/packages/effects/src/linear-progressive-blur.ts
@@ -0,0 +1,5 @@
+export {
+ linearProgressiveBlur,
+ type LinearProgressiveBlurParams,
+ type LinearProgressiveBlurUvCoordinate,
+} from './linear-progressive-blur/index.js';
diff --git a/packages/effects/src/linear-progressive-blur/index.ts b/packages/effects/src/linear-progressive-blur/index.ts
new file mode 100644
index 0000000000..c2d1235ad6
--- /dev/null
+++ b/packages/effects/src/linear-progressive-blur/index.ts
@@ -0,0 +1,149 @@
+import type {SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import {assertOptionalFiniteNumber} from '../color-utils.js';
+import {assertEffectParamsObject} from '../validate-effect-param.js';
+import {
+ applyLinearProgressiveBlur,
+ cleanupLinearProgressiveBlur,
+ setupLinearProgressiveBlur,
+ type LinearProgressiveBlurState,
+} from './linear-progressive-blur-runtime.js';
+
+const {createEffect} = Internals;
+
+const DEFAULT_START = [0, 0.5] as const;
+const DEFAULT_END = [1, 0.5] as const;
+const DEFAULT_START_BLUR = 0;
+const DEFAULT_END_BLUR = 50;
+
+export type LinearProgressiveBlurUvCoordinate = readonly [number, number];
+
+export type LinearProgressiveBlurParams = {
+ /**
+ * UV coordinate where `startBlur` is reached. Defaults to `[0, 0.5]`.
+ */
+ readonly start?: LinearProgressiveBlurUvCoordinate;
+ /**
+ * UV coordinate where `endBlur` is reached. Defaults to `[1, 0.5]`.
+ */
+ readonly end?: LinearProgressiveBlurUvCoordinate;
+ /**
+ * Blur radius in pixels at `start`. Defaults to `0`.
+ */
+ readonly startBlur?: number;
+ /**
+ * Blur radius in pixels at `end`. Defaults to `50`.
+ */
+ readonly endBlur?: number;
+};
+
+export type LinearProgressiveBlurResolved = {
+ readonly start: LinearProgressiveBlurUvCoordinate;
+ readonly end: LinearProgressiveBlurUvCoordinate;
+ readonly startBlur: number;
+ readonly endBlur: number;
+};
+
+const linearProgressiveBlurSchema = {
+ start: {
+ type: 'uv-coordinate',
+ min: -1,
+ max: 2,
+ step: 0.01,
+ default: DEFAULT_START,
+ description: 'Start',
+ },
+ end: {
+ type: 'uv-coordinate',
+ min: -1,
+ max: 2,
+ step: 0.01,
+ default: DEFAULT_END,
+ description: 'End',
+ },
+ startBlur: {
+ type: 'number',
+ min: 0,
+ max: 200,
+ step: 1,
+ default: DEFAULT_START_BLUR,
+ description: 'Start blur',
+ hiddenFromList: false,
+ },
+ endBlur: {
+ type: 'number',
+ min: 0,
+ max: 200,
+ step: 1,
+ default: DEFAULT_END_BLUR,
+ description: 'End blur',
+ hiddenFromList: false,
+ },
+} as const satisfies SequenceSchema;
+
+const clampBlur = (value: number): number => Math.max(0, value);
+
+const resolveLinearProgressiveBlurParams = (
+ params: LinearProgressiveBlurParams,
+): LinearProgressiveBlurResolved => ({
+ start: [
+ ...(params.start ?? DEFAULT_START),
+ ] as LinearProgressiveBlurUvCoordinate,
+ end: [...(params.end ?? DEFAULT_END)] as LinearProgressiveBlurUvCoordinate,
+ startBlur: clampBlur(params.startBlur ?? DEFAULT_START_BLUR),
+ endBlur: clampBlur(params.endBlur ?? DEFAULT_END_BLUR),
+});
+
+const assertOptionalUvCoordinate = (value: unknown, name: string): void => {
+ if (value === undefined) {
+ return;
+ }
+
+ 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 validateLinearProgressiveBlurParams = (
+ params: LinearProgressiveBlurParams,
+): void => {
+ assertEffectParamsObject(params, 'Linear progressive blur');
+ assertOptionalUvCoordinate(params.start, 'start');
+ assertOptionalUvCoordinate(params.end, 'end');
+ assertOptionalFiniteNumber(params.startBlur, 'startBlur');
+ assertOptionalFiniteNumber(params.endBlur, 'endBlur');
+};
+
+export const linearProgressiveBlur = createEffect<
+ LinearProgressiveBlurParams,
+ LinearProgressiveBlurState
+>({
+ type: 'remotion/linear-progressive-blur',
+ label: 'linearProgressiveBlur()',
+ documentationLink:
+ 'https://www.remotion.dev/docs/effects/linear-progressive-blur',
+ backend: 'webgl2',
+ calculateKey: (params) => {
+ const r = resolveLinearProgressiveBlurParams(params);
+ return `linear-progressive-blur-${r.start.join(':')}-${r.end.join(':')}-${r.startBlur}-${r.endBlur}`;
+ },
+ setup: (target) => setupLinearProgressiveBlur(target),
+ apply: ({source, width, height, params, state, flipSourceY}) => {
+ const r = resolveLinearProgressiveBlurParams(params);
+ applyLinearProgressiveBlur({
+ state,
+ source,
+ width,
+ height,
+ params: r,
+ flipSourceY,
+ });
+ },
+ cleanup: (state) => cleanupLinearProgressiveBlur(state),
+ schema: linearProgressiveBlurSchema,
+ validateParams: validateLinearProgressiveBlurParams,
+});
diff --git a/packages/effects/src/linear-progressive-blur/linear-progressive-blur-runtime.ts b/packages/effects/src/linear-progressive-blur/linear-progressive-blur-runtime.ts
new file mode 100644
index 0000000000..b446e8faa8
--- /dev/null
+++ b/packages/effects/src/linear-progressive-blur/linear-progressive-blur-runtime.ts
@@ -0,0 +1,386 @@
+import {Internals} from 'remotion';
+import type {LinearProgressiveBlurResolved} from './index.js';
+import {
+ buildLinearProgressiveBlurFs,
+ LINEAR_PROGRESSIVE_BLUR_VS,
+} from './linear-progressive-blur-shaders.js';
+
+const {createWebGL2ContextError} = Internals;
+
+type LinearProgressiveBlurUniforms = {
+ readonly uSource: WebGLUniformLocation | null;
+ readonly uTexelSize: WebGLUniformLocation | null;
+ readonly uStart: WebGLUniformLocation | null;
+ readonly uEnd: WebGLUniformLocation | null;
+ readonly uStartBlur: WebGLUniformLocation | null;
+ readonly uEndBlur: WebGLUniformLocation | null;
+};
+
+export type LinearProgressiveBlurState = {
+ readonly gl: WebGL2RenderingContext;
+ readonly programHorizontal: WebGLProgram;
+ readonly programVertical: WebGLProgram;
+ readonly vao: WebGLVertexArrayObject;
+ readonly vbo: WebGLBuffer;
+ readonly textureSource: WebGLTexture;
+ readonly textureIntermediate: WebGLTexture;
+ readonly framebuffer: WebGLFramebuffer;
+ readonly horizontal: LinearProgressiveBlurUniforms;
+ readonly vertical: LinearProgressiveBlurUniforms;
+};
+
+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(
+ `Linear progressive blur shader compile failed: ${log ?? '(no log)'}`,
+ );
+ }
+
+ return shader;
+};
+
+const linkProgram = (
+ gl: WebGL2RenderingContext,
+ vs: WebGLShader,
+ fs: WebGLShader,
+): WebGLProgram => {
+ const program = gl.createProgram();
+ if (!program) {
+ throw new Error('Failed to create WebGL program');
+ }
+
+ gl.attachShader(program, vs);
+ gl.attachShader(program, fs);
+ gl.linkProgram(program);
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
+ const log = gl.getProgramInfoLog(program);
+ gl.deleteProgram(program);
+ throw new Error(
+ `Linear progressive blur program link failed: ${log ?? '(no log)'}`,
+ );
+ }
+
+ return program;
+};
+
+const createProgram = (
+ gl: WebGL2RenderingContext,
+ vertexSource: string,
+ fragmentSource: string,
+): WebGLProgram => {
+ const vs = compileShader(gl, gl.VERTEX_SHADER, vertexSource);
+ const fs = compileShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
+ const program = linkProgram(gl, vs, fs);
+ gl.deleteShader(vs);
+ gl.deleteShader(fs);
+ return program;
+};
+
+const getUniforms = (
+ gl: WebGL2RenderingContext,
+ program: WebGLProgram,
+): LinearProgressiveBlurUniforms => ({
+ uSource: gl.getUniformLocation(program, 'uSource'),
+ uTexelSize: gl.getUniformLocation(program, 'uTexelSize'),
+ uStart: gl.getUniformLocation(program, 'uStart'),
+ uEnd: gl.getUniformLocation(program, 'uEnd'),
+ uStartBlur: gl.getUniformLocation(program, 'uStartBlur'),
+ uEndBlur: gl.getUniformLocation(program, 'uEndBlur'),
+});
+
+const createRgbaTexture = (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;
+};
+
+export const setupLinearProgressiveBlur = (
+ target: HTMLCanvasElement,
+): LinearProgressiveBlurState => {
+ const gl = target.getContext('webgl2', {
+ premultipliedAlpha: true,
+ alpha: true,
+ preserveDrawingBuffer: true,
+ });
+ if (!gl) {
+ throw createWebGL2ContextError('linear progressive blur effect');
+ }
+
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
+
+ const programHorizontal = createProgram(
+ gl,
+ LINEAR_PROGRESSIVE_BLUR_VS,
+ buildLinearProgressiveBlurFs('horizontal'),
+ );
+ const programVertical = createProgram(
+ gl,
+ LINEAR_PROGRESSIVE_BLUR_VS,
+ buildLinearProgressiveBlurFs('vertical'),
+ );
+
+ const vao = gl.createVertexArray();
+ if (!vao) {
+ throw new Error('Failed to create WebGL vertex array');
+ }
+
+ gl.bindVertexArray(vao);
+
+ const data = new Float32Array([
+ -1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1,
+ ]);
+
+ const vbo = gl.createBuffer();
+ if (!vbo) {
+ throw new Error('Failed to create WebGL buffer');
+ }
+
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
+ gl.bufferData(gl.ARRAY_BUFFER, data, 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);
+
+ const textureSource = createRgbaTexture(gl);
+ const textureIntermediate = createRgbaTexture(gl);
+
+ const framebuffer = gl.createFramebuffer();
+ if (!framebuffer) {
+ throw new Error('Failed to create WebGL framebuffer');
+ }
+
+ const w = Math.max(1, target.width);
+ const h = Math.max(1, target.height);
+ gl.bindTexture(gl.TEXTURE_2D, textureIntermediate);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ w,
+ h,
+ 0,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ null,
+ );
+ gl.bindTexture(gl.TEXTURE_2D, null);
+
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.framebufferTexture2D(
+ gl.FRAMEBUFFER,
+ gl.COLOR_ATTACHMENT0,
+ gl.TEXTURE_2D,
+ textureIntermediate,
+ 0,
+ );
+ const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ if (status !== gl.FRAMEBUFFER_COMPLETE) {
+ throw new Error(
+ `Linear progressive blur framebuffer incomplete: 0x${status.toString(16)}`,
+ );
+ }
+
+ return {
+ gl,
+ programHorizontal,
+ programVertical,
+ vao,
+ vbo,
+ textureSource,
+ textureIntermediate,
+ framebuffer,
+ horizontal: getUniforms(gl, programHorizontal),
+ vertical: getUniforms(gl, programVertical),
+ };
+};
+
+export const cleanupLinearProgressiveBlur = (
+ state: LinearProgressiveBlurState,
+): void => {
+ const {
+ gl,
+ programHorizontal,
+ programVertical,
+ vao,
+ vbo,
+ textureSource,
+ textureIntermediate,
+ framebuffer,
+ } = state;
+ gl.deleteFramebuffer(framebuffer);
+ gl.deleteTexture(textureSource);
+ gl.deleteTexture(textureIntermediate);
+ gl.deleteBuffer(vbo);
+ gl.deleteProgram(programHorizontal);
+ gl.deleteProgram(programVertical);
+ gl.deleteVertexArray(vao);
+};
+
+const uploadSource = ({
+ gl,
+ textureSource,
+ textureIntermediate,
+ source,
+ width,
+ height,
+ flipSourceY,
+}: {
+ readonly gl: WebGL2RenderingContext;
+ readonly textureSource: WebGLTexture;
+ readonly textureIntermediate: WebGLTexture;
+ readonly source: CanvasImageSource;
+ readonly width: number;
+ readonly height: number;
+ readonly flipSourceY: boolean;
+}): void => {
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
+ gl.bindTexture(gl.TEXTURE_2D, textureSource);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ source as TexImageSource,
+ );
+
+ gl.bindTexture(gl.TEXTURE_2D, textureIntermediate);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ width,
+ height,
+ 0,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ null,
+ );
+ gl.bindTexture(gl.TEXTURE_2D, null);
+};
+
+const setUniforms = ({
+ gl,
+ uniforms,
+ width,
+ height,
+ params,
+}: {
+ readonly gl: WebGL2RenderingContext;
+ readonly uniforms: LinearProgressiveBlurUniforms;
+ readonly width: number;
+ readonly height: number;
+ readonly params: LinearProgressiveBlurResolved;
+}): void => {
+ if (uniforms.uSource) gl.uniform1i(uniforms.uSource, 0);
+ if (uniforms.uTexelSize)
+ gl.uniform2f(uniforms.uTexelSize, 1 / width, 1 / height);
+ if (uniforms.uStart)
+ gl.uniform2f(uniforms.uStart, params.start[0], params.start[1]);
+ if (uniforms.uEnd) gl.uniform2f(uniforms.uEnd, params.end[0], params.end[1]);
+ if (uniforms.uStartBlur) gl.uniform1f(uniforms.uStartBlur, params.startBlur);
+ if (uniforms.uEndBlur) gl.uniform1f(uniforms.uEndBlur, params.endBlur);
+};
+
+const drawFullscreenQuad = (state: LinearProgressiveBlurState): void => {
+ const {gl, vao} = state;
+ gl.bindVertexArray(vao);
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+ gl.bindVertexArray(null);
+};
+
+export const applyLinearProgressiveBlur = ({
+ state,
+ source,
+ width,
+ height,
+ params,
+ flipSourceY,
+}: {
+ readonly state: LinearProgressiveBlurState;
+ readonly source: CanvasImageSource;
+ readonly width: number;
+ readonly height: number;
+ readonly params: LinearProgressiveBlurResolved;
+ readonly flipSourceY: boolean;
+}): void => {
+ const {
+ gl,
+ programHorizontal,
+ programVertical,
+ textureSource,
+ textureIntermediate,
+ framebuffer,
+ } = state;
+
+ gl.viewport(0, 0, width, height);
+
+ uploadSource({
+ gl,
+ textureSource,
+ textureIntermediate,
+ source,
+ width,
+ height,
+ flipSourceY,
+ });
+
+ gl.clearColor(0, 0, 0, 0);
+
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, textureSource);
+ gl.useProgram(programHorizontal);
+ setUniforms({
+ gl,
+ uniforms: state.horizontal,
+ width,
+ height,
+ params,
+ });
+ drawFullscreenQuad(state);
+
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.bindTexture(gl.TEXTURE_2D, textureIntermediate);
+ gl.useProgram(programVertical);
+ setUniforms({
+ gl,
+ uniforms: state.vertical,
+ width,
+ height,
+ params,
+ });
+ drawFullscreenQuad(state);
+
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.useProgram(null);
+};
diff --git a/packages/effects/src/linear-progressive-blur/linear-progressive-blur-shaders.ts b/packages/effects/src/linear-progressive-blur/linear-progressive-blur-shaders.ts
new file mode 100644
index 0000000000..95df860dcc
--- /dev/null
+++ b/packages/effects/src/linear-progressive-blur/linear-progressive-blur-shaders.ts
@@ -0,0 +1,82 @@
+export const LINEAR_PROGRESSIVE_BLUR_VS = /* glsl */ `#version 300 es
+layout(location = 0) in vec2 aPos;
+layout(location = 1) in vec2 aUv;
+out vec2 vUv;
+void main() {
+ vUv = aUv;
+ gl_Position = vec4(aPos, 0.0, 1.0);
+}
+`;
+
+export const buildLinearProgressiveBlurFs = (
+ direction: 'horizontal' | 'vertical',
+) => {
+ const dirVec =
+ direction === 'horizontal' ? 'vec2(1.0, 0.0)' : 'vec2(0.0, 1.0)';
+
+ return /* glsl */ `#version 300 es
+precision highp float;
+
+in vec2 vUv;
+out vec4 fragColor;
+
+uniform sampler2D uSource;
+uniform vec2 uTexelSize;
+uniform vec2 uStart;
+uniform vec2 uEnd;
+uniform float uStartBlur;
+uniform float uEndBlur;
+
+const int MIN_KERNEL_HALF = 4;
+const int MAX_KERNEL_HALF = 32;
+const float TARGET_SAMPLE_DISTANCE_PX = 2.0;
+const vec2 DIRECTION = ${dirVec};
+
+float progressiveRadius(vec2 uv) {
+ vec2 gradient = uEnd - uStart;
+ float gradientLengthSq = dot(gradient, gradient);
+ if (gradientLengthSq <= 0.0000001) {
+ return max(0.0, uStartBlur);
+ }
+
+ float progress = dot(uv - uStart, gradient) / gradientLengthSq;
+ return max(0.0, mix(uStartBlur, uEndBlur, clamp(progress, 0.0, 1.0)));
+}
+
+void main() {
+ // Public UV coordinates use top-left origin, matching canvas/CSS coordinates.
+ float radius = progressiveRadius(vec2(vUv.x, 1.0 - vUv.y));
+ if (radius <= 0.0) {
+ fragColor = texture(uSource, vUv);
+ return;
+ }
+
+ int kernelHalf = int(
+ min(
+ float(MAX_KERNEL_HALF),
+ max(float(MIN_KERNEL_HALF), ceil(radius / TARGET_SAMPLE_DISTANCE_PX))
+ )
+ );
+ float pixelStride = radius / float(kernelHalf);
+ float sigma = max(radius / 3.0, 0.0001);
+ float twoSigmaSq = 2.0 * sigma * sigma;
+
+ vec4 sum = vec4(0.0);
+ float weightSum = 0.0;
+
+ for (int i = -MAX_KERNEL_HALF; i <= MAX_KERNEL_HALF; ++i) {
+ if (abs(i) > kernelHalf) {
+ continue;
+ }
+
+ float offsetPx = float(i) * pixelStride;
+ float w = exp(-(offsetPx * offsetPx) / twoSigmaSq);
+ vec2 uv = vUv + DIRECTION * uTexelSize * offsetPx;
+ sum += texture(uSource, uv) * w;
+ weightSum += w;
+ }
+
+ fragColor = sum / weightSum;
+}
+`;
+};
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 3cc6e35850..65b6cc86dc 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -16,6 +16,7 @@ import {halftoneLinearGradient} from '../halftone-linear-gradient.js';
import {halftone} from '../halftone.js';
import {hue} from '../hue.js';
import {invert} from '../invert.js';
+import {linearProgressiveBlur} from '../linear-progressive-blur/index.js';
import {lines} from '../lines.js';
import {mirror} from '../mirror.js';
import {noise} from '../noise.js';
@@ -89,6 +90,9 @@ test('@remotion/effects expose documentation links', () => {
expect(lines().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/lines',
);
+ expect(linearProgressiveBlur().definition.documentationLink).toBe(
+ 'https://www.remotion.dev/docs/effects/linear-progressive-blur',
+ );
expect(dotGrid().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/dot-grid',
);
@@ -162,6 +166,9 @@ test('@remotion/effects expose API names as Studio labels', () => {
expect(hue().definition.label).toBe('hue()');
expect(invert().definition.label).toBe('invert()');
expect(lines().definition.label).toBe('lines()');
+ expect(linearProgressiveBlur().definition.label).toBe(
+ 'linearProgressiveBlur()',
+ );
expect(dotGrid().definition.label).toBe('dotGrid()');
expect(mirror().definition.label).toBe('mirror()');
expect(noise().definition.label).toBe('noise()');
@@ -1522,6 +1529,59 @@ test('lines() parameters produce distinct effect keys', () => {
).toBe(7);
});
+test('linearProgressiveBlur() accepts default params', () => {
+ expect(() => linearProgressiveBlur()).not.toThrow();
+});
+
+test('linearProgressiveBlur() rejects invalid start', () => {
+ expect(() =>
+ linearProgressiveBlur({
+ start: [0.5] as unknown as [number, number],
+ }),
+ ).toThrow('"start" must be a [number, number] tuple');
+});
+
+test('linearProgressiveBlur() rejects invalid end', () => {
+ expect(() =>
+ linearProgressiveBlur({
+ end: [0.5, Number.NaN],
+ }),
+ ).toThrow('"end" must be a [number, number] tuple');
+});
+
+test('linearProgressiveBlur() rejects non-finite blur radii', () => {
+ expect(() => linearProgressiveBlur({startBlur: Number.NaN})).toThrow(
+ '"startBlur" must be a finite number',
+ );
+ expect(() => linearProgressiveBlur({endBlur: Number.NaN})).toThrow(
+ '"endBlur" must be a finite number',
+ );
+});
+
+test('linearProgressiveBlur() clamps negative blur radii', () => {
+ const clamped = linearProgressiveBlur({startBlur: -10, endBlur: -1});
+ const zero = linearProgressiveBlur({startBlur: 0, endBlur: 0});
+ expect(clamped.effectKey).toBe(zero.effectKey);
+});
+
+test('linearProgressiveBlur() parameters produce distinct effect keys', () => {
+ const defaults = linearProgressiveBlur();
+ const shiftedStart = linearProgressiveBlur({start: [0.2, 0.5]});
+ const shiftedEnd = linearProgressiveBlur({end: [0.8, 0.5]});
+ const moreStartBlur = linearProgressiveBlur({startBlur: 12});
+ const moreEndBlur = linearProgressiveBlur({endBlur: 80});
+
+ expect(
+ new Set([
+ defaults.effectKey,
+ shiftedStart.effectKey,
+ shiftedEnd.effectKey,
+ moreStartBlur.effectKey,
+ moreEndBlur.effectKey,
+ ]).size,
+ ).toBe(5);
+});
+
test('rings() accepts default params', () => {
expect(() => rings()).not.toThrow();
});