Skip to content

🎥 Make videos programmatically with React

Add rings effect

Cursor Agent51e418053311
diff --git a/packages/docs/components/effects-demos/registry.ts b/packages/docs/components/effects-demos/registry.ts
index f4936826c7..77c5b501f1 100644
--- a/packages/docs/components/effects-demos/registry.ts
+++ b/packages/docs/components/effects-demos/registry.ts
@@ -18,6 +18,7 @@ import {lines} from '@remotion/effects/lines';
import {mirror} from '@remotion/effects/mirror';
import {noise} from '@remotion/effects/noise';
import {pixelDissolve} from '@remotion/effects/pixel-dissolve';
+import {rings} from '@remotion/effects/rings';
import {saturation} from '@remotion/effects/saturation';
import {scale} from '@remotion/effects/scale';
import {scanlines} from '@remotion/effects/scanlines';
@@ -53,6 +54,7 @@ import {EffectsLinesPreview} from '../effects/effects-lines-preview';
import {EffectsMirrorPreview} from '../effects/effects-mirror-preview';
import {EffectsNoisePreview} from '../effects/effects-noise-preview';
import {EffectsPixelDissolvePreview} from '../effects/effects-pixel-dissolve-preview';
+import {EffectsRingsPreview} from '../effects/effects-rings-preview';
import {EffectsSaturationPreview} from '../effects/effects-saturation-preview';
import {EffectsScalePreview} from '../effects/effects-scale-preview';
import {EffectsScanlinesPreview} from '../effects/effects-scanlines-preview';
@@ -229,6 +231,14 @@ export const effectsDemos: EffectsDemoType[] = [
comp: EffectsLinesPreview,
schema: lines().definition.schema,
},
+ {
+ ...defaults,
+ id: 'effects-rings',
+ effectName: 'rings',
+ effectImportPath: '@remotion/effects/rings',
+ comp: EffectsRingsPreview,
+ schema: rings().definition.schema,
+ },
{
...defaults,
id: 'effects-scale',
diff --git a/packages/docs/components/effects/effects-rings-preview.tsx b/packages/docs/components/effects/effects-rings-preview.tsx
new file mode 100644
index 0000000000..cf9a2b1634
--- /dev/null
+++ b/packages/docs/components/effects/effects-rings-preview.tsx
@@ -0,0 +1,21 @@
+import type {RingsCenter} from '@remotion/effects/rings';
+import {rings} from '@remotion/effects/rings';
+import React from 'react';
+import {Solid, useVideoConfig} from 'remotion';
+
+export const EffectsRingsPreview: React.FC<{
+ readonly center: RingsCenter;
+ readonly thickness: number;
+ readonly gap: number;
+ readonly offset: number;
+}> = ({center, thickness, gap, offset}) => {
+ const {width, height} = useVideoConfig();
+
+ return (
+ <Solid
+ width={width}
+ height={height}
+ effects={[rings({center, thickness, gap, offset})]}
+ />
+ );
+};
diff --git a/packages/docs/docs/effects/rings.mdx b/packages/docs/docs/effects/rings.mdx
new file mode 100644
index 0000000000..9dd6a1011b
--- /dev/null
+++ b/packages/docs/docs/effects/rings.mdx
@@ -0,0 +1,94 @@
+---
+image: /generated/articles-docs-effects-rings.png
+slug: /effects/rings
+title: rings()
+sidebar_label: rings()
+crumb: '@remotion/effects'
+---
+
+# rings()<AvailableFrom v="4.0.471" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+Adds concentric circular bands to canvas-based components such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) and [`<Solid>`](/docs/solid).
+
+It uses the same band controls as [`lines()`](/docs/effects/lines), but radiates the bands from a normalized center point.
+
+## Preview
+
+<EffectsDemo type="effects-rings" />
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {rings} from '@remotion/effects/rings';
+import {Video} from '@remotion/media';
+import {AbsoluteFill, useCurrentFrame} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ const frame = useCurrentFrame();
+
+ return (
+ <AbsoluteFill>
+ <Video
+ src="https://remotion.media/video.mp4"
+ effects={[
+ rings({
+ colors: ['#9fd6ff', 'transparent'],
+ center: [0.5, 0.5],
+ thickness: 40,
+ gap: 0,
+ offset: frame * 2,
+ }),
+ ]}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+## API
+
+Pass an object with the following properties. You can also call `rings()` without arguments.
+
+### `colors?`
+
+An array of color strings for the rings. Colors are assigned cyclically. Must contain at least 2 colors.
+
+Defaults to `['#dff4ff', '#7cc6ff']`.
+
+### `center?`
+
+Center of the rings, normalized from `0` to `1` in x/y. Defaults to `[0.5, 0.5]`.
+
+### `thickness?`
+
+Thickness of each colored ring in pixels. Defaults to `40`.
+
+### `gap?`
+
+Transparent gap in pixels between each ring. Defaults to `0`.
+
+With `gap: 0` (the default), rings are packed solid with no gap between them.
+With `gap > 0`, a transparent ring appears between each colored ring, allowing the source content to show through.
+
+### `offset?`
+
+Radial offset in pixels. Defaults to `0`.
+
+Animate this value with [`useCurrentFrame()`](/docs/use-current-frame) to expand or contract the rings.
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`.
+
+## Requirements
+
+`rings()` 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)
+- [`lines()`](/docs/effects/lines)
+- [`waves()`](/docs/effects/waves)
+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/rings.ts)
diff --git a/packages/docs/docs/effects/table-of-contents.tsx b/packages/docs/docs/effects/table-of-contents.tsx
index 2e1955e137..c8934b85c5 100644
--- a/packages/docs/docs/effects/table-of-contents.tsx
+++ b/packages/docs/docs/effects/table-of-contents.tsx
@@ -270,6 +270,13 @@ const categories: {
name: 'lines()',
description: 'Alternating line pattern effect',
},
+ {
+ link: '/docs/effects/rings',
+ preview: '/img/effects-rings-preview.png',
+ alt: 'rings effect preview',
+ name: 'rings()',
+ description: 'Concentric ring pattern effect',
+ },
{
link: '/docs/effects/waves',
preview: '/img/effects-waves-preview.png',
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 33d13fc1c7..4af42c7876 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -288,6 +288,7 @@ const sidebars: SidebarsConfig = {
'effects/lines',
'effects/mirror',
'effects/noise',
+ 'effects/rings',
'effects/saturation',
'effects/scale',
'effects/scanlines',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 8f6e15db39..8f6b478dc6 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1907,6 +1907,15 @@ export const articles = [
noAi: false,
slug: 'effects/pixel-dissolve',
},
+ {
+ id: 'effects/rings',
+ title: 'rings()',
+ relativePath: 'docs/effects/rings.mdx',
+ compId: 'articles-docs-effects-rings',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/rings',
+ },
{
id: 'effects/saturation',
title: 'saturation()',
diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsx
index c304af2db3..8c903033a5 100644
--- a/packages/docs/src/remotion/Root.tsx
+++ b/packages/docs/src/remotion/Root.tsx
@@ -23,6 +23,7 @@ import {EffectsLinesPreview} from '../../components/effects/effects-lines-previe
import {EffectsMirrorPreview} from '../../components/effects/effects-mirror-preview';
import {EffectsNoisePreview} from '../../components/effects/effects-noise-preview';
import {EffectsPixelDissolvePreview} from '../../components/effects/effects-pixel-dissolve-preview';
+import {EffectsRingsPreview} from '../../components/effects/effects-rings-preview';
import {EffectsSaturationPreview} from '../../components/effects/effects-saturation-preview';
import {EffectsScalePreview} from '../../components/effects/effects-scale-preview';
import {EffectsScanlinesPreview} from '../../components/effects/effects-scanlines-preview';
@@ -381,6 +382,18 @@ export const RemotionRoot: React.FC = () => {
offset: 0,
}}
/>
+ <Still
+ id="effects-rings-preview"
+ component={EffectsRingsPreview}
+ width={1280}
+ height={720}
+ defaultProps={{
+ center: [0.5, 0.5],
+ thickness: 40,
+ gap: 0,
+ offset: 0,
+ }}
+ />
<Still
id="effects-waves-preview"
component={EffectsWavesPreview}
diff --git a/packages/docs/static/generated/articles-docs-effects-rings.png b/packages/docs/static/generated/articles-docs-effects-rings.png
new file mode 100644
index 0000000000..1a8bed9ba9
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-rings.png differ
diff --git a/packages/docs/static/img/effects-rings-preview.png b/packages/docs/static/img/effects-rings-preview.png
new file mode 100644
index 0000000000..a9d2995777
Binary files /dev/null and b/packages/docs/static/img/effects-rings-preview.png differ
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 2d086d0a65..d2bdc79bee 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -27,6 +27,7 @@ const effectEntrypoints = [
'src/lines.ts',
'src/mirror.ts',
'src/noise.ts',
+ 'src/rings.ts',
'src/saturation.ts',
'src/scanlines.ts',
'src/scale.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index 1b2a9ff58b..ea32dd8766 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -134,6 +134,11 @@
"module": "./dist/esm/noise.mjs",
"import": "./dist/esm/noise.mjs"
},
+ "./rings": {
+ "types": "./dist/rings.d.ts",
+ "module": "./dist/esm/rings.mjs",
+ "import": "./dist/esm/rings.mjs"
+ },
"./saturation": {
"types": "./dist/saturation.d.ts",
"module": "./dist/esm/saturation.mjs",
@@ -258,6 +263,9 @@
"noise": [
"dist/noise.d.ts"
],
+ "rings": [
+ "dist/rings.d.ts"
+ ],
"saturation": [
"dist/saturation.d.ts"
],
diff --git a/packages/effects/src/index.ts b/packages/effects/src/index.ts
index 13d293cb00..20ebdfc45d 100644
--- a/packages/effects/src/index.ts
+++ b/packages/effects/src/index.ts
@@ -1 +1,2 @@
+export {rings, type RingsCenter, type RingsParams} from './rings.js';
export {zigzag, type ZigzagDirection, type ZigzagParams} from './zigzag.js';
diff --git a/packages/effects/src/rings.ts b/packages/effects/src/rings.ts
new file mode 100644
index 0000000000..ffbf1b87de
--- /dev/null
+++ b/packages/effects/src/rings.ts
@@ -0,0 +1,487 @@
+import type {SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import {
+ assertOptionalFiniteNumber,
+ parseColorRgba,
+ validateUnitInterval,
+ type ParsedColorRgba,
+} from './color-utils.js';
+import {
+ assertEffectParamsObject,
+ assertRequiredColor,
+} from './validate-effect-param.js';
+
+const {createEffect, createWebGL2ContextError} = Internals;
+
+const DEFAULT_COLORS = ['#dff4ff', '#7cc6ff'] as const;
+const DEFAULT_CENTER = [0.5, 0.5] as const;
+const DEFAULT_THICKNESS = 40 as const;
+const DEFAULT_GAP = 0 as const;
+const DEFAULT_OFFSET = 0 as const;
+
+export const ringsSchema = {
+ center: {
+ type: 'uv-coordinate',
+ min: 0,
+ max: 1,
+ step: 0.01,
+ default: DEFAULT_CENTER,
+ description: 'Center',
+ },
+ thickness: {
+ type: 'number',
+ min: 0.1,
+ max: 400,
+ step: 0.1,
+ default: DEFAULT_THICKNESS,
+ description: 'Thickness',
+ },
+ gap: {
+ type: 'number',
+ min: 0,
+ max: 400,
+ step: 0.1,
+ default: DEFAULT_GAP,
+ description: 'Gap',
+ },
+ offset: {
+ type: 'number',
+ step: 0.1,
+ default: DEFAULT_OFFSET,
+ description: 'Offset',
+ },
+} as const satisfies SequenceSchema;
+
+export type RingsCenter = readonly [number, number];
+
+export type RingsParams = {
+ /** Ring colors, assigned cyclically. Same semantics as lines(). */
+ readonly colors?: readonly string[];
+ /** Center of the rings, normalized from `0` to `1` in x/y. */
+ readonly center?: RingsCenter;
+ /** Thickness of each colored ring in pixels. Same as lines(). */
+ readonly thickness?: number;
+ /** Transparent gap in pixels between rings. Same as lines(). */
+ readonly gap?: number;
+ /** Radial offset in pixels. Animate to expand or contract the rings. */
+ readonly offset?: number;
+};
+
+type RingsResolved = {
+ colors: readonly string[];
+ center: RingsCenter;
+ thickness: number;
+ spacing: number;
+ offset: number;
+};
+
+type RingsState = {
+ readonly gl: WebGL2RenderingContext;
+ readonly program: WebGLProgram;
+ readonly vao: WebGLVertexArrayObject;
+ readonly vbo: WebGLBuffer;
+ readonly sourceTexture: WebGLTexture;
+ readonly paletteTexture: WebGLTexture;
+ readonly colorCtx: CanvasRenderingContext2D;
+ readonly uniforms: {
+ readonly uSource: WebGLUniformLocation | null;
+ readonly uPalette: WebGLUniformLocation | null;
+ readonly uResolution: WebGLUniformLocation | null;
+ readonly uNumColors: WebGLUniformLocation | null;
+ readonly uCenter: WebGLUniformLocation | null;
+ readonly uThickness: WebGLUniformLocation | null;
+ readonly uSpacing: WebGLUniformLocation | null;
+ readonly uOffset: WebGLUniformLocation | null;
+ };
+ cachedPaletteKey: string;
+ palettePixelData: Uint8Array;
+};
+
+const resolve = (p: RingsParams): RingsResolved => {
+ const thickness = p.thickness ?? DEFAULT_THICKNESS;
+ const gap = p.gap ?? DEFAULT_GAP;
+
+ return {
+ colors: p.colors ?? DEFAULT_COLORS,
+ center: [...(p.center ?? DEFAULT_CENTER)] as RingsCenter,
+ thickness,
+ spacing: thickness + gap,
+ offset: p.offset ?? DEFAULT_OFFSET,
+ };
+};
+
+const validatePositive = (value: number, name: string): void => {
+ if (value <= 0) {
+ throw new TypeError(
+ `"${name}" must be greater than 0, but got ${JSON.stringify(value)}`,
+ );
+ }
+};
+
+const validateNonNegative = (value: number, name: string): void => {
+ if (value < 0) {
+ throw new TypeError(
+ `"${name}" must be greater than or equal to 0, but got ${JSON.stringify(value)}`,
+ );
+ }
+};
+
+const validateColors = (colors: unknown): void => {
+ if (colors === undefined) {
+ return;
+ }
+
+ if (!Array.isArray(colors) || colors.length < 2) {
+ throw new TypeError(
+ `"colors" must be an array with at least 2 colors, but got ${JSON.stringify(colors)}`,
+ );
+ }
+
+ for (let i = 0; i < colors.length; i++) {
+ assertRequiredColor(colors[i], `colors[${i}]`);
+ }
+};
+
+const assertOptionalCenter = (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`);
+ }
+
+ validateUnitInterval(value[0], `${name}[0]`);
+ validateUnitInterval(value[1], `${name}[1]`);
+};
+
+const validateRingsParams = (params: RingsParams): void => {
+ assertEffectParamsObject(params, 'Rings');
+ validateColors(params.colors);
+ assertOptionalCenter(params.center, 'center');
+ assertOptionalFiniteNumber(params.thickness, 'thickness');
+ assertOptionalFiniteNumber(params.gap, 'gap');
+ assertOptionalFiniteNumber(params.offset, 'offset');
+
+ const thickness = params.thickness ?? DEFAULT_THICKNESS;
+ const gap = params.gap ?? DEFAULT_GAP;
+ validatePositive(thickness, 'thickness');
+ validateNonNegative(gap, 'gap');
+};
+
+const RINGS_VS = /* glsl */ `#version 300 es
+in vec2 aPos;
+in vec2 aUv;
+out vec2 vUv;
+
+void main() {
+ vUv = aUv;
+ gl_Position = vec4(aPos, 0.0, 1.0);
+}
+`;
+
+const RINGS_FS = /* glsl */ `#version 300 es
+precision highp float;
+
+in vec2 vUv;
+out vec4 fragColor;
+
+uniform sampler2D uSource;
+uniform sampler2D uPalette;
+uniform vec2 uResolution;
+uniform float uNumColors;
+uniform vec2 uCenter;
+uniform float uThickness;
+uniform float uSpacing;
+uniform float uOffset;
+
+void main() {
+ vec4 texColor = texture(uSource, vUv);
+ float thickness = max(uThickness, 0.001);
+ float spacing = max(uSpacing, 0.001);
+ float cycle = spacing * uNumColors;
+ vec2 pixelPosition = vUv * uResolution;
+ vec2 centerPosition = uCenter * uResolution;
+ float radius = length(pixelPosition - centerPosition);
+ float position = mod(radius + uOffset, cycle);
+ if (position < 0.0) {
+ position += cycle;
+ }
+
+ float colorIndex = floor(position / spacing);
+ float inStripe = mod(position, spacing);
+ if (inStripe > thickness) {
+ fragColor = texColor;
+ return;
+ }
+
+ float texCoord = (colorIndex + 0.5) / uNumColors;
+ vec4 ringColor = texture(uPalette, vec2(texCoord, 0.5));
+ float ringAlpha = ringColor.a;
+ vec3 premultipliedRing = ringColor.rgb * ringAlpha;
+
+ fragColor = vec4(
+ premultipliedRing + texColor.rgb * (1.0 - ringAlpha),
+ ringAlpha + texColor.a * (1.0 - ringAlpha)
+ );
+}
+`;
+
+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(`Rings 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(`Rings 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 createTexture = (
+ gl: WebGL2RenderingContext,
+ filter: number,
+): 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, filter);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
+ 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 setupRings = (target: HTMLCanvasElement): RingsState => {
+ const gl = target.getContext('webgl2', {
+ premultipliedAlpha: true,
+ alpha: true,
+ preserveDrawingBuffer: true,
+ });
+ if (!gl) {
+ throw createWebGL2ContextError('rings effect');
+ }
+
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
+
+ const program = createProgram(gl, RINGS_VS, RINGS_FS);
+
+ 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);
+
+ const aPos = gl.getAttribLocation(program, 'aPos');
+ const aUv = gl.getAttribLocation(program, 'aUv');
+ gl.enableVertexAttribArray(aPos);
+ gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 16, 0);
+ gl.enableVertexAttribArray(aUv);
+ gl.vertexAttribPointer(aUv, 2, gl.FLOAT, false, 16, 8);
+
+ gl.bindVertexArray(null);
+
+ const colorCanvas = document.createElement('canvas');
+ colorCanvas.width = 1;
+ colorCanvas.height = 1;
+ const colorCtx = colorCanvas.getContext('2d', {willReadFrequently: true});
+ if (!colorCtx) {
+ throw new Error('Failed to acquire 2D context for color parsing');
+ }
+
+ return {
+ gl,
+ program,
+ vao,
+ vbo,
+ sourceTexture: createTexture(gl, gl.LINEAR),
+ paletteTexture: createTexture(gl, gl.NEAREST),
+ colorCtx,
+ uniforms: {
+ uSource: gl.getUniformLocation(program, 'uSource'),
+ uPalette: gl.getUniformLocation(program, 'uPalette'),
+ uResolution: gl.getUniformLocation(program, 'uResolution'),
+ uNumColors: gl.getUniformLocation(program, 'uNumColors'),
+ uCenter: gl.getUniformLocation(program, 'uCenter'),
+ uThickness: gl.getUniformLocation(program, 'uThickness'),
+ uSpacing: gl.getUniformLocation(program, 'uSpacing'),
+ uOffset: gl.getUniformLocation(program, 'uOffset'),
+ },
+ cachedPaletteKey: '',
+ palettePixelData: new Uint8Array(0),
+ };
+};
+
+const updatePalette = (
+ state: RingsState,
+ colors: readonly string[],
+): boolean => {
+ const paletteKey = colors.join('|');
+ const paletteDirty = state.cachedPaletteKey !== paletteKey;
+ if (!paletteDirty) {
+ return false;
+ }
+
+ state.cachedPaletteKey = paletteKey;
+ const len = colors.length * 4;
+ if (state.palettePixelData.length !== len) {
+ state.palettePixelData = new Uint8Array(len);
+ }
+
+ const {palettePixelData} = state;
+ for (let i = 0; i < colors.length; i++) {
+ const color: ParsedColorRgba = parseColorRgba(state.colorCtx, colors[i]);
+ palettePixelData[i * 4] = color[0];
+ palettePixelData[i * 4 + 1] = color[1];
+ palettePixelData[i * 4 + 2] = color[2];
+ palettePixelData[i * 4 + 3] = color[3];
+ }
+
+ return true;
+};
+
+export const rings = createEffect<RingsParams, RingsState>({
+ type: 'remotion/rings',
+ label: 'rings()',
+ documentationLink: 'https://www.remotion.dev/docs/effects/rings',
+ backend: 'webgl2',
+ calculateKey: (params) => {
+ const r = resolve(params);
+ return `rings-${r.colors.join('|')}-${r.center.join(':')}-${r.thickness}-${r.spacing}-${r.offset}`;
+ },
+ setup: (target) => setupRings(target),
+ apply: ({source, width, height, params, state, flipSourceY}) => {
+ const r = resolve(params);
+ const paletteDirty = updatePalette(state, r.colors);
+ const {gl, program, sourceTexture, paletteTexture, uniforms, vao} = state;
+
+ gl.viewport(0, 0, width, height);
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.clearColor(0, 0, 0, 0);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, sourceTexture);
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ source as TexImageSource,
+ );
+
+ gl.activeTexture(gl.TEXTURE1);
+ gl.bindTexture(gl.TEXTURE_2D, paletteTexture);
+ if (paletteDirty) {
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ r.colors.length,
+ 1,
+ 0,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ state.palettePixelData,
+ );
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
+ }
+
+ gl.useProgram(program);
+ if (uniforms.uSource) gl.uniform1i(uniforms.uSource, 0);
+ if (uniforms.uPalette) gl.uniform1i(uniforms.uPalette, 1);
+ if (uniforms.uResolution) gl.uniform2f(uniforms.uResolution, width, height);
+ if (uniforms.uNumColors) gl.uniform1f(uniforms.uNumColors, r.colors.length);
+ if (uniforms.uCenter)
+ gl.uniform2f(uniforms.uCenter, r.center[0], r.center[1]);
+ if (uniforms.uThickness) gl.uniform1f(uniforms.uThickness, r.thickness);
+ if (uniforms.uSpacing) gl.uniform1f(uniforms.uSpacing, r.spacing);
+ if (uniforms.uOffset) gl.uniform1f(uniforms.uOffset, r.offset);
+
+ gl.bindVertexArray(vao);
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+ gl.bindVertexArray(null);
+ gl.bindTexture(gl.TEXTURE_2D, null);
+ gl.useProgram(null);
+ },
+ cleanup: ({gl, program, vao, vbo, sourceTexture, paletteTexture}) => {
+ gl.deleteTexture(sourceTexture);
+ gl.deleteTexture(paletteTexture);
+ gl.deleteBuffer(vbo);
+ gl.deleteProgram(program);
+ gl.deleteVertexArray(vao);
+ },
+ schema: ringsSchema,
+ validateParams: validateRingsParams,
+});
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 580cfa132d..6057fd07db 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -19,6 +19,7 @@ import {lines} from '../lines.js';
import {mirror} from '../mirror.js';
import {noise} from '../noise.js';
import {pixelDissolve} from '../pixel-dissolve.js';
+import {rings} from '../rings.js';
import {saturation} from '../saturation.js';
import {scale} from '../scale.js';
import {scanlines} from '../scanlines.js';
@@ -93,6 +94,9 @@ test('@remotion/effects expose documentation links', () => {
expect(noise().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/noise',
);
+ expect(rings().definition.documentationLink).toBe(
+ 'https://www.remotion.dev/docs/effects/rings',
+ );
expect(saturation().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/saturation',
);
@@ -156,6 +160,7 @@ test('@remotion/effects expose API names as Studio labels', () => {
expect(dotGrid().definition.label).toBe('dotGrid()');
expect(mirror().definition.label).toBe('mirror()');
expect(noise().definition.label).toBe('noise()');
+ expect(rings().definition.label).toBe('rings()');
expect(saturation().definition.label).toBe('saturation()');
expect(scanlines().definition.label).toBe('scanlines()');
expect(scale({scale: 1}).definition.label).toBe('scale()');
@@ -1436,6 +1441,87 @@ test('lines() parameters produce distinct effect keys', () => {
).toBe(7);
});
+test('rings() accepts default params', () => {
+ expect(() => rings()).not.toThrow();
+});
+
+test('rings() accepts custom colors', () => {
+ expect(() => rings({colors: ['#dff4ff', 'transparent']})).not.toThrow();
+});
+
+test('rings() rejects invalid colors', () => {
+ expect(() => rings({colors: ['#dff4ff']})).toThrow(
+ '"colors" must be an array with at least 2 colors',
+ );
+ expect(() => rings({colors: ['#dff4ff', '' as unknown as string]})).toThrow(
+ '"colors[1]" must be a non-empty string',
+ );
+});
+
+test('rings() rejects invalid center', () => {
+ const invalidCenter = [0.5] as unknown as [number, number];
+ expect(() => rings({center: invalidCenter})).toThrow(
+ '"center" must be a [number, number] tuple',
+ );
+});
+
+test('rings() rejects center outside unit interval', () => {
+ expect(() => rings({center: [1.1, 0.5]})).toThrow('"center[0]" must be <= 1');
+ expect(() => rings({center: [0.5, -0.1]})).toThrow(
+ '"center[1]" must be >= 0',
+ );
+});
+
+test('rings() rejects non-finite thickness', () => {
+ expect(() => rings({thickness: Number.NaN})).toThrow(
+ '"thickness" must be a finite number',
+ );
+});
+
+test('rings() rejects non-positive thickness', () => {
+ expect(() => rings({thickness: 0})).toThrow(
+ '"thickness" must be greater than 0',
+ );
+});
+
+test('rings() rejects non-finite gap', () => {
+ expect(() => rings({gap: Number.NaN})).toThrow(
+ '"gap" must be a finite number',
+ );
+});
+
+test('rings() rejects negative gap', () => {
+ expect(() => rings({gap: -1})).toThrow(
+ '"gap" must be greater than or equal to 0',
+ );
+});
+
+test('rings() rejects non-finite offset', () => {
+ expect(() => rings({offset: Number.NaN})).toThrow(
+ '"offset" must be a finite number',
+ );
+});
+
+test('rings() parameters produce distinct effect keys', () => {
+ const defaultRings = rings();
+ const colored = rings({colors: ['#ffffff', 'transparent']});
+ const centered = rings({center: [0.3, 0.7]});
+ const thin = rings({thickness: 20});
+ const gapped = rings({gap: 24});
+ const shifted = rings({offset: 10});
+
+ expect(
+ new Set([
+ defaultRings.effectKey,
+ colored.effectKey,
+ centered.effectKey,
+ thin.effectKey,
+ gapped.effectKey,
+ shifted.effectKey,
+ ]).size,
+ ).toBe(6);
+});
+
test('hue() accepts default params', () => {
expect(() => hue()).not.toThrow();
});