Skip to main content

Drag and Drop Multiple Shapes

Instructions: Drag and drop the shapes or remove them by double clicking or double tapping.

import Konva from 'konva';

const width = window.innerWidth;
const height = window.innerHeight;

const stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});

const layer = new Konva.Layer();

const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];

for (let i = 0; i < 6; i++) {
const box = new Konva.Rect({
x: i * 30 + 50,
y: i * 18 + 40,
fill: colors[i],
stroke: 'black',
strokeWidth: 4,
draggable: true,
width: 100,
height: 50,
});

box.on('dragstart', function () {
this.moveToTop();
});

box.on('dragmove', function () {
document.body.style.cursor = 'pointer';
});

// dblclick to remove box for desktop app
// and dbltap to remove box for mobile app
box.on('dblclick dbltap', function () {
this.destroy();
});

box.on('mouseover', function () {
document.body.style.cursor = 'pointer';
});

box.on('mouseout', function () {
document.body.style.cursor = 'default';
});

layer.add(box);
}

// add the layer to the stage
stage.add(layer);