Plugin Option: Background Mask
backgroundMask draws a cover or mask over the canvas using compositing operations.
Examples
Static cover (legacy)
ts
backgroundMask: {
enable: true,
composite: "destination-out",
cover: {
color: { value: "#000000" },
opacity: 1,
},
}Dynamic draw callback (since 4.3.0)
ts
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.0)
ts
backgroundMask: {
enable: true,
cover: {
element: "#myVideo",
opacity: 0.8,
},
}Properties
| Property | Type | Default | Description |
|---|---|---|---|
enable | boolean | false | Enables background mask mode |
composite | GlobalCompositeOperation | "destination-out" | Canvas composite operation |
cover | BackgroundMaskCover | { opacity: 1 } | Cover configuration |
cover (BackgroundMaskCover)
| Property | Type | Default | Description |
|---|---|---|---|
color | string / OptionsColor | undefined | Cover color |
image | string | undefined | Cover image URL |
opacity | number | 1 | Cover alpha (0..1) |
element | string / HTMLCanvasElement / OffscreenCanvas / HTMLVideoElement / HTMLImageElement | undefined | External element or CSS selector auto-drawn each frame (since 4.3.0) |
draw | (context: BackgroundDrawContext, delta: IDelta) => void | undefined | Custom draw callback on main canvas context each frame; delta is a fallback { value: 0, factor: 1 } (since 4.3.0) |
Layer order (since 4.3.0)
clear()— canvas pixel clearcover.elementauto-draw (if set)cover.drawcallback (if set)- Static cover (color/image) — fallback, only if neither element nor draw is set
- Global composite operation
Notes
- Use
cover.imageto use an image as the mask cover. - When
cover.draworcover.elementis set, the static cover (color/image) is skipped. - If none of
color,image,element,draware set, the cover is transparent. - The plugin extends the built-in
backgroundMaskwith additional compositing features.
