@@ -478,12 +478,6 @@ var Frame = new Class({
478478 var rw = this . realWidth ;
479479 var rh = this . realHeight ;
480480
481- // x = Clamp(x, 0, cw);
482- // y = Clamp(y, 0, ch);
483-
484- // width = Clamp(width, 0, cw - x);
485- // height = Clamp(height, 0, ch - y);
486-
487481 x = Clamp ( x , 0 , rw ) ;
488482 y = Clamp ( y , 0 , rh ) ;
489483
@@ -492,6 +486,8 @@ var Frame = new Class({
492486
493487 var ox = cx + x ;
494488 var oy = cy + y ;
489+ var ow = width ;
490+ var oh = height ;
495491
496492 if ( flipX )
497493 {
@@ -503,20 +499,55 @@ var Frame = new Class({
503499 oy = cy + ( ch - y - height ) ;
504500 }
505501
506- // Check ox/oy within cut region, otherwise make UVs empty
502+ var data = this . data ;
507503
508- if ( this . data . trim )
504+ if ( data . trim )
509505 {
506+ var ss = data . spriteSourceSize ;
507+
510508 // Need to check for intersection between the cut xywh and ox
511509 // If there is none, we set UV to be empty, otherwise set it to be the intersection rect
512510
513- // return !(rectA.right < rectB.x || rectA.bottom < rectB.y || rectA.x > rectB.right || rectA.y > rectB.bottom);
511+ // The cut region
512+ var rectA = { x : ss . x , y : ss . y , w : ss . w , h : ss . h , right : ss . x + ss . w , bottom : ss . y + ss . h } ;
513+
514+ // The crop region
515+ var rectB = { x : x , y : y , w : width , h : height , right : x + width , bottom : y + height } ;
516+
517+ var intersects = ! ( rectA . right < rectB . x || rectA . bottom < rectB . y || rectA . x > rectB . right || rectA . y > rectB . bottom ) ;
514518
515- // out.x = Math.max(rectA.x, rectB.x);
516- // out.y = Math.max(rectA.y, rectB.y);
517- // out.width = Math.min(rectA.right, rectB.right) - out.x;
518- // out.height = Math.min(rectA.bottom, rectB.bottom) - out.y;
519+ window . rectA = rectA ;
520+ window . rectB = rectB ;
521+
522+ if ( intersects )
523+ {
524+ var rx = Math . max ( rectA . x , rectB . x ) ;
525+ var ry = Math . max ( rectA . y , rectB . y ) ;
526+ var rw = Math . min ( rectA . right , rectB . right ) - rx ;
527+ var rh = Math . min ( rectA . bottom , rectB . bottom ) - ry ;
528+
529+ var rectC = { x : rx , y : ry , w : rw , h : rh , right : rx + rw , bottom : ry + rh } ;
530+
531+ window . rectC = rectC ;
532+
533+ ox = cx + ( rx - ss . x ) ;
534+ oy = cy + ( ry - ss . y ) ;
535+ ow = rw ;
536+ oh = rh ;
519537
538+ x = rx ;
539+ y = ry ;
540+
541+ width = rw ;
542+ height = rh ;
543+ }
544+ else
545+ {
546+ ox = 0 ;
547+ oy = 0 ;
548+ ow = 0 ;
549+ oh = 0 ;
550+ }
520551 }
521552
522553 var tw = this . source . width ;
@@ -526,17 +557,17 @@ var Frame = new Class({
526557
527558 crop . u0 = Math . max ( 0 , ox / tw ) ;
528559 crop . v0 = Math . max ( 0 , oy / th ) ;
529- crop . u1 = Math . min ( 1 , ( ox + width ) / tw ) ;
530- crop . v1 = Math . min ( 1 , ( oy + height ) / th ) ;
560+ crop . u1 = Math . min ( 1 , ( ox + ow ) / tw ) ;
561+ crop . v1 = Math . min ( 1 , ( oy + oh ) / th ) ;
531562
532- crop . width = width ;
533- crop . height = height ;
563+ crop . cx = cx + x ;
564+ crop . cy = cy + y ;
534565
535566 crop . x = x ;
536567 crop . y = y ;
537568
538- crop . cx = cx + x ;
539- crop . cy = cy + y ;
569+ crop . width = width ;
570+ crop . height = height ;
540571
541572 crop . flipX = flipX ;
542573 crop . flipY = flipY ;
0 commit comments