Fix matting: refresh image after background removal

Fixed an issue where images were not immediately refreshed after background removal (matting). Now, the canvas updates instantly when the background is removed, ensuring correct display without requiring manual scaling or other actions.
This commit is contained in:
Dariusz L
2025-08-03 22:14:39 +02:00
parent 3c3e6934d7
commit 3d34bfafd5
4 changed files with 18 additions and 20 deletions

View File

@@ -961,17 +961,13 @@ export class CanvasLayers {
}
async getLayerImageData(layer) {
try {
const { canvas: tempCanvas, ctx: tempCtx } = createCanvas(layer.width, layer.height, '2d', { willReadFrequently: true });
const width = layer.originalWidth || layer.width;
const height = layer.originalHeight || layer.height;
const { canvas: tempCanvas, ctx: tempCtx } = createCanvas(width, height, '2d', { willReadFrequently: true });
if (!tempCtx)
throw new Error("Could not create canvas context");
// We need to draw the layer relative to the new canvas, so we "move" it to 0,0
// by creating a temporary layer object for drawing.
const layerToDraw = {
...layer,
x: 0,
y: 0,
};
this._drawLayer(tempCtx, layerToDraw);
// Use original image directly to ensure full quality
tempCtx.drawImage(layer.image, 0, 0, width, height);
const dataUrl = tempCanvas.toDataURL('image/png');
if (!dataUrl.startsWith('data:image/png;base64,')) {
throw new Error("Invalid image data format");

View File

@@ -430,6 +430,8 @@ async function createCanvasWidget(node, widget, app) {
delete newLayer.imageId;
canvas.layers[selectedLayerIndex] = newLayer;
canvas.canvasSelection.updateSelection([newLayer]);
// Invalidate processed image cache when layer image changes (matting)
canvas.canvasLayers.invalidateProcessedImageCache(newLayer.id);
canvas.render();
canvas.saveState();
showSuccessNotification("Background removed successfully!");