Skip to main content

HTML5 Canvas Drag and Drop the Stage

To drag and drop a stage with Konva, we can set the draggable property of the config object to true when the group is instantiated, or we can use the draggable() method.

Unlike drag and drop for other nodes, such as shapes, groups, and layers, we can drag the entire stage by dragging any portion of the stage.

import Konva from 'konva';

const stage = new Konva.Stage({
  container: 'container',
  width: window.innerWidth,
  height: window.innerHeight,
  draggable: true
});

const layer = new Konva.Layer();
stage.add(layer);

// create circle
const circle = new Konva.Circle({
  x: stage.width() / 2,
  y: stage.height() / 2,
  radius: 70,
  fill: 'red',
  stroke: 'black',
  strokeWidth: 4
});

// create text
const text = new Konva.Text({
  x: 10,
  y: 10,
  text: 'Drag the stage anywhere',
  fontSize: 20,
  fontFamily: 'Calibri',
  fill: 'black'
});

layer.add(circle);
layer.add(text);