选择类型或类名称kineticjs形状,我们可以使用的类型或类名称get()方法;该方法返回的get()选择器字符串匹配节点数组。返回的数组是一个动力学。收藏阵列,这基本上是一个特殊的each()方法典型的JavaScript数组。该each()方法使我们能够迅速遍历数组中的每个节点。
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#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 rectangles">
</div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.6.0.min.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
for(var n = 0; n < 10; n++) {
var circle = new Kinetic.Circle({
x: Math.random() * stage.getWidth(),
y: Math.random() * stage.getHeight(),
radius: Math.random() * 50 + 25,
fill: 'red',
strokeWidth: 3
});
layer.add(circle);
}
var rect1 = new Kinetic.Rect({
x: 250,
y: 90,
width: 100,
height: 50,
fill: 'green',
strokeWidth: 3,
offset: {
x: 50,
y: 25
},
draggable: true,
name: 'rectangle'
});
var rect2 = new Kinetic.Rect({
x: 400,
y: 90,
width: 100,
height: 50,
fill: 'green',
strokeWidth: 3,
offset: {
x: 50,
y: 25
},
draggable: true
});
layer.add(rect1);
layer.add(rect2);
stage.add(layer);
document.getElementById('activate').addEventListener('click', function() {
// select shapes by type
var shapes = stage.get('Rect');
// apply transition to all nodes in the array
shapes.each(function(shape) {
new Kinetic.Tween({
node: shape,
duration: 1,
scaleX: Math.random() * 2,
scaleY: Math.random() * 2,
easing: Kinetic.Easings.ElasticEaseOut
}).play();
});
}, false);
</script>
</body>
</html>