Chroma key
Remove green or blue screen backgrounds from videos in real time. Set chromaKey: true for zero-config green screen removal — no color values to configure. The subject renders over a transparent background.
Live preview
Green screen pixels are removed — subject renders over a transparent background.
{ chromaKey: true }Green background renders as-is — no keying applied.
Usage
import { asciifyVideo } from 'asciify-engine';
// Zero-config green screen — heuristic, works on any shade of green
asciifyVideo('/greenscreen.mp4', canvas, {
options: { chromaKey: true },
});
// Blue screen
asciifyVideo('/bluescreen.mp4', canvas, {
options: { chromaKey: 'blue-screen' },
});
// Custom color — any CSS string
asciifyVideo('/clip.mp4', canvas, {
options: {
chromaKey: '#00b140',
chromaKeyTolerance: 80, // Euclidean distance, default 60
},
});
// Custom color as RGB object
asciifyVideo('/clip.mp4', canvas, {
options: {
chromaKey: { r: 0, g: 177, b: 64 },
chromaKeyTolerance: 80,
},
});How it works
Each pixel is evaluated per frame before the character is chosen:
chromaKey: trueHeuristic: green channel dominant — g > r × 1.4 and g > b × 1.4 and g > 80. Shade-agnostic, works on lime, chroma, broadcast green, etc.
chromaKey: 'blue-screen'Symmetric heuristic for blue: b > r × 1.4 and b > g × 1.4 and b > 80.
chromaKey: "#hex" | {r,g,b}Keyed by Euclidean RGB distance from the reference color. Pixels within chromaKeyTolerance radius are removed.
chromaKey: null / falseDisabled (default).
Tolerance guide
chromaKeyTolerance applies to a custom key colour only; true and 'blue-screen' use the fixed heuristics above and ignore it.
40–60Tight key: an evenly lit, natural green screen.
60–80Broader key: wrinkled fabric or uneven lighting.
80–120Aggressive: expect some spill into the subject.
Keyed auto-trim
With chromaKey and sourceCrop both set, asciifyVideo trims the keyed empty margins inside the crop before sizing the layout, so a green-screen subject fills a wide hero without CSS hacks or per-frame zoom. For trimmed or scroll-scrubbed video it samples the playback range and keeps the union of the foreground bounds.
await asciifyVideo('/presenter.mp4', canvas, {
fitTo: '#hero',
scroll: true,
options: {
chromaKey: true,
sourceCrop: { top: 0.05, bottom: 0.05 }, // the trim runs inside this window
chromaKeyTrimMode: 'frame', // follow a subject that moves
chromaKeyTrimPadding: 0, // edge-tight
chromaKeyTrimLuminanceThreshold: 12, // ignore faint edge noise
},
});| Option | Type | Default | Description |
|---|---|---|---|
chromaKeyTrimMode | 'range' | 'frame' | 'off' | 'range' | range keeps one stable crop for the playback range. frame remeasures each rendered frame and expands back to the render aspect, for a subject that moves across a scrubbed hero; its scale can visibly change. off disables the trim. |
chromaKeyTrimPadding | number | 0.002 | Normalized safety padding around the detected foreground. 0 hugs the subject; 0.01 keeps 1%. |
chromaKeyTrimLuminanceThreshold | 0–255 | 0 | Minimum source luminance counted as foreground. Raise it slightly when invisible edge noise stops the subject filling a preserved-ratio layout. |
Options reference
options: {
chromaKey?: true | 'blue-screen' | string | { r: number; g: number; b: number } | null;
chromaKeyTolerance?: number; // custom colours only — default: 60
chromaKeyTrimMode?: 'range' | 'frame' | 'off'; // default: 'range'
chromaKeyTrimPadding?: number; // default: 0.002
chromaKeyTrimLuminanceThreshold?: number; // 0–255, default: 0
}