kangax/fabric.js — Issue #128: Object.isContainedWithinObject() doesn't work when moving
Author: jeffcrow
Created: 2012-03-05 19:48:11+00:00
State: open

I need functionality in fabric where the system can determine when you are dragging certain elements over certain other elements. Currently, fabric.Object.isContainedWithinObject() uses isContainedWithinRect(), which uses oCoords to determine this, but the oCoords aren't updated until movement stops on mouse down.

I wrote a new version of the function that works when an object is moving that uses left/top and width/height/scale to determine containing objects since those are updated as an object is moved. The code is pretty straightforward:

```
isContainedWithinRect: function(selectionTL, selectionBR) {
var scaledTargetWidth = this.width*this.scaleX;
var scaledTargetHeight = this.height*this.scaleY;

var tl = new fabric.Point(this.left-(scaledTargetWidth/2), this.top-(scaledTargetHeight/2)),
tr = new fabric.Point(this.left+(scaledTargetWidth/2), this.top-(scaledTargetHeight/2)),
bl = new fabric.Point(this.left-(scaledTargetWidth/2), this.top+(scaledTargetHeight/2)),
br = new fabric.Point(this.left+(scaledTargetWidth/2), this.top+(scaledTargetHeight/2));

return tl.x > selectionTL.x && tr.x < selectionBR.x && tl.y > selectionTL.y && bl.y < selectionBR.y;
},
```
I'm not sure if it takes into account the angle, I haven't tested any objects that are rotated a lot.