Skip to content

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

PropertyTypeDefaultDescription
enablebooleanfalseEnables background mask mode
compositeGlobalCompositeOperation"destination-out"Canvas composite operation
coverBackgroundMaskCover{ opacity: 1 }Cover configuration

cover (BackgroundMaskCover)

PropertyTypeDefaultDescription
colorstring / OptionsColorundefinedCover color
imagestringundefinedCover image URL
opacitynumber1Cover alpha (0..1)
elementstring / HTMLCanvasElement / OffscreenCanvas / HTMLVideoElement / HTMLImageElementundefinedExternal element or CSS selector auto-drawn each frame (since 4.3.0)
draw(context: BackgroundDrawContext, delta: IDelta) => voidundefinedCustom 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)

  1. clear() — canvas pixel clear
  2. cover.element auto-draw (if set)
  3. cover.draw callback (if set)
  4. Static cover (color/image) — fallback, only if neither element nor draw is set
  5. Global composite operation

Notes

  • Use cover.image to use an image as the mask cover.
  • When cover.draw or cover.element is set, the static cover (color/image) is skipped.
  • If none of color, image, element, draw are set, the cover is transparent.
  • The plugin extends the built-in backgroundMask with additional compositing features.

Source reference