JSFiddle - React, Tailwind, and code Playground
by m1erickson
HTML
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.min.js"></script>
<h4>Drag blue bubble (children also move)<br>Children can drag separately also</h4>
<div id="container"></div>
CSS
body{padding:20px;}
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:350px;
height:350px;
}
JavaScript
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();