diff --git a/web/extensions/core/simpleTouchSupport.js b/web/extensions/core/simpleTouchSupport.js index 041fc2c4c..20adacb38 100644 --- a/web/extensions/core/simpleTouchSupport.js +++ b/web/extensions/core/simpleTouchSupport.js @@ -52,6 +52,7 @@ app.registerExtension({ } touchTime = null; } + app.updateScaleValue() }); app.canvasEl.addEventListener( diff --git a/web/scripts/app.js b/web/scripts/app.js index f96d197a8..011a89c6a 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -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 diff --git a/web/scripts/ui.js b/web/scripts/ui.js index 72e43d357..aa0379a08 100644 --- a/web/scripts/ui.js +++ b/web/scripts/ui.js @@ -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({