等比缩放

获取Konva最新的信息

怎么等比缩放图形?

默认情况下拖动控件四个角上的控制点缩放是等比例的,如果你不想拖动它们时等比缩放,可以设置 keepRatio 属性为 false来关闭此特性。

同时即使设置了 keepRatio 属性为 false,也可以在拖动控制点时按住 SHIFT 保持等比缩放。

说明: 试试缩放下面的文字

Konva Shape transform and selection 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 Keep Ratio 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 text1 = new Konva.Text({
x: 50,
y: 70,
fontSize: 30,
text: 'keepRatio = true',
draggable: true
});
layer.add(text1);

var tr1 = new Konva.Transformer({
node: text1,
keepRatio: true,
enabledAnchors: ['top-left', 'top-right', 'bottom-left', 'bottom-right']
});
layer.add(tr1);

var text2 = new Konva.Text({
x: 50,
y: 200,
fontSize: 30,
text: 'keepRatio = false',
draggable: true
});
layer.add(text2);

var tr2 = new Konva.Transformer({
node: text2,
keepRatio: false,
enabledAnchors: ['top-left', 'top-right', 'bottom-left', 'bottom-right']
});
layer.add(tr2);

layer.draw();
</script>
</body>
</html>