禁用变换

获取Konva最新的信息

如果你想阻止图形变换,可以执行 Konva.Transformer 实例的 stopTransform 方法。

说明:试试变换下面图形,当图形宽度大于 200 时图形将不会再变化。

Konva Stop Transform Demoview raw
<!DOCTYPE html>
<html>
<head>
<!-- USE DEVELOPMENT VERSION -->
<script src="https://unpkg.com/konva@4.0.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Stop Transform Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>

<body>
<div id="container"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

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

var rect = new Konva.Rect({
x: 160,
y: 60,
width: 100,
height: 90,
fill: 'red',
name: 'rect',
stroke: 'black',
draggable: true
});
layer.add(rect);

// create new transformer
var tr = new Konva.Transformer();
layer.add(tr);
tr.attachTo(rect);
layer.draw();

tr.on('transform', function() {
var width = rect.width() * rect.scaleX();
if (width > 200) {
tr.stopTransform();
// reset visible width to 200
// so future transform is possible
var scaleX = 200 / rect.width();
rect.scaleX(scaleX);
}
});
</script>
</body>
</html>