🎥 Make videos programmatically with React
`@remotion/effects`: Add outline effect (#10643)
Jonny Burger20d613de28aa
diff --git a/packages/docs/components/effects-demos/registry.ts b/packages/docs/components/effects-demos/registry.tsindex a964fd426e..13cfc39d4f 100644--- a/packages/docs/components/effects-demos/registry.ts+++ b/packages/docs/components/effects-demos/registry.ts@@ -36,6 +36,7 @@ import {liquidContours} from '@remotion/effects/liquid-contours';import {mirror} from '@remotion/effects/mirror';import {noise} from '@remotion/effects/noise';import {noiseDisplacement} from '@remotion/effects/noise-displacement';+import {outline} from '@remotion/effects/outline';import {paper} from '@remotion/effects/paper';import {pattern} from '@remotion/effects/pattern';import {pixelDissolve} from '@remotion/effects/pixel-dissolve';@@ -112,6 +113,7 @@ import {NOISE_DISPLACEMENT_PREVIEW_PARAMS,} from '../effects/effects-noise-displacement-preview';import {EffectsNoisePreview} from '../effects/effects-noise-preview';+import {EffectsOutlinePreview} from '../effects/effects-outline-preview';import {EffectsPaletteMapPreview,paletteMap,@@ -238,6 +240,16 @@ export const effectsDemos: EffectsDemoType[] = [similarity: 0.45,},},+ {+ ...defaults,+ id: 'effects-outline',+ effectName: 'outline',+ effectImportPath: '@remotion/effects/outline',+ comp: EffectsOutlinePreview,+ schema: outline().definition.schema,+ durationInFrames: 150,+ autoPlay: true,+ },{...defaults,id: 'effects-color-correction',diff --git a/packages/docs/components/effects/effects-outline-preview.tsx b/packages/docs/components/effects/effects-outline-preview.tsxnew file mode 100644index 0000000000..c31a3a2ca6--- /dev/null+++ b/packages/docs/components/effects/effects-outline-preview.tsx@@ -0,0 +1,25 @@+import {colorKey} from '@remotion/effects/color-key';+import {outline} from '@remotion/effects/outline';+import {Video} from '@remotion/media';+import React from 'react';+import {AbsoluteFill} from 'remotion';++export const EffectsOutlinePreview: React.FC<{+ readonly width: number;+ readonly edgeSimplification: number;+ readonly color: string;+ readonly opacity: number;+ readonly outlineOnly: boolean;+}> = ({width, edgeSimplification, color, opacity, outlineOnly}) => {+ return (+ <AbsoluteFill style={{backgroundColor: '#15171e'}}>+ <Video+ src="https://remotion.media/greenscreen.mp4"+ effects={[+ colorKey({similarity: 0.45}),+ outline({width, edgeSimplification, color, opacity, outlineOnly}),+ ]}+ />+ </AbsoluteFill>+ );+};diff --git a/packages/docs/docs/effects/outline.mdx b/packages/docs/docs/effects/outline.mdxnew file mode 100644index 0000000000..805b22da4e--- /dev/null+++ b/packages/docs/docs/effects/outline.mdx@@ -0,0 +1,89 @@+---+image: /generated/articles-docs-effects-outline.png+slug: /effects/outline+title: outline()+sidebar_label: outline()+crumb: '@remotion/effects'+---++# outline()<AvailableFrom v="4.0.515" />++_Part of the [`@remotion/effects`](/docs/effects/api) package._++Draws a solid outline around the non-transparent pixels of a canvas-based component such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) or [`<Solid>`](/docs/solid).++## Preview++<EffectsDemo type="effects-outline" />++The preview applies [`colorKey()`](/docs/effects/color-key) before `outline()` so the outline follows the subject after the green background has been removed.++## Example++```tsx twoslash title="MyComp.tsx"+import {colorKey} from '@remotion/effects/color-key';+import {outline} from '@remotion/effects/outline';+import {Video} from '@remotion/media';+import {AbsoluteFill} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <AbsoluteFill style={{backgroundColor: '#15171e'}}>+ <Video+ src="https://remotion.media/greenscreen.mp4"+ effects={[+ colorKey({similarity: 0.45}),+ outline({width: 12, edgeSimplification: 12, color: '#00ffff'}),+ ]}+ />+ </AbsoluteFill>+ );+};+```++Effects run in array order. Place `outline()` after an effect that creates transparency, such as [`colorKey()`](/docs/effects/color-key), to outline the resulting alpha silhouette.++## API++All parameters are optional. You can call `outline()` without arguments.++### width?++Width of the outline in pixels. Defaults to `8`. Must be at least `0`.++### edgeSimplification?++Pixel tolerance used to simplify the alpha contour into straight edges. Larger values remove more contour points and make the outline more polygonal. `0` disables simplification and is the default.++The source remains unchanged. With `outlineOnly: true`, the simplified contour is used for the filled mask as well.++Values above `0` extract and simplify the alpha contour on the CPU for every frame, which is more expensive than the default outline.++### color?++Color of the outline. Accepts any CSS color string. Defaults to white.++### opacity?++Opacity of the outline from `0` to `1`. Defaults to `1`.++### outlineOnly?++When `true`, replaces the source with a filled mask in the outline color. The mask includes both the original alpha silhouette and the outline around it. Defaults to `false`.++### disabled?++When `true`, the effect is skipped. Defaults to `false`.++## Requirements++`outline()` uses a WebGL2 backend. See [Using WebGL and WebGPU during renders](/docs/webgl).++The outline is clipped at the canvas boundary. Leave transparent space around the source if the outline should extend beyond its current bounds.++## See also++- [`@remotion/effects`](/docs/effects/api)+- [`colorKey()`](/docs/effects/color-key)+- [`dropShadow()`](/docs/effects/drop-shadow)+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/outline.ts)diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 86619c85df..f42ad1b54b 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -311,6 +311,7 @@ const sidebars: SidebarsConfig = {'effects/mirror','effects/noise','effects/noise-displacement',+ 'effects/outline','effects/paper','effects/pattern','effects/pixel-dissolve',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 2aa9b3ec19..8a78d07622 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -2159,6 +2159,15 @@ export const articles = [noAi: false,slug: 'effects/noise',},+ {+ id: 'effects/outline',+ title: 'outline()',+ relativePath: 'docs/effects/outline.mdx',+ compId: 'articles-docs-effects-outline',+ crumb: '@remotion/effects',+ noAi: false,+ slug: 'effects/outline',+ },{id: 'effects/paper',title: 'paper()',diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsxindex 1c04bc20ec..8b22f648f1 100644--- a/packages/docs/src/remotion/Root.tsx+++ b/packages/docs/src/remotion/Root.tsx@@ -51,6 +51,7 @@ import {NoiseDisplacementTextSource,} from '../../components/effects/effects-noise-displacement-preview';import {EffectsNoisePreview} from '../../components/effects/effects-noise-preview';+import {EffectsOutlinePreview} from '../../components/effects/effects-outline-preview';import {EffectsPaperPreview,PAPER_PREVIEW_PARAMS,@@ -285,6 +286,19 @@ export const RemotionRoot: React.FC = () => {spillSuppression: 0.25,}}/>+ <Still+ id="effects-outline-preview"+ component={EffectsOutlinePreview}+ width={1280}+ height={720}+ defaultProps={{+ width: 12,+ edgeSimplification: 12,+ color: '#00ffff',+ opacity: 1,+ outlineOnly: false,+ }}+ /><Stillid="effects-duotone-preview"component={EffectsDuotonePreview}diff --git a/packages/docs/static/generated/articles-docs-effects-outline.png b/packages/docs/static/generated/articles-docs-effects-outline.pngnew file mode 100644index 0000000000..7f17e629cdBinary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-outline.png differdiff --git a/packages/docs/static/img/effects-outline-preview.png b/packages/docs/static/img/effects-outline-preview.pngnew file mode 100644index 0000000000..cce27e3900Binary files /dev/null and b/packages/docs/static/img/effects-outline-preview.png differdiff --git a/packages/effects/bundle.ts b/packages/effects/bundle.tsindex 7a61a8dbf1..be501e1cb5 100644--- a/packages/effects/bundle.ts+++ b/packages/effects/bundle.ts@@ -47,6 +47,7 @@ const effectEntrypoints = ['src/mirror.ts','src/noise.ts','src/noise-displacement.ts',+ 'src/outline.ts','src/paper.ts','src/roughen-edges.ts','src/pattern.ts',diff --git a/packages/effects/package.json b/packages/effects/package.jsonindex 77b22c8555..bd55ebfbae 100644--- a/packages/effects/package.json+++ b/packages/effects/package.json@@ -234,6 +234,11 @@"module": "./dist/esm/noise-displacement.mjs","import": "./dist/esm/noise-displacement.mjs"},+ "./outline": {+ "types": "./dist/outline.d.ts",+ "module": "./dist/esm/outline.mjs",+ "import": "./dist/esm/outline.mjs"+ },"./paper": {"types": "./dist/paper.d.ts","module": "./dist/esm/paper.mjs",@@ -508,6 +513,9 @@"noise-displacement": ["dist/noise-displacement.d.ts"],+ "outline": [+ "dist/outline.d.ts"+ ],"paper": ["dist/paper.d.ts"],diff --git a/packages/effects/src/outline.ts b/packages/effects/src/outline.tsnew file mode 100644index 0000000000..3308c3e80d--- /dev/null+++ b/packages/effects/src/outline.ts@@ -0,0 +1,550 @@+import type {InteractivitySchema} from 'remotion';+import {Internals} from 'remotion';+import {+ assertOptionalFiniteNumber,+ parseColorRgba,+ type ParsedColorRgba,+ validateNonNegative,+ validateUnitInterval,+} from './color-utils.js';+import {polygonizeAlpha} from './outline/polygonize-alpha.js';+import {+ assertOptionalBoolean,+ assertEffectParamsObject,+ assertOptionalColor,+} from './validate-effect-param.js';++const {createEffect, createWebGL2ContextError} = Internals;++const DEFAULT_WIDTH = 8 as const;+const DEFAULT_EDGE_SIMPLIFICATION = 0 as const;+const DEFAULT_COLOR = '#ffffff' as const;+const DEFAULT_OPACITY = 1 as const;+const DEFAULT_OUTLINE_ONLY = false as const;++export const outlineSchema = {+ width: {+ type: 'number',+ min: 0,+ max: 100,+ step: 1,+ default: DEFAULT_WIDTH,+ description: 'Width',+ hiddenFromList: false,+ },+ edgeSimplification: {+ type: 'number',+ min: 0,+ max: 100,+ step: 1,+ default: DEFAULT_EDGE_SIMPLIFICATION,+ description: 'Edge simplification',+ hiddenFromList: false,+ },+ color: {+ type: 'color',+ default: DEFAULT_COLOR,+ description: 'Color',+ },+ opacity: {+ type: 'number',+ min: 0,+ max: 1,+ step: 0.01,+ default: DEFAULT_OPACITY,+ description: 'Opacity',+ hiddenFromList: false,+ },+ outlineOnly: {+ type: 'boolean',+ default: DEFAULT_OUTLINE_ONLY,+ description: 'Outline only',+ },+} as const satisfies InteractivitySchema;++export type OutlineParams = {+ /** Width of the outline in pixels. Defaults to `8`. */+ readonly width?: number;+ /** Pixel tolerance used to simplify the alpha contour into straight edges. Defaults to `0`. */+ readonly edgeSimplification?: number;+ /** Color of the outline. Defaults to white. */+ readonly color?: string;+ /** Opacity of the outline from `0` to `1`. Defaults to `1`. */+ readonly opacity?: number;+ /** Whether to replace the source with a filled outline mask. Defaults to `false`. */+ readonly outlineOnly?: boolean;+};++type OutlineResolved = {+ readonly width: number;+ readonly edgeSimplification: number;+ readonly color: string;+ readonly opacity: number;+ readonly outlineOnly: boolean;+};++type OutlineState = {+ readonly gl: WebGL2RenderingContext;+ readonly program: WebGLProgram;+ readonly vao: WebGLVertexArrayObject;+ readonly vbo: WebGLBuffer;+ readonly textureSource: WebGLTexture;+ readonly texturePolygonMask: WebGLTexture;+ readonly uniforms: {+ readonly uSource: WebGLUniformLocation | null;+ readonly uPolygonMask: WebGLUniformLocation | null;+ readonly uUsePolygonMask: WebGLUniformLocation | null;+ readonly uWidth: WebGLUniformLocation | null;+ readonly uColor: WebGLUniformLocation | null;+ readonly uOpacity: WebGLUniformLocation | null;+ readonly uOutlineOnly: WebGLUniformLocation | null;+ };+ readonly alphaCanvas: HTMLCanvasElement;+ readonly alphaCtx: CanvasRenderingContext2D;+ readonly polygonMaskCanvas: HTMLCanvasElement;+ readonly polygonMaskCtx: CanvasRenderingContext2D;+ readonly colorCtx: CanvasRenderingContext2D;+ cachedColor: string;+ cachedColorRgba: ParsedColorRgba;+};++const resolve = (params: OutlineParams): OutlineResolved => ({+ width: params.width ?? DEFAULT_WIDTH,+ edgeSimplification: params.edgeSimplification ?? DEFAULT_EDGE_SIMPLIFICATION,+ color: params.color ?? DEFAULT_COLOR,+ opacity: params.opacity ?? DEFAULT_OPACITY,+ outlineOnly: params.outlineOnly ?? DEFAULT_OUTLINE_ONLY,+});++const validateOutlineParams = (params: OutlineParams): void => {+ assertEffectParamsObject(params, 'Outline');+ assertOptionalFiniteNumber(params.width, 'width');+ assertOptionalFiniteNumber(params.edgeSimplification, 'edgeSimplification');+ assertOptionalColor(params.color, 'color');+ assertOptionalFiniteNumber(params.opacity, 'opacity');+ assertOptionalBoolean(params.outlineOnly, 'outlineOnly');++ const resolved = resolve(params);+ validateNonNegative(resolved.width, 'width');+ validateNonNegative(resolved.edgeSimplification, 'edgeSimplification');+ validateUnitInterval(resolved.opacity, 'opacity');+};++const OUTLINE_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 OUTLINE_FS = /* glsl */ `#version 300 es+precision highp float;++in vec2 vUv;+out vec4 fragColor;++uniform sampler2D uSource;+uniform sampler2D uPolygonMask;+uniform bool uUsePolygonMask;+uniform float uWidth;+uniform vec4 uColor;+uniform float uOpacity;+uniform bool uOutlineOnly;++const float TAU = 6.283185307179586;+const float MIN_ALPHA = 0.5 / 255.0;+const int SAMPLE_DIRECTIONS = 32;+const int SAMPLE_RINGS = 3;++void main() {+ vec4 source = texture(uSource, vUv);++ if (uOpacity <= 0.0 || uColor.a <= 0.0) {+ fragColor = uOutlineOnly ? vec4(0.0) : source;+ return;+ }++ if (uWidth <= 0.0 && !uOutlineOnly) {+ fragColor = source;+ return;+ }++ float outlineMaskAlpha = 0.0;+ if (uUsePolygonMask) {+ outlineMaskAlpha = step(MIN_ALPHA, texture(uPolygonMask, vUv).a);+ } else if (uWidth > 0.0) {+ ivec2 sourceSize = textureSize(uSource, 0);+ ivec2 sourcePosition = clamp(+ ivec2(vUv * vec2(sourceSize)),+ ivec2(0),+ sourceSize - ivec2(1)+ );+ for (int ring = 1; ring <= SAMPLE_RINGS; ring++) {+ float distancePx = uWidth * float(ring) / float(SAMPLE_RINGS);+ for (int direction = 0; direction < SAMPLE_DIRECTIONS; direction++) {+ float angle = TAU * float(direction) / float(SAMPLE_DIRECTIONS);+ ivec2 offset = ivec2(round(+ vec2(cos(angle), sin(angle)) * distancePx+ ));+ ivec2 samplePosition = clamp(+ sourcePosition + offset,+ ivec2(0),+ sourceSize - ivec2(1)+ );+ outlineMaskAlpha = max(+ outlineMaskAlpha,+ step(MIN_ALPHA, texelFetch(uSource, samplePosition, 0).a)+ );+ }+ }+ }++ if (uOutlineOnly) {+ float filledAlpha = (+ uUsePolygonMask+ ? outlineMaskAlpha+ : max(step(MIN_ALPHA, source.a), outlineMaskAlpha)+ ) * uColor.a * uOpacity;+ fragColor = vec4(uColor.rgb * filledAlpha, filledAlpha);+ return;+ }++ float outlineAlpha = outlineMaskAlpha * uColor.a * uOpacity * (1.0 - source.a);+ vec3 outlineRgb = uColor.rgb * outlineAlpha;+ fragColor = vec4(source.rgb + outlineRgb, source.a + outlineAlpha);+}+`;++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(`Outline shader compile failed: ${log ?? '(no log)'}`);+ }++ return shader;+};++const createProgram = (gl: WebGL2RenderingContext): WebGLProgram => {+ const vertexShader = compileShader(gl, gl.VERTEX_SHADER, OUTLINE_VS);+ const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, OUTLINE_FS);+ 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(`Outline 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 updatePolygonMask = ({+ source,+ width,+ height,+ simplification,+ outlineWidth,+ state,+}: {+ readonly source: CanvasImageSource;+ readonly width: number;+ readonly height: number;+ readonly simplification: number;+ readonly outlineWidth: number;+ readonly state: OutlineState;+}): void => {+ if (+ state.alphaCanvas.width !== width ||+ state.alphaCanvas.height !== height ||+ state.polygonMaskCanvas.width !== width ||+ state.polygonMaskCanvas.height !== height+ ) {+ state.alphaCanvas.width = width;+ state.alphaCanvas.height = height;+ state.polygonMaskCanvas.width = width;+ state.polygonMaskCanvas.height = height;+ }++ state.alphaCtx.clearRect(0, 0, width, height);+ state.alphaCtx.drawImage(source, 0, 0, width, height);+ const imageData = state.alphaCtx.getImageData(0, 0, width, height);+ const contours = polygonizeAlpha({+ data: imageData.data,+ width,+ height,+ simplification,+ });++ const {polygonMaskCtx: context} = state;+ context.clearRect(0, 0, width, height);+ context.beginPath();+ for (const contour of contours) {+ if (contour.length < 3) {+ continue;+ }++ context.moveTo(contour[0][0], contour[0][1]);+ for (let index = 1; index < contour.length; index++) {+ context.lineTo(contour[index][0], contour[index][1]);+ }++ context.closePath();+ }++ context.fillStyle = 'white';+ context.fill('evenodd');+ if (outlineWidth > 0) {+ context.strokeStyle = 'white';+ context.lineWidth = outlineWidth * 2;+ context.lineJoin = 'miter';+ context.miterLimit = 4;+ context.stroke();+ }+};++const setupOutline = (target: HTMLCanvasElement): OutlineState => {+ const gl = target.getContext('webgl2', {+ premultipliedAlpha: true,+ alpha: true,+ preserveDrawingBuffer: true,+ });+ if (!gl) {+ throw createWebGL2ContextError('outline effect');+ }++ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);++ const program = createProgram(gl);+ const vao = gl.createVertexArray();+ if (!vao) {+ throw new Error('Failed to create WebGL vertex array');+ }++ gl.bindVertexArray(vao);+ 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,+ new Float32Array([-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1]),+ 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 textureSource = createTexture(gl);+ const texturePolygonMask = createTexture(gl);+ gl.bindTexture(gl.TEXTURE_2D, texturePolygonMask);+ gl.texImage2D(+ gl.TEXTURE_2D,+ 0,+ gl.RGBA,+ 1,+ 1,+ 0,+ gl.RGBA,+ gl.UNSIGNED_BYTE,+ new Uint8Array([0, 0, 0, 0]),+ );+ gl.bindTexture(gl.TEXTURE_2D, null);++ const colorCanvas = target.ownerDocument.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');+ }++ const alphaCanvas = target.ownerDocument.createElement('canvas');+ const alphaCtx = alphaCanvas.getContext('2d', {willReadFrequently: true});+ if (!alphaCtx) {+ throw new Error('Failed to acquire 2D context for outline extraction');+ }++ const polygonMaskCanvas = target.ownerDocument.createElement('canvas');+ polygonMaskCanvas.width = 1;+ polygonMaskCanvas.height = 1;+ const polygonMaskCtx = polygonMaskCanvas.getContext('2d');+ if (!polygonMaskCtx) {+ throw new Error('Failed to acquire 2D context for outline mask');+ }++ return {+ gl,+ program,+ vao,+ vbo,+ textureSource,+ texturePolygonMask,+ uniforms: {+ uSource: gl.getUniformLocation(program, 'uSource'),+ uPolygonMask: gl.getUniformLocation(program, 'uPolygonMask'),+ uUsePolygonMask: gl.getUniformLocation(program, 'uUsePolygonMask'),+ uWidth: gl.getUniformLocation(program, 'uWidth'),+ uColor: gl.getUniformLocation(program, 'uColor'),+ uOpacity: gl.getUniformLocation(program, 'uOpacity'),+ uOutlineOnly: gl.getUniformLocation(program, 'uOutlineOnly'),+ },+ alphaCanvas,+ alphaCtx,+ polygonMaskCanvas,+ polygonMaskCtx,+ colorCtx,+ cachedColor: '',+ cachedColorRgba: [255, 255, 255, 255],+ };+};++export const outline = createEffect<OutlineParams, OutlineState>({+ type: 'remotion/outline',+ label: 'outline()',+ documentationLink: 'https://www.remotion.dev/docs/effects/outline',+ backend: 'webgl2',+ calculateKey: (params) => {+ const resolved = resolve(params);+ return `outline-${resolved.width}-${resolved.edgeSimplification}-${resolved.color}-${resolved.opacity}-${resolved.outlineOnly}`;+ },+ setup: setupOutline,+ apply: ({source, width, height, params, state, flipSourceY}) => {+ const resolved = resolve(params);+ if (state.cachedColor !== resolved.color) {+ state.cachedColor = resolved.color;+ state.cachedColorRgba = parseColorRgba(state.colorCtx, resolved.color);+ }++ const {gl, program, textureSource, texturePolygonMask, uniforms, vao} =+ state;+ const [red, green, blue, alpha] = state.cachedColorRgba;+ const usePolygonMask =+ resolved.edgeSimplification > 0 &&+ resolved.opacity > 0 &&+ alpha > 0 &&+ (resolved.width > 0 || resolved.outlineOnly);+ if (usePolygonMask) {+ updatePolygonMask({+ source,+ width,+ height,+ simplification: resolved.edgeSimplification,+ outlineWidth: resolved.width,+ 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, textureSource);+ 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, texturePolygonMask);+ if (usePolygonMask) {+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);+ gl.texImage2D(+ gl.TEXTURE_2D,+ 0,+ gl.RGBA,+ gl.RGBA,+ gl.UNSIGNED_BYTE,+ state.polygonMaskCanvas,+ );+ }++ gl.useProgram(program);+ if (uniforms.uSource) gl.uniform1i(uniforms.uSource, 0);+ if (uniforms.uPolygonMask) gl.uniform1i(uniforms.uPolygonMask, 1);+ if (uniforms.uUsePolygonMask)+ gl.uniform1i(uniforms.uUsePolygonMask, usePolygonMask ? 1 : 0);+ if (uniforms.uWidth) gl.uniform1f(uniforms.uWidth, resolved.width);+ if (uniforms.uColor)+ gl.uniform4f(+ uniforms.uColor,+ red / 255,+ green / 255,+ blue / 255,+ alpha / 255,+ );+ if (uniforms.uOpacity) gl.uniform1f(uniforms.uOpacity, resolved.opacity);+ if (uniforms.uOutlineOnly)+ gl.uniform1i(uniforms.uOutlineOnly, resolved.outlineOnly ? 1 : 0);++ gl.bindVertexArray(vao);+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);+ gl.bindVertexArray(null);+ gl.activeTexture(gl.TEXTURE1);+ gl.bindTexture(gl.TEXTURE_2D, null);+ gl.activeTexture(gl.TEXTURE0);+ gl.bindTexture(gl.TEXTURE_2D, null);+ gl.useProgram(null);+ },+ cleanup: ({gl, program, vao, vbo, textureSource, texturePolygonMask}) => {+ gl.deleteTexture(textureSource);+ gl.deleteTexture(texturePolygonMask);+ gl.deleteBuffer(vbo);+ gl.deleteProgram(program);+ gl.deleteVertexArray(vao);+ },+ schema: outlineSchema,+ validateParams: validateOutlineParams,+});diff --git a/packages/effects/src/outline/polygonize-alpha.ts b/packages/effects/src/outline/polygonize-alpha.tsnew file mode 100644index 0000000000..0228320345--- /dev/null+++ b/packages/effects/src/outline/polygonize-alpha.ts@@ -0,0 +1,258 @@+export type OutlinePoint = readonly [x: number, y: number];++type EncodedPoint = readonly [x: number, y: number];++const pointKey = ([x, y]: EncodedPoint): string => `${x},${y}`;++const edgeKey = (a: string, b: string): string =>+ a < b ? `${a}|${b}` : `${b}|${a}`;++const squaredDistance = (a: OutlinePoint, b: OutlinePoint): number => {+ const dx = a[0] - b[0];+ const dy = a[1] - b[1];+ return dx * dx + dy * dy;+};++const squaredSegmentDistance = (+ point: OutlinePoint,+ start: OutlinePoint,+ end: OutlinePoint,+): number => {+ let x = start[0];+ let y = start[1];+ let dx = end[0] - x;+ let dy = end[1] - y;++ if (dx !== 0 || dy !== 0) {+ const progress =+ ((point[0] - x) * dx + (point[1] - y) * dy) / (dx * dx + dy * dy);+ if (progress > 1) {+ x = end[0];+ y = end[1];+ } else if (progress > 0) {+ x += dx * progress;+ y += dy * progress;+ }+ }++ dx = point[0] - x;+ dy = point[1] - y;+ return dx * dx + dy * dy;+};++const simplifyOpenContour = (+ points: readonly OutlinePoint[],+ toleranceSquared: number,+): OutlinePoint[] => {+ if (points.length <= 2) {+ return [...points];+ }++ const keep = new Uint8Array(points.length);+ keep[0] = 1;+ keep[points.length - 1] = 1;+ const stack: Array<readonly [number, number]> = [[0, points.length - 1]];++ while (stack.length > 0) {+ const range = stack.pop();+ if (!range) {+ break;+ }++ const [startIndex, endIndex] = range;+ let furthestIndex = -1;+ let furthestDistance = toleranceSquared;+ for (let index = startIndex + 1; index < endIndex; index++) {+ const distance = squaredSegmentDistance(+ points[index],+ points[startIndex],+ points[endIndex],+ );+ if (distance > furthestDistance) {+ furthestDistance = distance;+ furthestIndex = index;+ }+ }++ if (furthestIndex !== -1) {+ keep[furthestIndex] = 1;+ stack.push([startIndex, furthestIndex], [furthestIndex, endIndex]);+ }+ }++ return points.filter((_, index) => keep[index] === 1);+};++const simplifyClosedContour = (+ points: readonly OutlinePoint[],+ tolerance: number,+): OutlinePoint[] => {+ if (points.length <= 3 || tolerance <= 0) {+ return [...points];+ }++ let oppositeIndex = 1;+ let oppositeDistance = 0;+ for (let index = 1; index < points.length; index++) {+ const distance = squaredDistance(points[0], points[index]);+ if (distance > oppositeDistance) {+ oppositeDistance = distance;+ oppositeIndex = index;+ }+ }++ const firstHalf = points.slice(0, oppositeIndex + 1);+ const secondHalf = [...points.slice(oppositeIndex), points[0]];+ const toleranceSquared = tolerance * tolerance;+ const simplified = [+ ...simplifyOpenContour(firstHalf, toleranceSquared).slice(0, -1),+ ...simplifyOpenContour(secondHalf, toleranceSquared).slice(0, -1),+ ];++ return simplified.length >= 3 ? simplified : [...points];+};++export const polygonizeAlpha = ({+ data,+ width,+ height,+ simplification,+}: {+ readonly data: Uint8ClampedArray;+ readonly width: number;+ readonly height: number;+ readonly simplification: number;+}): OutlinePoint[][] => {+ const adjacency = new Map<string, string[]>();+ const points = new Map<string, EncodedPoint>();++ const addSegment = (a: EncodedPoint, b: EncodedPoint) => {+ const aKey = pointKey(a);+ const bKey = pointKey(b);+ points.set(aKey, a);+ points.set(bKey, b);+ const aNeighbors = adjacency.get(aKey);+ if (aNeighbors) {+ aNeighbors.push(bKey);+ } else {+ adjacency.set(aKey, [bKey]);+ }++ const bNeighbors = adjacency.get(bKey);+ if (bNeighbors) {+ bNeighbors.push(aKey);+ } else {+ adjacency.set(bKey, [aKey]);+ }+ };++ const isFilled = (x: number, y: number) =>+ x >= 0 &&+ y >= 0 &&+ x < width &&+ y < height &&+ data[(y * width + x) * 4 + 3] > 0;++ for (let y = -1; y < height; y++) {+ for (let x = -1; x < width; x++) {+ const configuration =+ (isFilled(x, y) ? 1 : 0) |+ (isFilled(x + 1, y) ? 2 : 0) |+ (isFilled(x + 1, y + 1) ? 4 : 0) |+ (isFilled(x, y + 1) ? 8 : 0);+ if (configuration === 0 || configuration === 15) {+ continue;+ }++ const top: EncodedPoint = [x * 2 + 2, y * 2 + 1];+ const right: EncodedPoint = [x * 2 + 3, y * 2 + 2];+ const bottom: EncodedPoint = [x * 2 + 2, y * 2 + 3];+ const left: EncodedPoint = [x * 2 + 1, y * 2 + 2];++ switch (configuration) {+ case 1:+ case 14:+ addSegment(top, left);+ break;+ case 2:+ case 13:+ addSegment(top, right);+ break;+ case 3:+ case 12:+ addSegment(left, right);+ break;+ case 4:+ case 11:+ addSegment(right, bottom);+ break;+ case 5:+ addSegment(top, left);+ addSegment(right, bottom);+ break;+ case 6:+ case 9:+ addSegment(top, bottom);+ break;+ case 7:+ case 8:+ addSegment(bottom, left);+ break;+ case 10:+ addSegment(top, right);+ addSegment(bottom, left);+ break;+ default:+ throw new Error(`Unexpected marching squares case: ${configuration}`);+ }+ }+ }++ const visitedEdges = new Set<string>();+ const contours: OutlinePoint[][] = [];+ for (const [start, neighbors] of adjacency) {+ for (const firstNeighbor of neighbors) {+ if (visitedEdges.has(edgeKey(start, firstNeighbor))) {+ continue;+ }++ const contour = [start];+ let previous = start;+ let current = firstNeighbor;+ visitedEdges.add(edgeKey(previous, current));++ while (current !== start) {+ contour.push(current);+ const candidates = adjacency.get(current) ?? [];+ const next = candidates.find(+ (candidate) =>+ candidate !== previous &&+ !visitedEdges.has(edgeKey(current, candidate)),+ );+ if (!next) {+ break;+ }++ previous = current;+ current = next;+ visitedEdges.add(edgeKey(previous, current));+ }++ if (current !== start || contour.length < 3) {+ continue;+ }++ const decoded = contour.map((key): OutlinePoint => {+ const point = points.get(key);+ if (!point) {+ throw new Error(`Missing outline point: ${key}`);+ }++ return [point[0] / 2, point[1] / 2];+ });+ contours.push(simplifyClosedContour(decoded, simplification));+ }+ }++ return contours;+};diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.tsindex 0968bfbbf4..affb1b7983 100644--- a/packages/effects/src/test/effect-params.test.ts+++ b/packages/effects/src/test/effect-params.test.ts@@ -43,6 +43,7 @@ import {type NoiseDisplacementParams,} from '../noise-displacement.js';import {noise} from '../noise.js';+import {outline} from '../outline.js';import {paper} from '../paper.js';import {pattern} from '../pattern.js';import {pixelDissolve} from '../pixel-dissolve.js';@@ -224,6 +225,9 @@ test('@remotion/effects expose documentation links', () => {noiseDisplacement({center: [0.5, 0.5], radius: 0.25}).definition.documentationLink,).toBe('https://www.remotion.dev/docs/effects/noise-displacement');+ expect(outline().definition.documentationLink).toBe(+ 'https://www.remotion.dev/docs/effects/outline',+ );expect(paper().definition.documentationLink).toBe('https://www.remotion.dev/docs/effects/paper',);@@ -363,6 +367,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(outline().definition.label).toBe('outline()');expect(noiseDisplacement({center: [0.5, 0.5], radius: 0.25}).definition.label,).toBe('noiseDisplacement()');@@ -626,6 +631,66 @@ test('colorKey() parameters produce distinct effect keys', () => {).toBe(5);});+test('outline() accepts default params', () => {+ expect(() => outline()).not.toThrow();+});++test('outline() rejects invalid width', () => {+ expect(() => outline({width: Number.NaN})).toThrow(+ '"width" must be a finite number',+ );+ expect(() => outline({width: -1})).toThrow('"width" must be >= 0');+});++test('outline() rejects invalid edgeSimplification', () => {+ expect(() => outline({edgeSimplification: Number.NaN})).toThrow(+ '"edgeSimplification" must be a finite number',+ );+ expect(() => outline({edgeSimplification: -1})).toThrow(+ '"edgeSimplification" must be >= 0',+ );+});++test('outline() rejects invalid color', () => {+ expect(() => outline({color: ''})).toThrow(+ '"color" must be a non-empty string',+ );+});++test('outline() rejects invalid opacity', () => {+ expect(() => outline({opacity: Number.NaN})).toThrow(+ '"opacity" must be a finite number',+ );+ expect(() => outline({opacity: -0.1})).toThrow('"opacity" must be >= 0');+ expect(() => outline({opacity: 1.1})).toThrow('"opacity" must be <= 1');+});++test('outline() rejects invalid outlineOnly', () => {+ expect(() => outline({outlineOnly: 'yes' as unknown as boolean})).toThrow(+ '"outlineOnly" must be a boolean',+ );+});++test('outline() parameters produce distinct effect keys', () => {+ const defaults = outline();+ const wider = outline({width: 16});+ const polygonal = outline({edgeSimplification: 8});+ const colored = outline({color: '#00ffff'});+ const transparent = outline({opacity: 0.5});+ const outlineOnly = outline({outlineOnly: true});++ expect(+ new Set([+ defaults.effectKey,+ wider.effectKey,+ polygonal.effectKey,+ colored.effectKey,+ transparent.effectKey,+ outlineOnly.effectKey,+ ]).size,+ ).toBe(6);+});+test('tint() throws when color is not passed', () => {expect(() => tint({} as Parameters<typeof tint>[0])).toThrow('"color" must be a non-empty string, but got undefined',diff --git a/packages/effects/src/visual-test/effects-visual.test.ts b/packages/effects/src/visual-test/effects-visual.test.tsindex e691426816..8ddf5bb008 100644--- a/packages/effects/src/visual-test/effects-visual.test.ts+++ b/packages/effects/src/visual-test/effects-visual.test.ts@@ -6,6 +6,7 @@ import {evolve} from '../evolve.js';import {exposure} from '../exposure.js';import {levels} from '../levels.js';import {noise} from '../noise.js';+import {outline} from '../outline.js';import {pixelDissolve} from '../pixel-dissolve.js';import {saturation} from '../saturation.js';import {scale} from '../scale.js';@@ -36,6 +37,128 @@ test('stacks repeated WebGL effects without blanking or flipping the image', asy});});+test('outline() draws around alpha while preserving the source', async () => {+ const width = 64;+ const height = 64;+ const source = document.createElement('canvas');+ source.width = width;+ source.height = height;+ const sourceContext = source.getContext('2d');+ if (!sourceContext) {+ throw new Error('Could not get source context');+ }++ sourceContext.fillStyle = 'white';+ sourceContext.fillRect(24, 24, 16, 16);+ sourceContext.fillStyle = 'rgba(255, 255, 255, 0.2)';+ sourceContext.fillRect(8, 48, 4, 4);++ const canvas = await renderEffectChainToCanvas({+ source,+ width,+ height,+ effects: descriptorsToMemoizedEffects([+ outline({width: 6, color: '#ff0000'}),+ ]),+ });+ const context = canvas.getContext('2d');+ if (!context) {+ throw new Error('Could not get output context');+ }++ const outlinePixel = context.getImageData(20, 32, 1, 1).data;+ expect(outlinePixel[0]).toBeGreaterThanOrEqual(250);+ expect(outlinePixel[1]).toBeLessThanOrEqual(5);+ expect(outlinePixel[2]).toBeLessThanOrEqual(5);+ expect(outlinePixel[3]).toBeGreaterThanOrEqual(250);++ const sourcePixel = context.getImageData(32, 32, 1, 1).data;+ expect([...sourcePixel]).toEqual([255, 255, 255, 255]);++ const transparentPixel = context.getImageData(8, 8, 1, 1).data;+ expect([...transparentPixel]).toEqual([0, 0, 0, 0]);++ const translucentSourceOutlinePixel = context.getImageData(4, 50, 1, 1).data;+ expect(translucentSourceOutlinePixel[0]).toBeGreaterThanOrEqual(250);+ expect(translucentSourceOutlinePixel[1]).toBeLessThanOrEqual(5);+ expect(translucentSourceOutlinePixel[2]).toBeLessThanOrEqual(5);+ expect(translucentSourceOutlinePixel[3]).toBeGreaterThanOrEqual(250);++ const smoothCornerPixel = context.getImageData(19, 19, 1, 1).data;+ expect(smoothCornerPixel[3]).toBeLessThanOrEqual(20);++ const polygonalCanvas = await renderEffectChainToCanvas({+ source,+ width,+ height,+ effects: descriptorsToMemoizedEffects([+ outline({width: 6, edgeSimplification: 8, color: '#ff0000'}),+ ]),+ });+ const polygonalContext = polygonalCanvas.getContext('2d');+ if (!polygonalContext) {+ throw new Error('Could not get polygonal outline output context');+ }++ const polygonalCornerPixel = polygonalContext.getImageData(19, 19, 1, 1).data;+ expect(polygonalCornerPixel[0]).toBeGreaterThanOrEqual(250);+ expect(polygonalCornerPixel[1]).toBeLessThanOrEqual(5);+ expect(polygonalCornerPixel[2]).toBeLessThanOrEqual(5);+ expect(polygonalCornerPixel[3]).toBeGreaterThanOrEqual(250);+ const polygonalSourcePixel = polygonalContext.getImageData(32, 32, 1, 1).data;+ expect([...polygonalSourcePixel]).toEqual([255, 255, 255, 255]);+ const polygonalPixels = polygonalContext.getImageData(+ 0,+ 0,+ width,+ height,+ ).data;+ const partiallyTransparentPolygonPixels = [];+ for (let index = 3; index < polygonalPixels.length; index += 4) {+ const alpha = polygonalPixels[index];+ if (alpha > 5 && alpha < 250) {+ partiallyTransparentPolygonPixels.push(alpha);+ }+ }++ expect(partiallyTransparentPolygonPixels).toEqual([]);++ const outlineOnlyCanvas = await renderEffectChainToCanvas({+ source,+ width,+ height,+ effects: descriptorsToMemoizedEffects([+ outline({+ width: 6,+ edgeSimplification: 8,+ color: '#ff0000',+ outlineOnly: true,+ }),+ ]),+ });+ const outlineOnlyContext = outlineOnlyCanvas.getContext('2d');+ if (!outlineOnlyContext) {+ throw new Error('Could not get outline-only output context');+ }++ const filledSourcePixel = outlineOnlyContext.getImageData(32, 32, 1, 1).data;+ expect([...filledSourcePixel]).toEqual([255, 0, 0, 255]);++ const filledOutlinePixel = outlineOnlyContext.getImageData(20, 32, 1, 1).data;+ expect(filledOutlinePixel[0]).toBeGreaterThanOrEqual(250);+ expect(filledOutlinePixel[1]).toBeLessThanOrEqual(5);+ expect(filledOutlinePixel[2]).toBeLessThanOrEqual(5);+ expect(filledOutlinePixel[3]).toBeGreaterThanOrEqual(250);++ const outlineOnlyTransparentPixel = outlineOnlyContext.getImageData(+ 8,+ 8,+ 1,+ 1,+ ).data;+ expect([...outlineOnlyTransparentPixel]).toEqual([0, 0, 0, 0]);+});+test('evolve() reveals with feather', async () => {const blob = await renderEffectChainToBlob({effects: descriptorsToMemoizedEffects([diff --git a/packages/studio-shared/src/effect-catalog.ts b/packages/studio-shared/src/effect-catalog.tsindex db694c2a42..d8c5a7f47b 100644--- a/packages/studio-shared/src/effect-catalog.ts+++ b/packages/studio-shared/src/effect-catalog.ts@@ -345,6 +345,20 @@ export const EFFECT_CATALOG: readonly EffectCatalogItem[] = [config: {},},},+ {+ id: 'effects-outline',+ category: 'Blur & Shadow',+ label: 'outline()',+ description: 'Solid alpha outline effect',+ effect: {+ name: 'outline',+ importPath: '@remotion/effects/outline',+ config: {+ width: 12,+ color: '#00ffff',+ },+ },+ },{id: 'effects-light-trail',category: 'Blur & Shadow',