Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image grid render consistency #654

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,34 +730,6 @@ export class ComfyApp {
}
}

const calculateGrid = (w, h, n) => {
let columns, rows, cellsize

if (w > h) {
cellsize = h
columns = Math.ceil(w / cellsize)
rows = Math.ceil(n / columns)
} else {
cellsize = w
rows = Math.ceil(h / cellsize)
columns = Math.ceil(n / rows)
}

while (columns * rows < n) {
cellsize++
if (w >= h) {
columns = Math.ceil(w / cellsize)
rows = Math.ceil(n / columns)
} else {
rows = Math.ceil(h / cellsize)
columns = Math.ceil(n / rows)
}
}

const cell_size = Math.min(w / columns, h / rows)
return { cell_size, columns, rows }
}

const is_all_same_aspect_ratio = (imgs) => {
// assume: imgs.length >= 2
let ratio = imgs[0].naturalWidth / imgs[0].naturalHeight
Expand Down Expand Up @@ -838,17 +810,23 @@ export class ComfyApp {
if (!compact_mode) {
// use rectangle cell style and border line
cell_padding = 2
const { cell_size, columns, rows } = calculateGrid(
dw,
dh,
numImages
// Prevent infinite canvas2d scale-up
const largestDimension = this.imgs.reduce(
(acc, current) =>
Math.max(acc, current.naturalWidth, current.naturalHeight),
0
)
cols = columns

cellWidth = cell_size
cellHeight = cell_size
shiftX = (dw - cell_size * cols) / 2
shiftY = (dh - cell_size * rows) / 2 + top
const fakeImgs = []
fakeImgs.length = this.imgs.length
fakeImgs[0] = {
naturalWidth: largestDimension,
naturalHeight: largestDimension
}
;({ cellWidth, cellHeight, cols, shiftX } = calculateImageGrid(
fakeImgs,
dw,
dh
))
} else {
cell_padding = 0
;({ cellWidth, cellHeight, cols, shiftX } = calculateImageGrid(
Expand Down
Loading