开发者

A Bug in Jcrop, minSize + aspectRatio

I am trying to use Jcrop for my application, though I have run into a bug with it. I went to the demo page, and the bug exists there too. Here is how to create it.

Go to this demo page http://deepliquid.com/projects/Jcrop/demos.php?demo=advanced

Make sure the following options are checked "Selection can be moved"

"Resizable selection" "Aspect ratio" "minSize/maxSize setting"

Create a selected area, drag it to the upper left corner, grab the lower right corner of the selection(as if you were going to resize it) and drag it to the upper left corner of the image.

Once you pass the upper left corner of the image the select area collapses down to a 0x0 pixel selection.

This bug only happens when an aspect ratio is set. Otherwise it works fine.

I'm wondering if anyone has any experience hacking around with this plugin to where they might be able to fix this bug. I've been going through it all day and haven't been able to figure it out yet.

--Edit-- After spending a few more hours with it I was able to get the bug mostly fixed. I changed the following code.

        // Magic %-)
        if(xx >= x1) { // right side <-- Changed > to >=
          if(xx - x1 < min_x) {
            xx = x1 + min_x;
          } else if (xx - x1 > max_x) {
            xx = x1 + max_x;
          }
          if(yy > 开发者_如何学Pythony1) {
            yy = y1 + (xx - x1)/aspect;
          } else {
            yy = y1 - (xx - x1)/aspect;
          }
        } else if (xx <= x1) { // left side <-- Changed < to <=
          if(x1 - xx < min_x) {
            xx = x1 - min_x
          } else if (x1 - xx > max_x) {
            xx = x1 - max_x;
          }
          if(yy > y1) {
            yy = y1 + (x1 - xx)/aspect;
          } else {
            yy = y1 - (x1 - xx)/aspect;
          }
        }

This stopped it from collapsing, its still acts a little buggy though.

--End Edit--


A more stable version seems to be the following:

if(xx===x1){xx=x1+min_x;}

// Magic %-)
if (xx > x1) { // right side
    if (xx - x1 < min_x) {
        xx = x1 + min_x;
    } else if (xx - x1 > max_x) {
        xx = x1 + max_x;
    }
    if (yy > y1) {
        yy = y1 + (xx - x1) / aspect;
    } else {
        yy = y1 - (xx - x1) / aspect;
    }
} else if (xx < x1) { // left side
    if (x1 - xx < min_x) {
        xx = x1 - min_x;
    } else if (x1 - xx > max_x) {
        xx = x1 - max_x;
    }
    if (yy > y1) {
        yy = y1 + (x1 - xx) / aspect;
    } else {
        yy = y1 - (x1 - xx) / aspect;
    }
}


Here's my patch, which I think produces better behavior than the others posted. One thing in particular it removes is the pop-to-edge seen in the demo when using minSize and switching sides near the edge.

@@ -578,44 +578,36 @@
         }

         // Magic %-)
-        if (xx > x1) { // right side
+        if (xx >= x1) { // right side
           if (xx - x1 < min_x) {
             xx = x1 + min_x;
           } else if (xx - x1 > max_x) {
             xx = x1 + max_x;
           }
-          if (yy > y1) {
+          if (yy >= y1) {
             yy = y1 + (xx - x1) / aspect;
           } else {
             yy = y1 - (xx - x1) / aspect;
           }
-        } else if (xx < x1) { // left side
+        } else { // left side
           if (x1 - xx < min_x) {
             xx = x1 - min_x;
           } else if (x1 - xx > max_x) {
             xx = x1 - max_x;
           }
-          if (yy > y1) {
+          if (yy >= y1) {
             yy = y1 + (x1 - xx) / aspect;
           } else {
             yy = y1 - (x1 - xx) / aspect;
           }
         }

-        if (xx < 0) {
-          x1 -= xx;
-          xx = 0;
-        } else if (xx > boundx) {
-          x1 -= xx - boundx;
-          xx = boundx;
+        if (xx < 0 || xx > boundx) {
+          xx = x1 + (x1 - xx)
         }

-        if (yy < 0) {
-          y1 -= yy;
-          yy = 0;
-        } else if (yy > boundy) {
-          y1 -= yy - boundy;
-          yy = boundy;
+        if (yy < 0 || yy > boundy) {
+          yy = y1 + (y1 - yy)
         }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜