3 Commits

Author SHA1 Message Date
  w-e-w 1b9dea7d90 apply brush size limit early 1 year ago
  w-e-w 7cf80a70d9 allow brush size up to 1/2 diagonal image 1 year ago
  w-e-w 2eef345743 reduce complexity 1 year ago
2 changed files with 17 additions and 20 deletions
Split View
  1. +16
    -18
      extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js
  2. +1
    -2
      extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py

+ 16
- 18
extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js View File

@@ -470,29 +470,27 @@ onUiLoaded(async() => {
gradioApp().querySelector(
`${elemId} button[aria-label="Use brush"]`
);

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 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 newValue = currentRadius + delta;
input.value = Math.min(Math.max(newValue, 0), maxValue);
}
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;
}
const newValue = currentRadius + delta;
input.value = Math.max(newValue, 1);
input.dispatchEvent(new Event("change"));
}
}


+ 1
- 2
extensions-builtin/canvas-zoom-and-pan/scripts/hotkey_config.py View File

@@ -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'),
}))

Loading…
Cancel
Save
Baidu
map