Add zoom controls to the web UI

This commit is contained in:
justin337 2024-06-05 00:47:56 -07:00
parent b1fd26fe9e
commit 1a4706f39b
3 changed files with 72 additions and 2 deletions

View File

@ -52,6 +52,7 @@ app.registerExtension({
}
touchTime = null;
}
app.updateScaleValue()
});
app.canvasEl.addEventListener(

View File

@ -76,6 +76,11 @@ export class ComfyApp {
* @type {boolean}
*/
this.shiftDown = false;
/**
* Cache the zoom scale to prevent unnecessary DOM Manipulations
* @type {null}
*/
this.previousScale = null;
}
getPreviewFormatParam() {
@ -1561,6 +1566,8 @@ export class ComfyApp {
// Ensure the canvas fills the window
resizeCanvas();
this.updateScaleValue()
this.monitorScaleChanges()
window.addEventListener("resize", resizeCanvas);
await this.#invokeExtensionsAsync("init");
@ -1605,7 +1612,6 @@ export class ComfyApp {
this.#addCopyHandler();
this.#addPasteHandler();
this.#addKeyboardHandler();
await this.#invokeExtensionsAsync("setup");
}
@ -2325,12 +2331,42 @@ export class ComfyApp {
await this.#invokeExtensionsAsync("refreshComboInNodes", defs);
}
monitorScaleChanges() {
if (this.canvas && this.canvas.ds) {
const currentScale = this.canvas.ds.scale;
if (currentScale !== this.previousScale) {
this.previousScale = currentScale;
this.updateScaleValue();
}
}
requestAnimationFrame(() => this.monitorScaleChanges());
}
resetView() {
app.canvas.ds.scale = 1;
app.canvas.ds.offset = [0, 0]
app.graph.setDirtyCanvas(true, true);
}
setScale(newScale) {
app.canvas.ds.changeScale(newScale)
app.canvas.setDirty(true, true);
this.updateScaleValue()
}
incrementScale(increment) {
let newScale = app.canvas.ds.scale += increment;
this.setScale(newScale)
}
getZoomLevel() {
if (this.canvas && this.canvas.ds && typeof this.canvas.ds.scale === 'number') {
return (this.canvas.ds.scale * 100).toFixed(0) + '%';
}
}
updateScaleValue() {
const scaleValueEl = document.getElementById("scaleValue");
if (scaleValueEl) {
scaleValueEl.value = app.getZoomLevel()
}
}
/**
* Clean current state

View File

@ -614,6 +614,39 @@ export class ComfyUI {
app.resetView();
}
}),
$el("div", { style: { marginTop: "1em" } }, [
$el("label", { innerHTML: "Zoom" }),
$el("button", {
innerHTML: "-",
onclick: () => {
this.app.incrementScale(-0.1);
},
style: { marginLeft: "0.5em", padding: "2px 5px", fontSize: "12px" },
}),
$el("input", {
id: "scaleValue",
type: "text",
value: this.app.getZoomLevel(),
style: { marginLeft: "0.5em", paddingLeft: "0px", width: "50px", textAlign: "center" },
onchange: (e) => {
console.log(e.target.value)
const value = parseFloat(e.target.value.replace('%', ''));
if (!isNaN(value)) {
this.app.setScale(value / 100);
} else {
console.log('target')
e.target.value = this.app.getZoomLevel(); // Reset to the current zoom level if input is invalid
}
},
}),
$el("button", {
innerHTML: "+",
onclick: () => {
this.app.incrementScale(0.1);
},
style: { marginLeft: "0.5em", padding: "2px 5px", fontSize: "12px" },
}),
]),
]);
const devMode = this.settings.addSetting({