svg中自動吸附靠近的物體
阿新 • • 發佈:2018-01-14
ont pda fse pdu color offset gpo call bsp
var attractSwitch = true;//自動吸附開關,true 為開啟,否則關閉 //矩形物體類別枚舉 var T_PDU = ‘PDU‘; var T_AC = ‘AC‘; var T_CABINET = ‘CABINET‘; var T_FLOOR = ‘FLOOR‘; var magicalLength = 10;//矩形物體邊緣的垂直距離小於等於此長度則自動吸附 var rectsPdu = new ArrayList(); var rectsAC = new ArrayList(); var rectsCabinet = new ArrayList();var rectsFloor = new ArrayList(); /** * 吸附 */ function attract(moveObj) { if (!attractSwitch) { return null; } var findAttractObj = false; var currentXOffset = 10000; var currentYOffset = 10000; for (var i = 0; i < rectsPdu.size(); i++) { //遍歷PDU var item = rectsPdu.get(i);if (!item.equals(moveObj)) { //若不是當前移動的PDU if (moveObj.underneath(item)) {//移動的物體在下側 if (!moveObj.onRight(item) && !moveObj.onLeft(item)) { var length = moveObj.y - (item.y + item.height); if (length <= magicalLength) { findAttractObj= true; if (length < Math.abs(currentYOffset)) { //moveObj.y = moveObj.y - length; currentYOffset = -length; } // return moveObj; } } } else if (moveObj.onLeft(item)) {//移動的物體在左側 if (!moveObj.onTop(item) && !moveObj.underneath(item)) { var length = item.x - (moveObj.x + moveObj.width); if (length <= magicalLength) { findAttractObj = true; if (length < Math.abs(currentXOffset)) { // moveObj.x = moveObj.x + length; currentXOffset = length; } // return moveObj; } } } else if (moveObj.onTop(item)) {//移動的物體在上側 if (!moveObj.onRight(item) && !moveObj.onLeft(item)) { var length = item.y - (moveObj.y + moveObj.height); if (length <= magicalLength) { findAttractObj = true; if (length < Math.abs(currentYOffset)) { //moveObj.y = moveObj.y + length; currentYOffset = length; } // return moveObj; } } } else if (moveObj.onRight(item)) { //移動的物體在右側 if (!moveObj.onTop(item) && !moveObj.underneath(item)) { var length = moveObj.x - (item.x + item.width); if (length <= magicalLength) { findAttractObj = true; if (length < Math.abs(currentXOffset)) { //moveObj.x = moveObj.x - length; currentXOffset = -length; } // return moveObj; } } } else {//移動物體和目標交叉 continue } } } if (findAttractObj) { if (currentXOffset < magicalLength) { moveObj.x = moveObj.x + currentXOffset; } if (currentYOffset < magicalLength) { moveObj.y = moveObj.y + currentYOffset; } return moveObj; } return null; } function add(obj) { if (rectObj.rType == T_PDU) { rectsPdu.add(obj); } else if (rectObj.rType == T_AC) { rectsAC.add(obj); } else if (rectObj.rType == T_CABINET) { rectsCabinet.add(obj); } else if (rectObj.rType == T_FLOOR) { rectsFloor.add(obj); } } function remove(obj) { if (rectObj.rType == T_PDU) { rectsPdu.remove(obj); } else if (rectObj.rType == T_AC) { rectsAC.remove(obj); } else if (rectObj.rType == T_CABINET) { rectsCabinet.remove(obj); } else if (rectObj.rType == T_FLOOR) { rectsFloor.remove(obj); } } function update(obj) { if (rectObj.rType == T_PDU) { rectsPdu.update(obj); } else if (rectObj.rType == T_AC) { rectsAC.update(obj); } else if (rectObj.rType == T_CABINET) { rectsCabinet.update(obj); } else if (rectObj.rType == T_FLOOR) { rectsFloor.update(obj); } } /** * 求兩點之間的距離 * @x1 點1x坐標 * @y1 點1y坐標 * @x2 點2x坐標 * @y2 點2y坐標 */ function getTwoPointLength(x1, y1, x2, y2) { return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)); }
svg中自動吸附靠近的物體