Skip to content

🎥 Make videos programmatically with React

@remotion/effects: Promote halftone() to public API

Jonny Burgerf6dd5d362515
diff --git a/packages/docs/docs/effects/halftone.mdx b/packages/docs/docs/effects/halftone.mdx
new file mode 100644
index 0000000000..dc38b367a3
--- /dev/null
+++ b/packages/docs/docs/effects/halftone.mdx
@@ -0,0 +1,103 @@
+---
+image: /generated/articles-docs-effects-halftone.png
+slug: /effects/halftone
+title: halftone()
+sidebar_label: halftone()
+crumb: '@remotion/effects'
+---
+
+# halftone()<AvailableFrom v="4.0.466" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+A halftone effect that turns luminance into a grid of circles, squares, or lines on canvas-based components such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) and [`<Solid>`](/docs/solid).
+
+Use it when you want a printed-dot look while still controlling effect order through the `effects` array.
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill} from 'remotion';
+import {Video} from '@remotion/media';
+import {halftone} from '@remotion/effects/halftone';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Video
+ src="https://remotion.media/video.mp4"
+ effects={[
+ halftone({
+ shape: 'circle',
+ dotSize: 18,
+ dotSpacing: 20,
+ rotation: 12,
+ color: '#111827',
+ }),
+ ]}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+## API
+
+Pass an object with the following properties. You can also call `halftone()` without arguments.
+
+### `shape?`
+
+Halftone primitive. Defaults to `'circle'`.
+
+Allowed values are `'circle'`, `'square'`, and `'line'`.
+
+### `dotSize?`
+
+Maximum size of each dot, square, or line in pixels. Defaults to `20`. Must be at least `1`.
+
+### `dotSpacing?`
+
+Distance between adjacent grid centers in pixels. Defaults to the value of `dotSize`. Must be at least `1`.
+
+Increase this value beyond `dotSize` to create more negative space between dots.
+
+### `rotation?`
+
+Rotation of the grid in degrees. Defaults to `0`.
+
+### `offsetX?`
+
+Horizontal grid offset in pixels. Defaults to `0`.
+
+### `offsetY?`
+
+Vertical grid offset in pixels. Defaults to `0`.
+
+### `sampling?`
+
+Texture sampling mode used when reading luminance from the source. Defaults to `'bilinear'`.
+
+Allowed values are `'bilinear'` and `'nearest'`.
+
+### `color?`
+
+Color of the dots. Defaults to `'black'`.
+
+### `invert?`
+
+When `false`, darker areas produce larger dots. When `true`, brighter and transparent areas produce larger dots. Defaults to `false`.
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`.
+
+## Requirements
+
+`halftone()` 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)
+- [`grayscale()`](/docs/effects/grayscale)
+- [HTML-in-canvas guide](/docs/html-in-canvas)
+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/halftone.ts)
diff --git a/packages/docs/docs/effects/table-of-contents.tsx b/packages/docs/docs/effects/table-of-contents.tsx
index a2fdb65cf5..0d5859449d 100644
--- a/packages/docs/docs/effects/table-of-contents.tsx
+++ b/packages/docs/docs/effects/table-of-contents.tsx
@@ -65,6 +65,21 @@ export const TableOfContents: React.FC = () => {
</div>
</div>
</TOCItem>
+ <TOCItem link="/docs/effects/halftone">
+ <div style={row}>
+ <img
+ src="/img/effects-halftone-preview.jpg"
+ alt="halftone effect preview"
+ style={previewImage}
+ />
+ <div style={{flex: 1, marginLeft: 10}}>
+ <strong>
+ <code>{'halftone()'}</code>
+ </strong>
+ <div>Halftone dot grid effect</div>
+ </div>
+ </div>
+ </TOCItem>
<TOCItem link="/docs/effects/invert">
<div style={row}>
<img
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index eaccfaa4b3..4867a0d923 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -270,6 +270,7 @@ const sidebars: SidebarsConfig = {
'effects/blur',
'effects/brightness',
'effects/grayscale',
+ 'effects/halftone',
'effects/hue',
'effects/invert',
'effects/scale',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index bc99af745e..99ec816ed4 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1736,6 +1736,15 @@ export const articles = [
noAi: false,
slug: 'effects/grayscale',
},
+ {
+ id: 'effects/halftone',
+ title: 'halftone()',
+ relativePath: 'docs/effects/halftone.mdx',
+ compId: 'articles-docs-effects-halftone',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/halftone',
+ },
{
id: 'effects/hue',
title: 'hue()',
diff --git a/packages/docs/static/generated/articles-docs-effects-halftone.png b/packages/docs/static/generated/articles-docs-effects-halftone.png
new file mode 100644
index 0000000000..186c3b7740
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-halftone.png differ
diff --git a/packages/docs/static/img/effects-halftone-preview.jpg b/packages/docs/static/img/effects-halftone-preview.jpg
new file mode 100644
index 0000000000..ba16d89e1a
Binary files /dev/null and b/packages/docs/static/img/effects-halftone-preview.jpg differ
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 5dc7c50f86..1c0055d4f2 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -10,6 +10,7 @@ const effectEntrypoints = [
'src/blur.ts',
'src/brightness.ts',
'src/grayscale.ts',
+ 'src/halftone.ts',
'src/hue.ts',
'src/invert.ts',
'src/scale.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index b6f7673c72..4a899e9759 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -42,6 +42,11 @@
"module": "./dist/esm/brightness.mjs",
"import": "./dist/esm/brightness.mjs"
},
+ "./halftone": {
+ "types": "./dist/halftone.d.ts",
+ "module": "./dist/esm/halftone.mjs",
+ "import": "./dist/esm/halftone.mjs"
+ },
"./grayscale": {
"types": "./dist/grayscale.d.ts",
"module": "./dist/esm/grayscale.mjs",
@@ -77,6 +82,9 @@
"brightness": [
"dist/brightness.d.ts"
],
+ "halftone": [
+ "dist/halftone.d.ts"
+ ],
"grayscale": [
"dist/grayscale.d.ts"
],
diff --git a/packages/effects/src/halftone.ts b/packages/effects/src/halftone.ts
index fc539f278c..b427a26fc8 100644
--- a/packages/effects/src/halftone.ts
+++ b/packages/effects/src/halftone.ts
@@ -1,7 +1,11 @@
import type {SequenceSchema} from 'remotion';
import {Internals} from 'remotion';
+import {assertOptionalFiniteNumber} from './color-utils.js';
+import {assertEffectParamsObject} from './validate-effect-param.js';
const {createEffect, createWebGL2ContextError} = Internals;
+const HALFTONE_SHAPES = ['circle', 'square', 'line'] as const;
+const HALFTONE_SAMPLING = ['bilinear', 'nearest'] as const;
export const halftoneSchema = {
dotSize: {
@@ -55,10 +59,15 @@ export const halftoneSchema = {
default: false,
description: 'Invert',
},
+ color: {
+ type: 'color',
+ default: 'black',
+ description: 'Color',
+ },
} as const satisfies SequenceSchema;
-export type HalftoneShape = 'circle' | 'square' | 'line';
-export type HalftoneSampling = 'bilinear' | 'nearest';
+export type HalftoneShape = (typeof HALFTONE_SHAPES)[number];
+export type HalftoneSampling = (typeof HALFTONE_SAMPLING)[number];
export type HalftoneParams = {
readonly shape?: HalftoneShape;
@@ -94,6 +103,55 @@ type HalftoneResolved = {
invert: boolean;
};
+const formatEnum = (variants: readonly string[]): string => {
+ if (variants.length === 2) {
+ return `"${variants[0]}" or "${variants[1]}"`;
+ }
+
+ return `${variants
+ .slice(0, -1)
+ .map((variant) => `"${variant}"`)
+ .join(', ')} or "${variants[variants.length - 1]}"`;
+};
+
+const assertOptionalEnum = <T extends string>(
+ value: unknown,
+ name: string,
+ variants: readonly T[],
+): void => {
+ if (value === undefined) {
+ return;
+ }
+
+ if (typeof value !== 'string' || !variants.includes(value as T)) {
+ throw new TypeError(`"${name}" must be ${formatEnum(variants)}`);
+ }
+};
+
+const assertOptionalBoolean = (value: unknown, name: string): void => {
+ if (value === undefined) {
+ return;
+ }
+
+ if (typeof value !== 'boolean') {
+ throw new TypeError(
+ `"${name}" must be a boolean, but got ${JSON.stringify(value)}`,
+ );
+ }
+};
+
+const assertOptionalColor = (value: unknown, name: string): void => {
+ if (value === undefined) {
+ return;
+ }
+
+ if (typeof value !== 'string' || value.length === 0) {
+ throw new TypeError(
+ `"${name}" must be a non-empty string, but got ${JSON.stringify(value)}`,
+ );
+ }
+};
+
const resolve = (p: HalftoneParams): HalftoneResolved => ({
shape: p.shape ?? 'circle',
dotSize: p.dotSize ?? 20,
@@ -106,6 +164,31 @@ const resolve = (p: HalftoneParams): HalftoneResolved => ({
invert: p.invert ?? false,
});
+const validateHalftoneParams = (params: HalftoneParams): void => {
+ assertEffectParamsObject(params, 'Halftone');
+ assertOptionalFiniteNumber(params.dotSize, 'dotSize');
+ assertOptionalFiniteNumber(params.dotSpacing, 'dotSpacing');
+ assertOptionalFiniteNumber(params.rotation, 'rotation');
+ assertOptionalFiniteNumber(params.offsetX, 'offsetX');
+ assertOptionalFiniteNumber(params.offsetY, 'offsetY');
+ assertOptionalEnum(params.shape, 'shape', HALFTONE_SHAPES);
+ assertOptionalEnum(params.sampling, 'sampling', HALFTONE_SAMPLING);
+ assertOptionalColor(params.color, 'color');
+ assertOptionalBoolean(params.invert, 'invert');
+
+ if (params.dotSize !== undefined && params.dotSize < 1) {
+ throw new TypeError(
+ `"dotSize" must be >= 1, but got ${JSON.stringify(params.dotSize)}`,
+ );
+ }
+
+ if (params.dotSpacing !== undefined && params.dotSpacing < 1) {
+ throw new TypeError(
+ `"dotSpacing" must be >= 1, but got ${JSON.stringify(params.dotSpacing)}`,
+ );
+ }
+};
+
const HALFTONE_VS = /* glsl */ `#version 300 es
in vec2 aPos;
in vec2 aUv;
@@ -430,5 +513,5 @@ export const halftone = createEffect<HalftoneParams, HalftoneState>({
gl.deleteTexture(texture);
},
schema: halftoneSchema,
- validateParams: () => {},
+ validateParams: validateHalftoneParams,
});
diff --git a/packages/effects/src/index.ts b/packages/effects/src/index.ts
index 2aa897e592..ac0d3fd852 100644
--- a/packages/effects/src/index.ts
+++ b/packages/effects/src/index.ts
@@ -1,3 +1,9 @@
export {EffectInternals} from './effect-internals.js';
-export {halftoneSchema, type HalftoneParams} from './halftone.js';
+export {
+ halftone,
+ halftoneSchema,
+ type HalftoneParams,
+ type HalftoneSampling,
+ type HalftoneShape,
+} from './halftone.js';
export {tintSchema, type TintParams} from './tint.js';
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 425491abe4..10d5d0a6cf 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -2,6 +2,7 @@ import {expect, test} from 'bun:test';
import {blur} from '../blur/index.js';
import {brightness} from '../brightness.js';
import {grayscale} from '../grayscale.js';
+import {halftone} from '../halftone.js';
import {hue} from '../hue.js';
import {invert} from '../invert.js';
import {scale} from '../scale.js';
@@ -136,6 +137,42 @@ test('brightness() amount produces distinct effect keys', () => {
).toBe(3);
});
+test('halftone() accepts default params', () => {
+ expect(() => halftone()).not.toThrow();
+});
+
+test('halftone() rejects shape outside the enum', () => {
+ expect(() =>
+ halftone({
+ shape: 'triangle' as Exclude<
+ Parameters<typeof halftone>[0],
+ undefined
+ >['shape'],
+ }),
+ ).toThrow('"shape" must be "circle", "square" or "line"');
+});
+
+test('halftone() rejects sampling outside the enum', () => {
+ expect(() =>
+ halftone({
+ sampling: 'cubic' as Exclude<
+ Parameters<typeof halftone>[0],
+ undefined
+ >['sampling'],
+ }),
+ ).toThrow('"sampling" must be "bilinear" or "nearest"');
+});
+
+test('halftone() rejects dotSize below range', () => {
+ expect(() => halftone({dotSize: 0})).toThrow('"dotSize" must be >= 1');
+});
+
+test('halftone() rejects empty color strings', () => {
+ expect(() => halftone({color: ''})).toThrow(
+ '"color" must be a non-empty string, but got ""',
+ );
+});
+
test('invert() accepts default params', () => {
expect(() => invert()).not.toThrow();
});
diff --git a/packages/example/src/HtmlInCanvas/effects-halftone-preview.tsx b/packages/example/src/HtmlInCanvas/effects-halftone-preview.tsx
new file mode 100644
index 0000000000..0a7fa2cffb
--- /dev/null
+++ b/packages/example/src/HtmlInCanvas/effects-halftone-preview.tsx
@@ -0,0 +1,26 @@
+import {halftone} from '@remotion/effects/halftone';
+import React from 'react';
+import {HtmlInCanvas, useVideoConfig} from 'remotion';
+import {EffectsPreviewImage} from './effects-preview-image';
+
+export const EffectsHalftonePreview: React.FC = () => {
+ const {width, height} = useVideoConfig();
+
+ return (
+ <HtmlInCanvas
+ width={width}
+ height={height}
+ effects={[
+ halftone({
+ shape: 'circle',
+ dotSize: 8,
+ dotSpacing: 7,
+ rotation: 12,
+ color: '#111827',
+ }),
+ ]}
+ >
+ <EffectsPreviewImage />
+ </HtmlInCanvas>
+ );
+};
diff --git a/packages/example/src/HtmlInCanvas/index.tsx b/packages/example/src/HtmlInCanvas/index.tsx
index a6194fbb70..97ca5b9ece 100644
--- a/packages/example/src/HtmlInCanvas/index.tsx
+++ b/packages/example/src/HtmlInCanvas/index.tsx
@@ -19,6 +19,7 @@ export {HtmlInCanvasDocsDemo2DBlur} from './docs-demo-2d-blur';
export {EffectsBlurPreview} from './effects-blur-preview';
export {EffectsBrightnessPreview} from './effects-brightness-preview';
export {EffectsGrayscalePreview} from './effects-grayscale-preview';
+export {EffectsHalftonePreview} from './effects-halftone-preview';
export {EffectsHuePreview} from './effects-hue-preview';
export {EffectsInvertPreview} from './effects-invert-preview';
export {EffectsWavePreview} from './effects-wave-preview';
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index fe0f8805de..9103308e77 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -55,6 +55,7 @@ import {
EffectsBlurPreview,
EffectsBrightnessPreview,
EffectsGrayscalePreview,
+ EffectsHalftonePreview,
EffectsHuePreview,
EffectsInvertPreview,
EffectsWavePreview,
@@ -1049,6 +1050,14 @@ export const Index: React.FC = () => {
width={1080}
durationInFrames={1}
/>
+ <Composition
+ id="html-in-canvas-effects-halftone-preview"
+ component={EffectsHalftonePreview}
+ fps={30}
+ height={720}
+ width={1080}
+ durationInFrames={2}
+ />
<Composition
id="html-in-canvas-effects-hue-preview"
component={EffectsHuePreview}
diff --git a/packages/example/src/webpack-override.mjs b/packages/example/src/webpack-override.mjs
index 97cc9c1b2e..00933bcbe9 100644
--- a/packages/example/src/webpack-override.mjs
+++ b/packages/example/src/webpack-override.mjs
@@ -62,6 +62,10 @@ const aliases = {
process.cwd(),
'../effects/dist/esm/wave.mjs',
),
+ '@remotion/effects/halftone': path.join(
+ process.cwd(),
+ '../effects/dist/esm/halftone.mjs',
+ ),
};
/**