HTML5 Canvas 按 id 选择形状教程

要使用 Konva 按 id 选择形状,我们可以使用 find() 方法和 # 选择器。
find() 方法始终返回一个元素数组,即使我们期待它返回一个元素。
如果你只需要一个元素,可以使用 findOne() 方法。
find() 方法适用于任何节点,包括舞台、图层、组和形状。

说明:按下 “激活矩形” 按钮以按 id 选择矩形并执行过渡。你也可以拖放矩形。

Konva 按 id 选择形状演示view raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/konva@9.3.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Select Shape by id Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}

#buttons {
position: absolute;
top: 5px;
left: 10px;
}

#buttons > input {
padding: 10px;
display: block;
margin-top: 5px;
}
</style>
</head>

<body>
<div id="container"></div>
<div id="buttons">
<input type="button" id="activate" value="Activate rectangle" />
</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();
for (var n = 0; n < 10; n++) {
var circle = new Konva.Circle({
x: Math.random() * stage.width(),
y: Math.random() * stage.height(),
radius: Math.random() * 50 + 25,
fill: 'red',
strokeWidth: 3,
stroke: 'black',
});

layer.add(circle);
}

var rect = new Konva.Rect({
x: 300,
y: 90,
width: 100,
height: 50,
fill: 'green',
strokeWidth: 3,
offset: {
x: 50,
y: 25,
},
draggable: true,
id: 'myRect',
});

layer.add(rect);
stage.add(layer);

var tween;

document.getElementById('activate').addEventListener(
'click',
function () {
// or var shape = stage.findOne('#myRect');
var shape = stage.find('#myRect')[0];

if (tween) {
tween.destroy();
}

tween = new Konva.Tween({
node: shape,
duration: 1,
scaleX: Math.random() * 2,
scaleY: Math.random() * 2,
easing: Konva.Easings.ElasticEaseOut,
}).play();
},
false
);
</script>
</body>
</html>