javascript - KineticJS drag multiple images without grouping them -
is possible drag children elements of layer without grouping them?
i have different layers image-elements , path shape. path draws bubble around images. if user drag bubble place, images should move there, too.
but should possible drag single element. @ moment, possible drag single element , drag bubble, there no combination...
yes, can drag "related" nodes when "container" node dragged:
add class children of bubble (for example, each child's name:"bubble1")
on bubble
dragstart, save bubble's starting positionon bubble
dragmove, calculate how far bubble has been dragged , reposition related elements same distance
example code , 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>
Comments
Post a Comment