diff --git a/js/RegionalPrompting.js b/js/RegionalPrompting.js index 003bc44..ae45d0a 100644 --- a/js/RegionalPrompting.js +++ b/js/RegionalPrompting.js @@ -3,7 +3,7 @@ // All-in-one nodes with CLIP input and prompt boxes import { app } from "/scripts/app.js"; -import {CUSTOM_INT, transformFunc, swapInputs, renameNodeInputs, removeNodeInputs, getDrawColor, computeCanvasSize} from "./utils.js" +import {CUSTOM_INT, transformFunc, swapInputs, renameNodeInputs, removeNodeInputs, getDrawColor, computeCanvasSize, calculateDefaultRegions} from "./utils.js" // Shared canvas function for both enhanced nodes function addEasyRegionCanvas(node, app) { @@ -220,15 +220,12 @@ app.registerExtension({ nodeType.prototype.onNodeCreated = function () { const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined; - this.setProperty("width", 512) - this.setProperty("height", 512) - // Default template: Regions in left & right thirds - // Region 1 (car): left third, 140x300px starting at (40, 100) - covers 8%-35% of width - // Region 2 (giraffe): right third, 140x350px starting at (360, 75) - covers 70%-98% of width - this.setProperty("values", [ - [40, 100, 140, 300, 2.0], // Region 1 - left third - [360, 75, 140, 350, 2.0] // Region 2 - right third - ]) + const defaultWidth = 512; + const defaultHeight = 512; + this.setProperty("width", defaultWidth) + this.setProperty("height", defaultHeight) + // Calculate percentage-based defaults (divisible by 8) + this.setProperty("values", calculateDefaultRegions(defaultWidth, defaultHeight)) this.selected = false @@ -325,15 +322,12 @@ app.registerExtension({ nodeType.prototype.onNodeCreated = function () { const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined; - this.setProperty("width", 1024) - this.setProperty("height", 1024) - // Default template: Regions in left & right thirds - // Region 1 (car): left third, 280x600px starting at (80, 200) - covers 8%-35% of width - // Region 2 (giraffe): right third, 280x700px starting at (720, 150) - covers 70%-98% of width - this.setProperty("values", [ - [80, 200, 280, 600, 2.0], // Region 1 - left third - [720, 150, 280, 700, 2.0] // Region 2 - right third - ]) + const defaultWidth = 1024; + const defaultHeight = 1024; + this.setProperty("width", defaultWidth) + this.setProperty("height", defaultHeight) + // Calculate percentage-based defaults (divisible by 8) + this.setProperty("values", calculateDefaultRegions(defaultWidth, defaultHeight)) this.selected = false diff --git a/js/utils.js b/js/utils.js index fada4eb..9c668df 100644 --- a/js/utils.js +++ b/js/utils.js @@ -134,6 +134,35 @@ export function getDrawColor(percent, alpha) { return `#${f(0)}${f(8)}${f(4)}${alpha}`; } +/** + * Calculate default region positions as percentages of width/height + * Ensures results are divisible by 8 for latent space compatibility + * @param {number} width - Canvas width + * @param {number} height - Canvas height + * @returns {Array} Array of [x, y, w, h, strength] for 2 regions + */ +export function calculateDefaultRegions(width, height) { + // Round to nearest multiple of 8 + const round8 = (val) => Math.round(val / 8) * 8; + + // Region 1: Left third (8% to 35% of width) + const region1_x = round8(width * 0.08); + const region1_w = round8(width * 0.27); + const region1_y = round8(height * 0.20); // Start at 20% down + const region1_h = round8(height * 0.60); // 60% of height + + // Region 2: Right third (70% to 98% of width) + const region2_x = round8(width * 0.70); + const region2_w = round8(width * 0.28); + const region2_y = round8(height * 0.15); // Start at 15% down + const region2_h = round8(height * 0.68); // 68% of height (taller for giraffe) + + return [ + [region1_x, region1_y, region1_w, region1_h, 2.0], // Region 1 - left third + [region2_x, region2_y, region2_w, region2_h, 2.0] // Region 2 - right third + ]; +} + export function computeCanvasSize(node, size) { if (!node.widgets || node.widgets.length === 0 || node.widgets[0].last_y == null) return; @@ -158,8 +187,9 @@ export function computeCanvasSize(node, size) { // Set canvas height node.canvasHeight = CANVAS_HEIGHT; - // Ensure node is tall enough - const requiredHeight = y + 20; // 20px bottom margin + // Ensure node is tall enough with generous bottom margin + const BOTTOM_MARGIN = 80; // Extra space to prevent canvas overlap + const requiredHeight = y + BOTTOM_MARGIN; if (node.size[1] < requiredHeight) { node.size[1] = requiredHeight; if (node.graph) {