From 4e808fbef41ef5c765ecaf7ade727ecab5e8e75c Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:03:27 +0900 Subject: [PATCH 01/11] Canvas: Adjust brush size by area --- .../canvas-zoom-and-pan/javascript/zoom.js | 19 ++++++++++++++----- .../scripts/hotkey_config.py | 8 +++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index b40c86027..dc87b8518 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -470,11 +470,20 @@ onUiLoaded(async() => { if (!withoutValue) { const maxValue = parseFloat(input.getAttribute("max")) || 100; - const changeAmount = maxValue * (percentage / 100); - const newValue = - parseFloat(input.value) + - (deltaY > 0 ? -changeAmount : changeAmount); - input.value = Math.min(Math.max(newValue, 0), maxValue); + if (opts.canvas_hotkey_brush_scale === "Radius") { + const changeAmount = maxValue * (percentage / 100); + const newValue = + parseFloat(input.value) + + (deltaY > 0 ? -changeAmount : changeAmount); + input.value = Math.min(Math.max(newValue, 0), maxValue); + } else { + const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor + const currentRadius = parseFloat(input.value); + const currentArea = currentRadius ** 2; + const newArea = currentArea * brush_factor; + const newValue = Math.sqrt(newArea); + input.value = Math.min(Math.max(newValue, 0), maxValue); + } input.dispatchEvent(new Event("change")); } } diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index 17b27b274..142fa6fcb 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -2,8 +2,8 @@ import gradio as gr from modules import shared shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas Hotkeys"), { - "canvas_hotkey_zoom": shared.OptionInfo("Alt", "Zoom canvas", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"), - "canvas_hotkey_adjust": shared.OptionInfo("Ctrl", "Adjust brush size", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"), + "canvas_hotkey_zoom": shared.OptionInfo("Alt", "Zoom canvas", gr.Radio, {"choices": ["Shift", "Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"), + "canvas_hotkey_adjust": shared.OptionInfo("Ctrl", "Adjust brush size", gr.Radio, {"choices": ["Shift", "Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"), "canvas_hotkey_shrink_brush": shared.OptionInfo("Q", "Shrink the brush size"), "canvas_hotkey_grow_brush": shared.OptionInfo("W", "Enlarge the brush size"), "canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"), @@ -13,5 +13,7 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_show_tooltip": shared.OptionInfo(True, "Enable tooltip on the canvas"), "canvas_auto_expand": shared.OptionInfo(True, "Automatically expands an image that does not fit completely in the canvas area, similar to manually pressing the S and R buttons"), "canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"), - "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom","Adjust brush size","Hotkey enlarge brush","Hotkey shrink brush","Moving canvas","Fullscreen","Reset Zoom","Overlap"]}), + "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom", "Adjust brush size", "Hotkey enlarge brush", "Hotkey shrink brush", "Moving canvas", "Fullscreen", "Reset Zoom", "Overlap"]}), + "canvas_hotkey_brush_scale": shared.OptionInfo("Radius", "Brush scale", gr.Radio, {"choices": ["Radius", "Area"]}), + "canvas_hotkey_brush_factor": shared.OptionInfo(0.1, "Brush factor", gr.Slider, {"minimum": 0, "maximum": 2, "step": 0.01}) })) From 356339eff2a93869aae5717012c6b422f8260216 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 04:35:45 +0900 Subject: [PATCH 02/11] Canvas: Undo hotkey --- .../canvas-zoom-and-pan/javascript/zoom.js | 10 +++++++++- .../canvas-zoom-and-pan/scripts/hotkey_config.py | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index dc87b8518..a788fe591 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -226,6 +226,7 @@ onUiLoaded(async() => { canvas_show_tooltip: true, canvas_auto_expand: true, canvas_blur_prompt: false, + canvas_hotkey_undo: "KeyZ", }; const functionMap = { @@ -321,6 +322,7 @@ onUiLoaded(async() => { action: "Adjust brush size", keySuffix: " + wheel" }, + {configKey: "canvas_hotkey_undo", action: "Undo brush stroke"}, {configKey: "canvas_hotkey_reset", action: "Reset zoom"}, { configKey: "canvas_hotkey_fullscreen", @@ -489,6 +491,11 @@ onUiLoaded(async() => { } } + // Undo the last brush stroke by clicking the undo button + function undoBrushStroke() { + gradioApp().querySelector(`${elemId} button[aria-label='Undo']`).click(); + } + // Reset zoom when uploading a new image const fileInput = gradioApp().querySelector( `${elemId} input[type="file"][accept="image/*"].svelte-116rqfv` @@ -708,7 +715,8 @@ onUiLoaded(async() => { [hotkeysConfig.canvas_hotkey_overlap]: toggleOverlap, [hotkeysConfig.canvas_hotkey_fullscreen]: fitToScreen, [hotkeysConfig.canvas_hotkey_shrink_brush]: () => adjustBrushSize(elemId, 10), - [hotkeysConfig.canvas_hotkey_grow_brush]: () => adjustBrushSize(elemId, -10) + [hotkeysConfig.canvas_hotkey_grow_brush]: () => adjustBrushSize(elemId, -10), + [hotkeysConfig.canvas_hotkey_undo]: undoBrushStroke }; const action = hotkeyActions[event.code]; diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index 142fa6fcb..400936f9e 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -7,6 +7,7 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_hotkey_shrink_brush": shared.OptionInfo("Q", "Shrink the brush size"), "canvas_hotkey_grow_brush": shared.OptionInfo("W", "Enlarge the brush size"), "canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"), + "canvas_hotkey_undo": shared.OptionInfo("Z", "Undo brush stroke"), "canvas_hotkey_fullscreen": shared.OptionInfo("S", "Fullscreen Mode, maximizes the picture so that it fits into the screen and stretches it to its full width "), "canvas_hotkey_reset": shared.OptionInfo("R", "Reset zoom and canvas position"), "canvas_hotkey_overlap": shared.OptionInfo("O", "Toggle overlap").info("Technical button, needed for testing"), From e009f586a2943a9ea046d2fc60cb6f84f43db594 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 05:17:13 +0900 Subject: [PATCH 03/11] limit the minimum delta to 1 --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index a788fe591..31e0b5626 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -483,7 +483,12 @@ onUiLoaded(async() => { const currentRadius = parseFloat(input.value); const currentArea = currentRadius ** 2; const newArea = currentArea * brush_factor; - const newValue = Math.sqrt(newArea); + let delta = Math.sqrt(newArea) - currentRadius; + // gradio seems to have a minimum brush size step of 1 + if (Math.abs(delta) < 1) { + delta = delta > 0 ? 1 : -1; + } + const newValue = currentRadius + delta; input.value = Math.min(Math.max(newValue, 0), maxValue); } input.dispatchEvent(new Event("change")); From c9f8953200e9dff3c3512f1ca397ca01b213b4cc Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 05:31:27 +0900 Subject: [PATCH 04/11] Canvas: Clear hotkey --- .../canvas-zoom-and-pan/javascript/zoom.js | 13 +++++++++---- .../canvas-zoom-and-pan/scripts/hotkey_config.py | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 31e0b5626..de2b359e1 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -227,6 +227,7 @@ onUiLoaded(async() => { canvas_auto_expand: true, canvas_blur_prompt: false, canvas_hotkey_undo: "KeyZ", + canvas_hotkey_clear: "KeyC", }; const functionMap = { @@ -323,6 +324,7 @@ onUiLoaded(async() => { keySuffix: " + wheel" }, {configKey: "canvas_hotkey_undo", action: "Undo brush stroke"}, + {configKey: "canvas_hotkey_clear", action: "Clear canvas"}, {configKey: "canvas_hotkey_reset", action: "Reset zoom"}, { configKey: "canvas_hotkey_fullscreen", @@ -481,9 +483,7 @@ onUiLoaded(async() => { } else { const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor const currentRadius = parseFloat(input.value); - const currentArea = currentRadius ** 2; - const newArea = currentArea * brush_factor; - let delta = Math.sqrt(newArea) - currentRadius; + let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius; // gradio seems to have a minimum brush size step of 1 if (Math.abs(delta) < 1) { delta = delta > 0 ? 1 : -1; @@ -501,6 +501,10 @@ onUiLoaded(async() => { gradioApp().querySelector(`${elemId} button[aria-label='Undo']`).click(); } + function clearCanvas() { + gradioApp().querySelector(`${elemId} button[aria-label='Clear']`).click(); + } + // Reset zoom when uploading a new image const fileInput = gradioApp().querySelector( `${elemId} input[type="file"][accept="image/*"].svelte-116rqfv` @@ -721,7 +725,8 @@ onUiLoaded(async() => { [hotkeysConfig.canvas_hotkey_fullscreen]: fitToScreen, [hotkeysConfig.canvas_hotkey_shrink_brush]: () => adjustBrushSize(elemId, 10), [hotkeysConfig.canvas_hotkey_grow_brush]: () => adjustBrushSize(elemId, -10), - [hotkeysConfig.canvas_hotkey_undo]: undoBrushStroke + [hotkeysConfig.canvas_hotkey_undo]: undoBrushStroke, + [hotkeysConfig.canvas_hotkey_clear]: clearCanvas }; const action = hotkeyActions[event.code]; diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index 400936f9e..d4565d57e 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -8,6 +8,7 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_hotkey_grow_brush": shared.OptionInfo("W", "Enlarge the brush size"), "canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"), "canvas_hotkey_undo": shared.OptionInfo("Z", "Undo brush stroke"), + "canvas_hotkey_clear": shared.OptionInfo("C", "Clear canvas"), "canvas_hotkey_fullscreen": shared.OptionInfo("S", "Fullscreen Mode, maximizes the picture so that it fits into the screen and stretches it to its full width "), "canvas_hotkey_reset": shared.OptionInfo("R", "Reset zoom and canvas position"), "canvas_hotkey_overlap": shared.OptionInfo("O", "Toggle overlap").info("Technical button, needed for testing"), From dd237f2541c22734044e79765109352e3fad4388 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 05:40:01 +0900 Subject: [PATCH 05/11] make Undo Clear hotkey disableable --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 4 +++- .../canvas-zoom-and-pan/scripts/hotkey_config.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index de2b359e1..785840c9a 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -238,7 +238,9 @@ onUiLoaded(async() => { "Moving canvas": "canvas_hotkey_move", "Fullscreen": "canvas_hotkey_fullscreen", "Reset Zoom": "canvas_hotkey_reset", - "Overlap": "canvas_hotkey_overlap" + "Overlap": "canvas_hotkey_overlap", + "Undo": "canvas_hotkey_undo", + "Clear": "canvas_hotkey_clear" }; // Loading the configuration from opts diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index d4565d57e..fea3fe1f8 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -15,7 +15,7 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_show_tooltip": shared.OptionInfo(True, "Enable tooltip on the canvas"), "canvas_auto_expand": shared.OptionInfo(True, "Automatically expands an image that does not fit completely in the canvas area, similar to manually pressing the S and R buttons"), "canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"), - "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom", "Adjust brush size", "Hotkey enlarge brush", "Hotkey shrink brush", "Moving canvas", "Fullscreen", "Reset Zoom", "Overlap"]}), + "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom", "Adjust brush size", "Hotkey enlarge brush", "Hotkey shrink brush", "Undo", "Clear", "Moving canvas", "Fullscreen", "Reset Zoom", "Overlap"]}), "canvas_hotkey_brush_scale": shared.OptionInfo("Radius", "Brush scale", gr.Radio, {"choices": ["Radius", "Area"]}), "canvas_hotkey_brush_factor": shared.OptionInfo(0.1, "Brush factor", gr.Slider, {"minimum": 0, "maximum": 2, "step": 0.01}) })) From 96eaca61539da532a94851dd879089123df43d68 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 05:55:06 +0900 Subject: [PATCH 06/11] make "Adjust brush size by area" Default behavior --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 2 +- extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 785840c9a..a902ab636 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -476,7 +476,7 @@ onUiLoaded(async() => { if (!withoutValue) { const maxValue = parseFloat(input.getAttribute("max")) || 100; - if (opts.canvas_hotkey_brush_scale === "Radius") { + if (opts.canvas_hotkey_brush_factor_mode === "Radius") { const changeAmount = maxValue * (percentage / 100); const newValue = parseFloat(input.value) + diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index fea3fe1f8..f31607644 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -16,6 +16,6 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_auto_expand": shared.OptionInfo(True, "Automatically expands an image that does not fit completely in the canvas area, similar to manually pressing the S and R buttons"), "canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"), "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom", "Adjust brush size", "Hotkey enlarge brush", "Hotkey shrink brush", "Undo", "Clear", "Moving canvas", "Fullscreen", "Reset Zoom", "Overlap"]}), - "canvas_hotkey_brush_scale": shared.OptionInfo("Radius", "Brush scale", gr.Radio, {"choices": ["Radius", "Area"]}), + "canvas_hotkey_brush_factor_mode": shared.OptionInfo("Area", "Brush size adjustment mode", gr.Radio, {"choices": ["Area", "Radius"]}), "canvas_hotkey_brush_factor": shared.OptionInfo(0.1, "Brush factor", gr.Slider, {"minimum": 0, "maximum": 2, "step": 0.01}) })) From 3a1497aaf1c3aef79b31ade0ac5eecc1ff4d51e1 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 20 Nov 2024 06:09:01 +0900 Subject: [PATCH 07/11] eslint --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index a902ab636..9c9589ccb 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -483,7 +483,7 @@ onUiLoaded(async() => { (deltaY > 0 ? -changeAmount : changeAmount); input.value = Math.min(Math.max(newValue, 0), maxValue); } else { - const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor + const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor; const currentRadius = parseFloat(input.value); let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius; // gradio seems to have a minimum brush size step of 1 From 2eef3457436e32994df0a3f929519388812df096 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Fri, 22 Nov 2024 10:14:53 +0900 Subject: [PATCH 08/11] reduce complexity remove the overly complex option of radius / area brush size change mode --- .../canvas-zoom-and-pan/javascript/zoom.js | 27 +++++++------------ .../scripts/hotkey_config.py | 3 +-- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 9c9589ccb..714e29de6 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -474,25 +474,16 @@ onUiLoaded(async() => { if (input) { input.click(); if (!withoutValue) { - const maxValue = - parseFloat(input.getAttribute("max")) || 100; - if (opts.canvas_hotkey_brush_factor_mode === "Radius") { - const changeAmount = maxValue * (percentage / 100); - const newValue = - parseFloat(input.value) + - (deltaY > 0 ? -changeAmount : changeAmount); - input.value = Math.min(Math.max(newValue, 0), maxValue); - } else { - const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor; - const currentRadius = parseFloat(input.value); - let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius; - // gradio seems to have a minimum brush size step of 1 - if (Math.abs(delta) < 1) { - delta = delta > 0 ? 1 : -1; - } - const newValue = currentRadius + delta; - input.value = Math.min(Math.max(newValue, 0), maxValue); + const maxValue = parseFloat(input.getAttribute("max")) || 100; + const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor; + const currentRadius = parseFloat(input.value); + let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius; + // minimum brush size step of 1 + if (Math.abs(delta) < 1) { + delta = deltaY > 0 ? -1 : 1; } + let newValue = currentRadius + delta; + input.value = Math.min(Math.max(newValue, 1), maxValue); input.dispatchEvent(new Event("change")); } } diff --git a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py index f31607644..18889583a 100644 --- a/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py +++ b/extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py @@ -16,6 +16,5 @@ shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas "canvas_auto_expand": shared.OptionInfo(True, "Automatically expands an image that does not fit completely in the canvas area, similar to manually pressing the S and R buttons"), "canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"), "canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom", "Adjust brush size", "Hotkey enlarge brush", "Hotkey shrink brush", "Undo", "Clear", "Moving canvas", "Fullscreen", "Reset Zoom", "Overlap"]}), - "canvas_hotkey_brush_factor_mode": shared.OptionInfo("Area", "Brush size adjustment mode", gr.Radio, {"choices": ["Area", "Radius"]}), - "canvas_hotkey_brush_factor": shared.OptionInfo(0.1, "Brush factor", gr.Slider, {"minimum": 0, "maximum": 2, "step": 0.01}) + "canvas_hotkey_brush_factor": shared.OptionInfo(0.1, "Brush size change rate", gr.Slider, {"minimum": 0, "maximum": 1, "step": 0.01}).info('controls how much the brush size is changed when using hotkeys or scroll wheel'), })) From 7cf80a70d9ff33eca787a57beaaf3c61eb6a40af Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:00:26 +0900 Subject: [PATCH 09/11] allow brush size up to 1/2 diagonal image --- .../canvas-zoom-and-pan/javascript/zoom.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 714e29de6..85d8c2cbb 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -482,8 +482,18 @@ onUiLoaded(async() => { if (Math.abs(delta) < 1) { delta = deltaY > 0 ? -1 : 1; } - let newValue = currentRadius + delta; - input.value = Math.min(Math.max(newValue, 1), maxValue); + const newValue = currentRadius + delta; + // allow increasing the brush size beyond what's limited by gradio up to 1/2 diagonal of the image + if (newValue > maxValue) { + const canvasImg = gradioApp().querySelector(`${elemId} img`); + if (canvasImg) { + const maxDiameter = Math.sqrt(canvasImg.naturalWidth ** 2 + canvasImg.naturalHeight ** 2) / 2; + if (newValue < maxDiameter) { + input.setAttribute("max", newValue); + } + } + } + input.value = Math.max(newValue, 1); input.dispatchEvent(new Event("change")); } } From 1b9dea7d9001c16b0cb5b89463a0c3dafbfb4d56 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:29:44 +0900 Subject: [PATCH 10/11] apply brush size limit early --- .../canvas-zoom-and-pan/javascript/zoom.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index 85d8c2cbb..da07d3c7c 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -470,11 +470,18 @@ onUiLoaded(async() => { gradioApp().querySelector( `${elemId} button[aria-label="Use brush"]` ); - if (input) { input.click(); if (!withoutValue) { const maxValue = parseFloat(input.getAttribute("max")) || 100; + // allow brush size up to 1/2 diagonal of the image, beyond gradio's arbitrary limit + const canvasImg = gradioApp().querySelector(`${elemId} img`); + if (canvasImg) { + const maxDiameter = Math.sqrt(canvasImg.naturalWidth ** 2 + canvasImg.naturalHeight ** 2) / 2; + if (maxDiameter > maxValue) { + input.setAttribute("max", maxDiameter); + } + } const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor; const currentRadius = parseFloat(input.value); let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius; @@ -483,16 +490,6 @@ onUiLoaded(async() => { delta = deltaY > 0 ? -1 : 1; } const newValue = currentRadius + delta; - // allow increasing the brush size beyond what's limited by gradio up to 1/2 diagonal of the image - if (newValue > maxValue) { - const canvasImg = gradioApp().querySelector(`${elemId} img`); - if (canvasImg) { - const maxDiameter = Math.sqrt(canvasImg.naturalWidth ** 2 + canvasImg.naturalHeight ** 2) / 2; - if (newValue < maxDiameter) { - input.setAttribute("max", newValue); - } - } - } input.value = Math.max(newValue, 1); input.dispatchEvent(new Event("change")); } From 661322707f003e89cf3a0897c72d2a9053d401df Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sat, 23 Nov 2024 06:59:19 +0900 Subject: [PATCH 11/11] set min brush size to 1 --- extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js index da07d3c7c..0b305dbc7 100644 --- a/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js +++ b/extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js @@ -474,6 +474,7 @@ onUiLoaded(async() => { input.click(); if (!withoutValue) { const maxValue = parseFloat(input.getAttribute("max")) || 100; + const minValue = parseFloat(input.getAttribute("min")) || 1; // allow brush size up to 1/2 diagonal of the image, beyond gradio's arbitrary limit const canvasImg = gradioApp().querySelector(`${elemId} img`); if (canvasImg) { @@ -481,6 +482,9 @@ onUiLoaded(async() => { if (maxDiameter > maxValue) { input.setAttribute("max", maxDiameter); } + if (minValue > 1) { + input.setAttribute("min", '1'); + } } const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor; const currentRadius = parseFloat(input.value);