multimodalart HF Staff commited on
Commit
c65f051
·
verified ·
1 Parent(s): b58ec2b

lock proportion for corners

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -757,10 +757,37 @@ function init() {
757
  } else {
758
  let {x1,y1,x2,y2} = {...o};
759
  const t = dragType;
760
- if (t.includes('l')) x1 = clamp01(o.x1+dx);
761
- if (t.includes('r')) x2 = clamp01(o.x2+dx);
762
- if (t.includes('t')) y1 = clamp01(o.y1+dy);
763
- if (t.includes('b')) y2 = clamp01(o.y2+dy);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764
  if (x1>x2) {const tmp=x1;x1=x2;x2=tmp;}
765
  if (y1>y2) {const tmp=y1;y1=y2;y2=tmp;}
766
  if (Math.abs(x2-x1)<0.01||Math.abs(y2-y1)<0.01) return;
 
757
  } else {
758
  let {x1,y1,x2,y2} = {...o};
759
  const t = dragType;
760
+ const isCorner = (t==='tl'||t==='tr'||t==='bl'||t==='br');
761
+ const freeResize = e && e.shiftKey;
762
+
763
+ if (isCorner && !freeResize) {
764
+ // Aspect-locked resize; opposite corner is the anchor
765
+ const origW = o.x2 - o.x1, origH = o.y2 - o.y1;
766
+ if (origW < 1e-6 || origH < 1e-6) return;
767
+ const aspect = origW / origH;
768
+ const ax = t.includes('l') ? o.x2 : o.x1;
769
+ const ay = t.includes('t') ? o.y2 : o.y1;
770
+ const mx = x / dispW, my = y / dispH;
771
+ let newW = Math.abs(mx - ax);
772
+ let newH = Math.abs(my - ay);
773
+ if (newW / aspect > newH) newH = newW / aspect;
774
+ else newW = newH * aspect;
775
+ const sx = t.includes('l') ? -1 : 1;
776
+ const sy = t.includes('t') ? -1 : 1;
777
+ const maxW = sx > 0 ? (1 - ax) : ax;
778
+ const maxH = sy > 0 ? (1 - ay) : ay;
779
+ const k = Math.min(1, maxW / newW, maxH / newH);
780
+ newW *= k; newH *= k;
781
+ x1 = sx > 0 ? ax : ax - newW;
782
+ x2 = sx > 0 ? ax + newW : ax;
783
+ y1 = sy > 0 ? ay : ay - newH;
784
+ y2 = sy > 0 ? ay + newH : ay;
785
+ } else {
786
+ if (t.includes('l')) x1 = clamp01(o.x1+dx);
787
+ if (t.includes('r')) x2 = clamp01(o.x2+dx);
788
+ if (t.includes('t')) y1 = clamp01(o.y1+dy);
789
+ if (t.includes('b')) y2 = clamp01(o.y2+dy);
790
+ }
791
  if (x1>x2) {const tmp=x1;x1=x2;x2=tmp;}
792
  if (y1>y2) {const tmp=y1;y1=y2;y2=tmp;}
793
  if (Math.abs(x2-x1)<0.01||Math.abs(y2-y1)<0.01) return;