跳到主要内容

深度自定义 Konva Transformer 样式

使用 anchorStyleFunc 属性,可以更精细地控制 Konva.Transformer 的锚点样式。

对于较简单的用例,请参阅 Transformer 样式

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);