JSFiddle - React, Tailwind, and code Playground

by EveryoneMustWin

HTML

<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.4.3.min.js"></script>
Stage
<div id="container"></div>

CSS

#container {
    position: absolute;
    top: 50px;
    left: 20px;
    width: 300px;
    height: 300px;
    background-color: #faa;
    border: 2px solid;
}

JavaScript

var kItems = {};

kItems.stage = new Kinetic.Stage({
    container: 'container',
    width: $("#container").width(),
    height: $("#container").height()
});

kItems.lowerLayer = new Kinetic.Layer({
    scale: 1.0,
    id: "lower",
    draggable: true,
    dragBoundFunc: function(pos) {
        return {
            x: pos.x,
            y: pos.y
        }
    }
});

kItems.upperLayer = new Kinetic.Layer({
    scale: 1.0,
    id: "upper"
});

kItems.stage.add(kItems.lowerLayer);
kItems.stage.add(kItems.upperLayer);

kItems.backdrop = new Kinetic.Rect({
    x: 50, 
    y: 50, 
    width: 200, 
    height: 200,
    stroke: '#822',
    strokeWidth: 2,
    fill: '#b62',
    opacity: 1,
    id: 'backdrop'
});

kItems.lowerLayer.add(kItems.backdrop);

kItems.block1 = new Kinetic.Rect({
    x: 100,
    y: 100,
    width: 20,
    height: 20,
    stroke: '#228',
    strokeWidth: 2,
    fill: '#65b',
    opacity: 1,
    id: 'block1',
    draggable: true
});

kItems.upperLayer.add(kItems.block1);

kItems.block2 = new Kinetic.Rect({
    x: 180,
    y: 100,
    width: 20,
    height: 20,
    stroke: '#228',
    strokeWidth: 2,
    fill: '#65b',
    opacity: 1,
    id: 'block2',
    draggable: true
});

kItems.upperLayer.add(kItems.block2);

kItems.lowerLayer.draw();
kItems.upperLayer.draw();

kItems.lowerLayer.on("dragmove", function() {
    kItems.upperLayer.setAttrs({x:this.getX(), y:this.getY()});
    kItems.upperLayer.draw();
});