From 0bb54a0a6d97c5d384e1ec00cdf531d6bf4a9bfd Mon Sep 17 00:00:00 2001 From: Dariusz L Date: Sun, 29 Jun 2025 20:56:58 +0200 Subject: [PATCH] Refactor image layer addition to use addLayer method Replaces manual image layer creation with calls to canvas.addLayer, passing options and addMode. This streamlines image addition logic and ensures consistent handling of layer properties. --- js/CanvasView.js | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/js/CanvasView.js b/js/CanvasView.js index 6f77708..8d73959 100644 --- a/js/CanvasView.js +++ b/js/CanvasView.js @@ -539,7 +539,7 @@ async function createCanvasWidget(node, widget, app) { reader.onload = (event) => { const img = new Image(); img.onload = () => { - canvas.addLayer(img, addMode); + canvas.addLayer(img, {}, addMode); }; img.src = event.target.result; }; @@ -968,27 +968,10 @@ async function createCanvasWidget(node, widget, app) { const img = new Image(); img.onload = async () => { log.debug("Image object loaded from dropped data:URL."); - const scale = Math.min( - canvas.width / img.width, - canvas.height / img.height - ); - - const layer = { - image: img, - x: (canvas.width - img.width * scale) / 2, - y: (canvas.height - img.height * scale) / 2, - width: img.width * scale, - height: img.height * scale, - rotation: 0, - zIndex: canvas.layers.length, - blendMode: 'normal', - opacity: 1 - }; - - canvas.layers.push(layer); - canvas.updateSelection([layer]); - canvas.render(); - canvas.saveState(); + const fitOnAddWidget = node.widgets.find(w => w.name === "fit_on_add"); + const addMode = fitOnAddWidget && fitOnAddWidget.value ? 'fit' : 'center'; + + await canvas.addLayer(img, {}, addMode); log.info("Dropped layer added and state saved."); }; img.src = event.target.result;