Skip to main content

Deep Style Konva Transformer

You can use anchorStyleFunc property of Konva.Transformer to have deeper control on styling of anchors.

Also take a look into Transformer Styling for simpler use cases.

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();
stage.add(layer);

const rect = new Konva.Rect({
x: 50,
y: 50,
width: 100,
height: 100,
fill: 'yellow',
stroke: 'black',
draggable: true,
});
layer.add(rect);

const tr = new Konva.Transformer({
nodes: [rect],
anchorStyleFunc: (anchor) => {
// make all anchors circles

anchor.cornerRadius(50);
// make all anchors red

anchor.fill('red');

    // make right-middle bigger

    if (anchor.hasName('middle-right')) {
      anchor.scale({ x: 2, y: 2 });
    }
    // make top-left invisible

    if (anchor.hasName('top-left')) {
      anchor.scale({ x: 0, y: 0 });
    }

},
});
layer.add(tr);


</rewritten_file>