is it possible to drag all children elements of a layer without grouping them?
I have different layers with image-elements and a path shape. The path draws a bubble around the images. If the user drag the bubble to another place, all images should move there, too.
But it should be also possible to drag a single element.
At the moment, it is possible to drag a single element and to drag the bubble, but there is no combination...
Yes, you can drag "related" nodes when a "container" node is dragged:
add a class to all children of a bubble (for example, each child's name:"bubble1")
on bubble dragstart
, save the bubble's starting position
on bubble dragmove
, calculate how far the bubble has been dragged and reposition all related elements by that same distance
Example code and a Demo: http://jsfiddle.net/m1erickson/Zr6TE/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.min.js"></script>
<style>
body{padding:20px;}
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:350px;
height:350px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 350,
height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);
var bubble=new Kinetic.Circle({
x:100,
y:100,
width:100,
height:100,
fill:"blue",
draggable:true,
});
bubble.lastPos;
bubble.on("dragstart",function(){
this.lastPos=this.position();
});
bubble.on("dragmove",function(){
var lastPos=bubble.lastPos;
var pos=bubble.position();
var deltaX=pos.x-lastPos.x;
var deltaY=pos.y-lastPos.y;
layer.find(".bubble1").each(function(child){
child.position({x:child.x()+deltaX, y:child.y()+deltaY} );
});
bubble.lastPos=pos;
layer.draw();
});
layer.add(bubble);
var circle1 = new Kinetic.Circle({
x:80,
y:80,
radius:15,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
draggable: true,
name:"bubble1"
});
layer.add(circle1);
//
var circle2 = new Kinetic.Circle({
x:110,
y:110,
radius:15,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
draggable: true,
name:"bubble1"
});
layer.add(circle2);
//
layer.draw();
}); // end $(function(){});
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>