Understanding Node zIndex
What is zIndex of a node?
You can get/set zIndex of a node in this way:
// get
const zIndex = shape.zIndex();
// set
shape.zIndex(1);
zIndex is just the index of a node in its parent's children array. Please don't confuse zIndex
in Konva with z-index
in CSS.
const group = new Konva.Group();
const circle = new Konva.Circle({});
group.add(circle);
// it will log 0
console.log(circle.zIndex());
// the next line will not work because the group has only one child
circle.zIndex(1);
// still logs 0
console.log(circle.zIndex());
// for any node this equation will be true:
console.log(circle.zIndex() === circle.getParent().children.indexOf(circle));
You can't use zIndex
to set absolute position of the node, like we do in CSS.
Konva draws nodes in the strict order as they are defined in the nodes tree.
Instructions: Try to change zIndex of shapes using the buttons. Notice how the order of shapes changes.
- Vanilla
- React
- Vue
</rewritten_file>