Skip to content

Commit 2be2e6f

Browse files
Fix: Refine canvas expansion logic for selection-based tools
This commit continues the work from PR zero01101#180 to address issues with canvas expansion, particularly for the stamp tool, selection tool, and dream tool. Key changes: - Canvas expansion for the stamp tool now only occurs upon actual stamping (on click/draw), not during cursor movement. The bounding box calculation for expansion has been corrected to accurately reflect the stamp's dimensions and placement. - Reviewed and confirmed that `js/lib/layers.js` correctly handles calls to `recalculateBg()`, only triggering it when the canvas dimensions actually change. - Ensured that `js/ui/tool/select.js` and `js/ui/tool/dream.js` also follow the principle of expanding the canvas only when an action (like finalizing a selection or initiating image generation) necessitates it. These changes aim to resolve issues where the canvas would expand excessively, leading to high VRAM usage and potential tool breakage, as discussed in PR zero01101#180. The overall behavior should now be more robust and user-friendly, with canvas expansion happening predictably and only when required.
1 parent cc53647 commit 2be2e6f

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

js/ui/tool/stamp.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,39 @@ const stampTool = () =>
419419
const resource = state.selected;
420420

421421
if (resource) {
422-
const real_bb = getBoundingBox(sx,sy,1,1); //TODO:Here we have to get the actual real bounding box somehow
422+
// Calculate the real bounding box
423+
const image = resource.image;
424+
const w = image.width * state.scale;
425+
const h = image.height * state.scale;
426+
427+
// Calculate rotated corners
428+
const cx = 0; // Image center x, relative to sx
429+
const cy = 0; // Image center y, relative to sy
430+
431+
const sinR = Math.sin(rotation);
432+
const cosR = Math.cos(rotation);
433+
434+
const x1 = sx + cx * cosR - cy * sinR;
435+
const y1 = sy + cx * sinR + cy * cosR;
436+
const x2 = sx + (cx + w) * cosR - cy * sinR;
437+
const y2 = sy + (cx + w) * sinR + cy * cosR;
438+
const x3 = sx + (cx + w) * cosR - (cy + h) * sinR;
439+
const y3 = sy + (cx + w) * sinR + (cy + h) * cosR;
440+
const x4 = sx + cx * cosR - (cy + h) * sinR;
441+
const y4 = sy + cx * sinR + (cy + h) * cosR;
442+
443+
const minX = Math.min(x1, x2, x3, x4);
444+
const minY = Math.min(y1, y2, y3, y4);
445+
const maxX = Math.max(x1, x2, x3, x4);
446+
const maxY = Math.max(y1, y2, y3, y4);
447+
448+
const real_bb = {
449+
x: minX,
450+
y: minY,
451+
width: maxX - minX,
452+
height: maxY - minY,
453+
};
454+
423455
imageCollection.auto_expand_to_fit(real_bb);
424456
state.redraw();
425457

0 commit comments

Comments
 (0)