Skip to main content

Shape Groups

To group multiple shapes together with Konva, we can instantiate a Konva.Group() object and then add shapes to it with the add() method. Grouping shapes together is really handy when we want to transform multiple shapes together, e.g. if we want to move, rotate, or scale multiple shapes at once. Groups can also be added to other groups to create more complex Node trees.

For a full list of attributes and methods, check out the Konva.Group documentation.

Instructions: Try to drag the group. Notice how all shapes move together.

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 group = new Konva.Group({
x: 50,
y: 50,
draggable: true,
});

const circle = new Konva.Circle({
x: 40,
y: 40,
radius: 30,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});

const rect = new Konva.Rect({
x: 80,
y: 20,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
});

group.add(circle);
group.add(rect);
layer.add(group);
stage.add(layer);