The backgroundMask plugin lets you draw a cover or mask over the canvas using compositing operations. Useful for hiding/masking parts of the rendering or creating erasing effects.
Core properties
composite (GlobalCompositeOperation) — Default: "destination-out". The compositing operation applied when the cover/mask is drawn. Possible values are standard Canvas 2D globalCompositeOperation.cover (BackgroundMaskCover) — Default: object with opacity: 1. Defines the cover to draw (color/image/element/draw/opacity).enable (boolean) — Default: false. Enables masking behavior.BackgroundMaskCover (sub-object)
color (OptionsColor | undefined) — Default: undefined. Color used for the cover. Created via OptionsColor.create when provided.image (string | undefined) — Default: undefined. URL of the image to use as the cover.element (string | HTMLCanvasElement | OffscreenCanvas | HTMLVideoElement | HTMLImageElement | undefined) — Default: undefined. External element or CSS selector auto-drawn each frame as the mask source (since 4.3).draw ((context: BackgroundDrawContext, delta: IDelta) => void | undefined) — Default: undefined. Custom callback executed each frame on the main canvas context; delta is a fallback { value: 0, factor: 1 } (since 4.3).opacity (number) — Default: 1. Cover opacity (0..1).Layer order (since 4.3)
clear() — canvas pixel clearcover.element auto-draw — ctx.drawImage() of the external element (if set)cover.draw callback — custom drawing on the main context (if set)drawSettingsSetupStatic (legacy) config
{
"backgroundMask": {
"enable": true,
"composite": "destination-out",
"cover": {
"color": { "value": "#000000" },
"opacity": 1
}
}
}
Dynamic draw callback (since 4.3)
{
backgroundMask: {
enable: true,
cover: {
draw: (ctx) => {
const t = performance.now() * 0.001;
ctx.fillStyle = `hsl(${(t * 30) % 360}, 70%, 50%)`;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
},
},
},
}
External element (since 4.3)
{
"backgroundMask": {
"enable": true,
"cover": {
"element": "#myVideo",
"opacity": 0.8
}
}
}
Element resolution rules (since 4.3)
document.querySelector():
mask-element-not-supportedmask-element-not-foundundefined/null → skipped, static cover used| Key | Message | Condition |
|---|---|---|
mask-element-not-found |
Mask cover element selector "..." not found in the DOM |
CSS selector didn't match any element |
mask-element-not-supported |
Mask cover element "..." matched a non-drawable element |
Selector matched non-drawable element |
mask-element-draw-error |
Error drawing background mask cover element onto canvas |
ctx.drawImage() threw |
mask-draw-error |
Error in mask cover.draw callback |
cover.draw callback threw |
Usage notes
cover is provided as a string it is treated as a color (e.g. "#000000").cover.image with the URL and adjust composite if needed.cover.draw or cover.element, the static fallback (color/image) is skipped automatically.color, image, element, or draw should be set; if none are present the cover is transparent.plugins/backgroundMask/src/Options/Classes/BackgroundMask.ts and BackgroundMaskCover.ts.