Skip to content

Commit 1b9dea7

Browse files
committed
apply brush size limit early
1 parent 7cf80a7 commit 1b9dea7

File tree

1 file changed

+8
-11
lines changed
  • extensions-builtin/canvas-zoom-and-pan/javascript

1 file changed

+8
-11
lines changed

extensions-builtin/canvas-zoom-and-pan/javascript/zoom.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,18 @@ onUiLoaded(async() => {
470470
gradioApp().querySelector(
471471
`${elemId} button[aria-label="Use brush"]`
472472
);
473-
474473
if (input) {
475474
input.click();
476475
if (!withoutValue) {
477476
const maxValue = parseFloat(input.getAttribute("max")) || 100;
477+
// allow brush size up to 1/2 diagonal of the image, beyond gradio's arbitrary limit
478+
const canvasImg = gradioApp().querySelector(`${elemId} img`);
479+
if (canvasImg) {
480+
const maxDiameter = Math.sqrt(canvasImg.naturalWidth ** 2 + canvasImg.naturalHeight ** 2) / 2;
481+
if (maxDiameter > maxValue) {
482+
input.setAttribute("max", maxDiameter);
483+
}
484+
}
478485
const brush_factor = deltaY > 0 ? 1 - opts.canvas_hotkey_brush_factor : 1 + opts.canvas_hotkey_brush_factor;
479486
const currentRadius = parseFloat(input.value);
480487
let delta = Math.sqrt(currentRadius ** 2 * brush_factor) - currentRadius;
@@ -483,16 +490,6 @@ onUiLoaded(async() => {
483490
delta = deltaY > 0 ? -1 : 1;
484491
}
485492
const newValue = currentRadius + delta;
486-
// allow increasing the brush size beyond what's limited by gradio up to 1/2 diagonal of the image
487-
if (newValue > maxValue) {
488-
const canvasImg = gradioApp().querySelector(`${elemId} img`);
489-
if (canvasImg) {
490-
const maxDiameter = Math.sqrt(canvasImg.naturalWidth ** 2 + canvasImg.naturalHeight ** 2) / 2;
491-
if (newValue < maxDiameter) {
492-
input.setAttribute("max", newValue);
493-
}
494-
}
495-
}
496493
input.value = Math.max(newValue, 1);
497494
input.dispatchEvent(new Event("change"));
498495
}

0 commit comments

Comments
 (0)