Skip to content

Commit 57cd46a

Browse files
committed
refactor: use dynamic text height from canvas measure context for calc info message -- refactors the calc info message rendering into separate getCalcInfoMessage and measureCalcInfoMessage methods, introduced getMeasureContext to lazily create an offscreen canvas FontMetrics rendering context, used this in the collapsed-sections height calculation and the draw path to accurately compute box height, and bumped the collapsed preset section height from 90 to 55 to match the reduced content area
1 parent 0c6add4 commit 57cd46a

1 file changed

Lines changed: 57 additions & 41 deletions

File tree

js/ResolutionMaster.js

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ class ResolutionMasterCanvas {
127127
actions: this.collapsedSections?.actions ? 25 : 55,
128128
scaling: this.collapsedSections?.scaling ? 25 : 130,
129129
autoDetect: this.collapsedSections?.autoDetect ? 25 : 135,
130-
presets: this.collapsedSections?.presets ? 25 : 90
130+
presets: this.collapsedSections?.presets ? 25 : 55
131131
};
132132
Object.values(sectionHeights).forEach(height => {
133133
currentY += height + spacing;
134134
});
135135
if (props.showCalcInfo && props.selectedCategory) {
136-
currentY += 40;
136+
currentY += this.measureCalcInfoMessage().boxHeight + spacing;
137137
}
138138

139139
return currentY + 20;
@@ -1046,55 +1046,71 @@ class ResolutionMasterCanvas {
10461046
return currentHeight;
10471047
}
10481048

1049-
drawInfoMessage(ctx, y) {
1050-
const node = this.node;
1051-
const props = node.properties;
1049+
getCalcInfoMessage() {
1050+
const props = this.node.properties;
10521051
const category = props.selectedCategory;
1053-
1054-
let message = "";
1052+
10551053
if (category === "SDXL") {
1056-
message = "💡 SDXL Mode: Only using presets!";
1054+
return "💡 SDXL Mode: Only using presets!";
10571055
} else if (category === "Flux") {
1058-
message = "💡 FLUX Mode: Round to: 32px | Edge range: 320-2560px | Max resolution: 4.0 MP";
1056+
return "💡 FLUX Mode: Round to: 32px | Edge range: 320-2560px | Max resolution: 4.0 MP";
10591057
} else if (category === "Flux.2") {
1060-
message = "💡 FLUX.2 Mode: Round to: 16px | Edge range: 320-3840px | Max resolution: 6.0 MP";
1058+
return "💡 FLUX.2 Mode: Round to: 16px | Edge range: 320-3840px | Max resolution: 6.0 MP";
10611059
} else if (category === "WAN" && this.widthWidget && this.heightWidget) {
10621060
const pixels = this.widthWidget.value * this.heightWidget.value;
10631061
const model = pixels < 600000 ? "480p" : "720p";
1064-
message = `💡 WAN Mode: Suggesting ${model} model | Round to: 16px | Resolution range: 320p-820p`;
1062+
return `💡 WAN Mode: Suggesting ${model} model | Round to: 16px | Resolution range: 320p-820p`;
10651063
} else if (category === "HiDream Dev") {
1066-
message = "💡 HiDream Dev: Only using presets!";
1064+
return "💡 HiDream Dev: Only using presets!";
10671065
} else if (category === "Qwen-Image") {
1068-
message = "💡 Qwen-Image: Resolution range: ~0.6MP-4.2MP. If input is already in this range, it remains unchanged.";
1066+
return "💡 Qwen-Image: Resolution range: ~0.6MP-4.2MP. If input is already in this range, it remains unchanged.";
10691067
} else if (['Standard', 'Social Media', 'Print', 'Cinema'].includes(category)) {
1070-
message = "💡 Calc Mode: Scales the selected preset to the closest current resolution, maintaining the preset's aspect ratio.";
1071-
} else {
1072-
message = "⚠️ Calc Mode: Custom calculation not available for this category)";
1073-
}
1074-
1075-
if (message) {
1076-
const paddingX = 10;
1077-
const paddingTop = 8;
1078-
const paddingBottom = 8;
1079-
const lineHeight = 14;
1080-
const maxWidth = node.size[0] - 40 - (paddingX * 2);
1081-
ctx.font = "11px Arial";
1082-
const words = message.split(' ');
1083-
const lines = [];
1084-
let currentLine = '';
1085-
for (const word of words) {
1086-
const testLine = currentLine ? `${currentLine} ${word}` : word;
1087-
if (ctx.measureText(testLine).width > maxWidth && currentLine) {
1088-
lines.push(currentLine);
1089-
currentLine = word;
1090-
} else {
1091-
currentLine = testLine;
1092-
}
1093-
}
1094-
if (currentLine) lines.push(currentLine);
1095-
1096-
const textHeight = lines.length * lineHeight - (lineHeight - ctx.measureText("M").width);
1097-
const boxHeight = textHeight + paddingTop + paddingBottom;
1068+
return "💡 Calc Mode: Scales the selected preset to the closest current resolution, maintaining the preset's aspect ratio.";
1069+
}
1070+
return "⚠️ Calc Mode: Custom calculation not available for this category)";
1071+
}
1072+
1073+
getMeasureContext() {
1074+
if (!this.measureContext && typeof document !== "undefined") {
1075+
this.measureContext = document.createElement("canvas").getContext("2d");
1076+
}
1077+
return this.measureContext;
1078+
}
1079+
1080+
measureCalcInfoMessage(ctx = null) {
1081+
const message = this.getCalcInfoMessage();
1082+
if (!message) return { boxHeight: 0 };
1083+
1084+
const measureCtx = ctx || this.getMeasureContext();
1085+
const paddingX = 10;
1086+
const paddingTop = 8;
1087+
const paddingBottom = 8;
1088+
const lineHeight = 14;
1089+
const maxWidth = this.node.size[0] - 40 - (paddingX * 2);
1090+
const words = message.split(' ');
1091+
const lines = [];
1092+
let currentLine = '';
1093+
1094+
if (measureCtx) measureCtx.font = "11px Arial";
1095+
for (const word of words) {
1096+
const testLine = currentLine ? `${currentLine} ${word}` : word;
1097+
const testWidth = measureCtx ? measureCtx.measureText(testLine).width : testLine.length * 6;
1098+
if (testWidth > maxWidth && currentLine) {
1099+
lines.push(currentLine);
1100+
currentLine = word;
1101+
} else {
1102+
currentLine = testLine;
1103+
}
1104+
}
1105+
if (currentLine) lines.push(currentLine);
1106+
1107+
return { boxHeight: lines.length * lineHeight + paddingTop + paddingBottom, lines, paddingTop, lineHeight };
1108+
}
1109+
1110+
drawInfoMessage(ctx, y) {
1111+
const node = this.node;
1112+
const { boxHeight, lines = [], paddingTop = 8, lineHeight = 14 } = this.measureCalcInfoMessage(ctx);
1113+
if (boxHeight > 0) {
10981114
ctx.fillStyle = "rgba(250, 165, 90, 0.15)";
10991115
ctx.strokeStyle = "rgba(250, 165, 90, 0.5)";
11001116
ctx.beginPath();

0 commit comments

Comments
 (0)