箱体吸附算法

// 吸附
    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
                        }
                    }
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值