// 吸附
function autoTile(arr, obj, flag) {
if (!flag) {
return
}
var sx = 0; // space x
var sy = 0; // space y
var offset = {}
for (var i in arr) {
if (arr[i] === obj) {
continue
}
sx = arr[i].x - obj.x;
sy = arr[i].y - obj.y;
if ((sx-obj.width) >= 0) { // 目标矩形在吸附对象的最左边
if (sx-obj.width <= activeAutoTile) {
if (sy-obj.height>=0 && sy-obj.height<=activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________1", arr[i].id)
break
} else if (sy >= 0 && sy < obj.height) {
if (sy <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________2", arr[i].id)
break
} else if (Math.abs(sy-obj.height)<=activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________3", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________4", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________5", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________6", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________7", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________8", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________9", arr[i].id)
break
}
}
}
} else if (sx < obj.width && sx >= 0) { // 吸附对象在目标矩形的内部
if (Math.abs(sx-obj.width) <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________10", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________11", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________12", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________13", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________14", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________15", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________16", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________17", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________18", arr[i].id)
break
}
}
} else if (sx <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________19", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________20", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________21", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________22", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________23", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________24", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________25", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________26", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________27", arr[i].id)
break
}
}
} else if ((obj.width-(sx+arr[i].width)) <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________28", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________29", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________30", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________31", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
// offset.y = 0
// dragOffset(offset)
// //console.log("___________32", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________33", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________34", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
// offset.y = 0
// dragOffset(offset)
// //console.log("___________35", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-(obj.width+obj.x)
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________36", arr[i].id)
break
}
}
} else {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________37", arr[i].id)
// break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________38", arr[i].id)
// break
} else if (sy <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.y
// dragOffset(offset)
// //console.log("___________39", arr[i].id)
// break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________40", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.y
// dragOffset(offset)
// //console.log("___________41", arr[i].id)
// break
} else if (Math.abs(sy) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.y
// dragOffset(offset)
// //console.log("___________42", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.y
// dragOffset(offset)
// //console.log("___________43", arr[i].id)
// break
}
}
}
} else if (sx < 0 && Math.abs(sx) <= arr[i].width) { // 目标矩形在吸附对象的右边,两者横坐标相距不超过吸附对象宽度
if (Math.abs(Math.abs(sx)-arr[i].width) <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________44", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________45", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________46", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________47", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________48", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________49", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________50", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________51", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________52", arr[i].id)
break
}
}
} else if (Math.abs(sx) <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________53", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________54", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________55", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________56", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________57", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________58", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________59", arr[i].id)
break
} else {
// offset.x = arr[i].x-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________60", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________61", arr[i].id)
break
}
}
} else {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________62", arr[i].id)
// break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________63", arr[i].id)
// break
} else if (sy <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.y
// dragOffset(offset)
// //console.log("___________64", arr[i].id)
// break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.height-obj.y
// dragOffset(offset)
// //console.log("___________65", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.y
// dragOffset(offset)
// //console.log("___________66", arr[i].id)
// break
} else if (Math.abs(sy) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y-obj.y
// dragOffset(offset)
// //console.log("___________67", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
// offset.x = 0
// offset.y = arr[i].y+arr[i].height-obj.y
// dragOffset(offset)
// //console.log("___________68", arr[i].id)
// break
}
}
}
} else if (sx < 0 && Math.abs(sx) > arr[i].width) { // 目标矩形在吸附对象的最右边
if (Math.abs(Math.abs(sx)-arr[i].width) <= activeAutoTile) {
if (sy-obj.height >= 0 && sy-obj.height <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________69", arr[i].id)
break
} else if (sy >=0 && sy < obj.height) {
if (Math.abs(sy-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.height-obj.y
dragOffset(offset)
//console.log("___________70", arr[i].id)
break
} else if (sy <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________71", arr[i].id)
break
} else if (Math.abs(sy+arr[i].height-obj.height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.height-obj.y
dragOffset(offset)
//console.log("___________72", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________73", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) <= arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________74", arr[i].id)
break
} else if (Math.abs(sy) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y-obj.y
dragOffset(offset)
//console.log("___________75", arr[i].id)
break
} else {
// offset.x = arr[i].x+arr[i].width-obj.x
// offset.y = 0
// dragOffset(offset)
// //console.log("___________76", arr[i].id)
// break
}
} else if (sy < 0 && Math.abs(sy) > arr[i].height) {
if (Math.abs(Math.abs(sy)-arr[i].height) <= activeAutoTile) {
offset.x = arr[i].x+arr[i].width-obj.x
offset.y = arr[i].y+arr[i].height-obj.y
dragOffset(offset)
//console.log("___________77", arr[i].id)
break
}
}
}
}
}
}