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] 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"),