PIXI.draggable
PIXI.draggable copied to clipboard
containment must be direct parent of draggables
for PIXI.draggable to work all draggable elements must direct children of the containment.
Would be great if it would be more flexible
It should be possible to define a hitArea for that instead of using a Sprite/Object as containment. If there already is a hitArea defined then you could use:
containment: object.hitArea
if not:
containment: new PIXI.Rectangle(x,y,width,height)
If that doesn't work then I'll have to look at it again.
As an example, in onDragMove only the width and height of the containment are considered, not the x and y values, so the restriction is always from 0 to containment.width/height in the local space of the dragged element.
I did the following to fix the issue FabianElsmer is describing. This also makes the containment take any anchor point set on the object (Sprite) into account:
// x bounds
if(options.axis !== 'y')
{
var anchorXPixelOffset = (item.anchor) ? (item.width * item.anchor.x) : 0;
var upperXBound = containment.width + containment.x - (item.width - anchorXPixelOffset);
var lowerXBound = containment.x + anchorXPixelOffset;
if (x < lowerXBound)
{
x = lowerXBound;
}
else if (x > upperXBound)
{
x = upperXBound;
}
}
// y bounds
if(options.axis !== 'x')
{
var anchorYPixelOffset = (item.anchor) ? (item.height * item.anchor.y) : 0;
var upperYBound = containment.height + containment.y - (item.height - anchorYPixelOffset);
var lowerYBound = containment.y + anchorYPixelOffset;
if (y < lowerYBound)
{
y = lowerYBound;
}
else if (y > upperYBound)
{
y = upperYBound;
}
}