From b60b1723a3493234753eb20752c32f7cadcf837f Mon Sep 17 00:00:00 2001 From: Dariusz L Date: Wed, 25 Jun 2025 23:26:54 +0200 Subject: [PATCH] Replace selected layer with matted image result Instead of adding a new layer after matting, this change replaces the selected image layer with the matted result and removes the old imageId to ensure the new image is saved. This prevents layer duplication and maintains the correct layer order. --- js/Canvas_view.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/js/Canvas_view.js b/js/Canvas_view.js index 09e057d..e2c504b 100644 --- a/js/Canvas_view.js +++ b/js/Canvas_view.js @@ -570,6 +570,7 @@ async function createCanvasWidget(node, widget, app) { if (canvas.selectedLayers.length !== 1) throw new Error("Please select exactly one image layer for matting."); const selectedLayer = canvas.selectedLayers[0]; + const selectedLayerIndex = canvas.layers.indexOf(selectedLayer); const imageData = await canvas.getLayerImageData(selectedLayer); const response = await fetch("/matting", { method: "POST", @@ -584,8 +585,13 @@ async function createCanvasWidget(node, widget, app) { mattedImage.src = result.matted_image; await mattedImage.decode(); - const newLayer = {...selectedLayer, image: mattedImage, zIndex: canvas.layers.length}; - canvas.layers.push(newLayer); + // Zastąp starą warstwę nową warstwą z obrazem bez tła + const newLayer = {...selectedLayer, image: mattedImage}; + // Usuń starą imageId, aby wymusić zapisanie nowego obrazu + delete newLayer.imageId; + + // Zastąp warstwę w tablicy zamiast dodawać nową + canvas.layers[selectedLayerIndex] = newLayer; canvas.updateSelection([newLayer]); canvas.render(); canvas.saveState();