我在画布上随机有许多静态方块。这些方块有什么方法可以检测到另一个draggable
图形何时在它们之间发生碰撞并打印 'name' 属性rect
?
这是代码(也可在JSFiddle中找到)
var arrowMode = true;
var stage = new Kinetic.Stage({
container: 'container',
height: 400,
width: 800
});
var arrowsLayer = new Kinetic.Layer();
stage.add(arrowsLayer);
$(stage.getContainer()).on('click',function(e){
if(arrowMode){
arrowMode=false;
var x = stage.getMousePosition().x;
var y = stage.getMousePosition().y;
for(var i=0;i<6;i++){
//Rects will be collide for a circle
var rect = new Kinetic.Rect({
name:'rect'+(i+1),
x: i<3? x + (i)*60:x + (i-3)*60,
y: i<3? y : y +60,
width: 50,
height: 50,
fill:'black'
});
arrowsLayer.add(rect);
}
var circle = new Kinetic.Circle({
draggable: true,
stroke: "black",
fill: "#0FF30F",
strokeWidth: 2,
radius: 10,
x:x+400,
y:y+100
});
arrowsLayer.add(circle);
stage.add(arrowsLayer);
}
});