Skip to content

Background Mask

backgroundMask lets particles punch through or blend with a masked background layer.

Examples

Static cover (legacy)

ts
backgroundMask: {
  enable: true,
  cover: {
    color: {
      value: "#0b1020",
    },
    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

PropertyTypeDescription
enablebooleanActivates background masking
compositeGlobalCompositeOperationCanvas composite operation (default: "destination-out")
coverBackgroundMaskCoverCover configuration

cover (BackgroundMaskCover)

PropertyTypeDescription
colorstring / OptionsColorCover color
imagestringCover image URL
opacitynumberCover alpha level (0..1, default: 1)
elementstring / HTMLCanvasElement / OffscreenCanvas / HTMLVideoElement / HTMLImageElementExternal element or CSS selector auto-drawn each frame (since 4.3.0)
draw(context: BackgroundDrawContext, delta: IDelta) => voidCustom draw callback on main canvas context each frame (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
  5. Global composite operation

When to use it

  • Spotlight-like effects.
  • Contrast-heavy hero sections.
  • Layered interactions on dark backgrounds.

Source reference