cutImage()

in src/nodes/shared/image-segmentation-viewer.ts [134:160]


  cutImage(layers: Set<string>) {
    const canvas = document.createElement("canvas");
    canvas.width = this.inputImage.width;
    canvas.height = this.inputImage.height;
    const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;

    ctx.save();
    ctx.drawImage(this.inputImage, 0, 0);

    const tempCanvas = document.createElement("canvas");
    const tempCtx = tempCanvas.getContext("2d") as CanvasRenderingContext2D;
    tempCanvas.width = this.inputImage.width;
    tempCanvas.height = this.inputImage.height;

    const masks = this.masks.filter((mask) =>
      layers.has(mask.getAttribute("data-label") as string)
    );

    for (const mask of masks) {
      tempCtx.drawImage(mask, 0, 0);
    }

    ctx.globalCompositeOperation = "destination-in";
    ctx.drawImage(tempCanvas, 0, 0);
    ctx.restore();
    return canvas;
  }