zhibin 6 anni fa
parent
commit
99d60045a1
3 ha cambiato i file con 180 aggiunte e 67 eliminazioni
  1. 31 5
      index.html
  2. 134 51
      js/CAD.js
  3. 15 11
      model/data.js

+ 31 - 5
index.html

@@ -1,35 +1,61 @@
 <!DOCTYPE html>
 <html>
+
 <head>
   <meta charset="utf-8" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge" />
   <title>Page Title</title>
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <style>
-    *{
+    * {
       margin: 0;
       padding: 0;
       border: 0;
     }
-    html, body {
+
+    html,
+    body {
       width: 100%;
       height: 100%;
       overflow: hidden;
       background-color: rgba(0, 0, 0, 0.5)
     }
+
+    canvas {
+      width: 600px;
+      height: 600px;
+    }
   </style>
 </head>
+
 <body>
-  <canvas id="myCanvas" width="700" height="700" >
+  <div id="asdas">
+    <canvas id="myCanvas" width="600" height="600"></canvas>
+  </div>
+  <!-- <canvas id="myCanvas" width="1400" height="1400" >
     Your browser does not support the HTML5 canvas tag.
-  </canvas>
+  </canvas> -->
 
   <script src="js/CAD.js"></script>
   <script src="model/data.js"></script>
   <script>
     var $canvas = document.getElementById("myCanvas")
     var cad = grendCAD($canvas, wallData, wallCamera)
-    cad.position({ x: -5.1421212121212125, y: -4.539151515151516 }, 5)
+    cad.position({x: -5, y: -4.5}, 5)
+
+    wallData[1].otherobjs = [{
+      id: "door_1",
+      points2d: [{
+        "x": 1.62,
+        "y": -4.124
+      }, {
+        x: 3.845,
+        y: -4.707333333333333
+      }]
+    }]
+
+    cad.updateComponents()
   </script>
 </body>
+
 </html>

+ 134 - 51
js/CAD.js

@@ -475,7 +475,7 @@ var grendCAD = (function grentWall() {
 
 
     function Wall(data, cameras) {
-      this.lineWidth = 8
+      this.lineWidth = 4
       this.cameras = cameras
       this.stateStack = []
       var faces = {}
@@ -568,18 +568,8 @@ var grendCAD = (function grentWall() {
       line.points[1].y = lastLine.points[0].y
     }
 
-    Wall.prototype._move = function (point, prevPoint, movePosition, renderer) {
-      var xdis = point.x - prevPoint.x
-      var ydis = point.y - prevPoint.y
+    Wall.prototype.detection = function (movePosition) {
       var revoke = false
-      var dire = movePosition.dire[0] - movePosition.dire[1] > 0 ? 'X' : 'Y'
-
-      if (dire === 'X') {
-        this.updateLine(movePosition.line, xdis, 0)
-      } else {
-        this.updateLine(movePosition.line, 0, ydis)
-      }
-
       var activeFacePoint = getFacePoints(movePosition.face)
       for (let key in this.faces) {
         if (this.faces[key] !== movePosition.face &&
@@ -599,6 +589,22 @@ var grendCAD = (function grentWall() {
         }
       }
 
+      return revoke
+    }
+
+    Wall.prototype._move = function (point, prevPoint, movePosition, renderer) {
+      var xdis = point.x - prevPoint.x
+      var ydis = point.y - prevPoint.y
+      var dire = movePosition.dire[0] - movePosition.dire[1] > 0 ? 'X' : 'Y'
+
+      if (dire === 'X') {
+        this.updateLine(movePosition.line, xdis, 0)
+      } else {
+        this.updateLine(movePosition.line, 0, ydis)
+      }
+
+      var revoke = this.detection(movePosition)
+
 
       if (revoke) {
         if (dire === 'X') {
@@ -697,9 +703,9 @@ var grendCAD = (function grentWall() {
     function Position(point, angle) {
       this.point = point
       this.angle = angle
-      this.withinW = 8
-      this.abroadW = 4
-      this.lightW = 14
+      this.withinW = 6
+      this.abroadW = 2
+      this.lightW = 12
       this.lightrRdian = 1 / 2.5 * Math.PI
     }
 
@@ -746,7 +752,7 @@ var grendCAD = (function grentWall() {
     function Door(line, point) {
       this.line = line
       this.point = point
-      this.lineWidth = 8
+      this.lineWidth = 4
       this.color = 'rgba(0,200,175,1)'
     }
 
@@ -814,7 +820,7 @@ var grendCAD = (function grentWall() {
       count++
       this.line = line
       this.points = points
-      this.lineWidth = 8
+      this.lineWidth = 4
       this.color = 'rgba(0,200,175,1)'
     }
 
@@ -944,7 +950,7 @@ var grendCAD = (function grentWall() {
       count++
       this.line = line
       this.points = points
-      this.lineWidth = 8
+      this.lineWidth = 4
       this.color = '#202123'
     }
 
@@ -1097,6 +1103,13 @@ var grendCAD = (function grentWall() {
       this.context.translate(this.width / 2, this.height / 2);
       this.screenOffset = getPosition(this.canvas)
 
+      this.section = {
+        minx: -this.width / 2,
+        maxx: this.width / 2,
+        miny: -this.height / 2,
+        maxy: this.height / 2
+      }
+
       var points = []
       this.origin.forEach(function (line) {
         points = points.concat(...line.points)
@@ -1131,6 +1144,7 @@ var grendCAD = (function grentWall() {
      * 画布点位转化为真实点位
      */
     Renderer.prototype.transRealPoint = function (point) {
+      console.log(point)
       return {
         x: (point.x * this.status.prop) / (this.status.range * this.width / 2) - this.status.offset.x,
         y: (point.y * this.status.prop) / (this.status.range * this.height / 2) - this.status.offset.y
@@ -1140,12 +1154,6 @@ var grendCAD = (function grentWall() {
 
     Renderer.prototype.regEvent = function () {
       var sinfo, model;
-      var section = {
-        minx: -this.width / 2,
-        maxx: this.width / 2,
-        miny: -this.height / 2,
-        maxy: this.height / 2
-      }
       var startPoint = null
       var prevPoint = null
 
@@ -1153,7 +1161,7 @@ var grendCAD = (function grentWall() {
         down(offset) {
           offset.x = (offset.x - this.screenOffset.x) * this.wMultiple
           offset.y = (offset.y - this.screenOffset.y) * this.hMultiple
-          startPoint = this.transRealPoint(util.tranPoint(offset, section))
+          startPoint = this.transRealPoint(util.tranPoint(offset, this.section))
           prevPoint = startPoint
 
           var models = Object.assign([], this.models).sort(function(a, b) {
@@ -1173,7 +1181,7 @@ var grendCAD = (function grentWall() {
         move(move) {
           move.x = (move.x - this.screenOffset.x) * this.wMultiple
           move.y = (move.y - this.screenOffset.y) * this.hMultiple
-          var point = this.transRealPoint(util.tranPoint(move, section))
+          var point = this.transRealPoint(util.tranPoint(move, this.section))
           model._move(point, prevPoint, sinfo, this, this.context)
           prevPoint = point
         },
@@ -1253,7 +1261,11 @@ var grendCAD = (function grentWall() {
   }
 
   function update2d(wall, line) {
+    if (line.___loadUpdate) {
+      return;
+    }
     var update = line.update
+    line.___loadUpdate = true
     line.update = function (dirs, len) {
       var args = [
         {x: 0, y: 0},
@@ -1269,30 +1281,89 @@ var grendCAD = (function grentWall() {
     }
   }
 
-  return function (dom, line2Ds, cameras) {
+  /**
+   * 装载门窗,并拦截线段移动碰到门窗
+   * @param {*} line2Ds 
+   * @param {*} wall 
+   */
+  function loadComponents(line2Ds, wall, renderer) {
     var doors = []
     var casements = []
-    var wall = new Wall(line2Ds, cameras)
 
-    line2Ds.forEach(function(line) {
-      if (line.doors) {
-        doors.push.apply(doors, 
-          line.doors.map(function(door) {
-            return new Door(line, door.points)
-          })
-        )
-      }
+    line2Ds.forEach(function (line) {
+      line.__doors = line.__doors || []
+      line.__casements = line.__casements || []
 
-      if (line.casements) {
-        casements.push.apply(casements,
-          line.casements.map(function (casements) {
-            return new Casement(line, casements.points)
-          })
-        )
-      }
+      
+      line.otherobjs && line.otherobjs.forEach(function (obj) {
+        function check(_obj) {
+          return _obj.origin === obj
+        }
+        if (~line.__doors.findIndex(check) || ~line.__casements.findIndex(check)) return;
+
+        if (~obj.id.indexOf('door')) {
+          var door = new Door(line, obj.points2d)
+          door.origin = obj
+          // 创建线段中的门
+          line.__doors.push(door)
+          doors.push(door)
+        } else if (~obj.id.indexOf('window')) {
+          var casement = new Casement(line, obj.points2d)
+          casement.origin = obj
+          // 创建线段中的门
+          line.__casements.push(casement)
+          casements.push(casement)
+        }
+
+        var update = obj.update2d
+        obj.update2d = function() {
+          update && update()
+          renderer.render()
+        }
+      })
+
+      // 3D改变同步状态改变2D方法注册
       update2d(wall, line)
     })
 
+
+    if (!wall.__additional__detection) {
+      // 创建拦截,拦截移动墙面碰到门窗
+      wall.__additional__detection = true
+      var detection = wall.detection.bind(wall)
+      wall.detection = function (movePos) {
+        if (detection(movePos)) {
+          return true
+        }
+
+        var line = movePos.line
+        // var objs = [(line.__doors || []).concat(line.__casements || [])]
+        var objs = []
+        var reject = false
+
+        for (var i = 0; i < line2Ds.length; i++) {
+          if (line2Ds[i].id === line.linkedLineID[0] || line2Ds[i].id === line.linkedLineID[1]) {
+            line2Ds[i].__doors && objs.push.apply(objs, line2Ds[i].__doors)
+            line2Ds[i].__casements && objs.push.apply(objs, line2Ds[i].__casements)
+          }
+        }
+        objs.forEach(function (obj) {
+          if (!obj.detection()) {
+            reject = true
+          }
+        })
+        return reject
+      }
+    }
+
+    return doors.concat(casements)
+  }
+
+  return function (dom, line2Ds, cameras) {
+    var renderer = new Renderer(dom, line2Ds)
+    var wall = new Wall(line2Ds, cameras)
+
+    // 当墙面位置发生改变时同步3D
     wall.listen('changePosition', function(line2D) {
       line2Ds.forEach(function (line) {
         if (line.id === line2D.linkedLineID[0] ||
@@ -1303,19 +1374,30 @@ var grendCAD = (function grentWall() {
       update3D(line2D)
     })
 
-    doors.concat(casements).forEach(function (obj) {
-      obj.listen('changePoints', function() {
-        update3D(obj.line)
-      })
-    })
 
-    var renderer = new Renderer(dom, line2Ds)
     renderer.models.push(wall)
-    renderer.models.push.apply(renderer.models, doors.concat(casements))
     renderer.render()
 
+    function load () {
+      var objs = loadComponents(line2Ds, wall, renderer)
+      // 当门窗位置大小发生改变时同步3D
+      objs.forEach(function (obj) {
+        obj.listen('changePoints', function () {
+          if (obj.origin.update3D) {
+            obj.origin.update3D()
+          } else {
+            console.log(obj.origin, '组件未绑定更新3D方法')
+          }
+        })
+      })
+      renderer.models.push.apply(renderer.models, objs)
+      renderer.render()
+    }
+    load();
+
     var position;
     return {
+      // 设置当前位置
       position: function (posPoint, angle) {
         if (!position) {
           position = new Position(posPoint, angle)
@@ -1326,7 +1408,8 @@ var grendCAD = (function grentWall() {
         }
         renderer.render()
       },
-      renderer: renderer
+      renderer: renderer,
+      updateComponents: load
     }
   }
 })();

+ 15 - 11
model/data.js

@@ -9,15 +9,18 @@ var wallData = [{
       "y": -4.124
     }],
     "linkedLineID": ["0_6", "0_1"],
-    "casements": [{
-      "points": [{
-        "x": 1.62,
-        "y": 0.501
-      }, {
-        "x": 1.62,
-        "y": -1
-      }]
-    }]
+    "otherobjs": [
+      {
+        id: "window_1",
+        points2d: [{
+          "x": 1.62,
+          "y": 0.501
+        }, {
+          "x": 1.62,
+          "y": -1
+        }]
+      }
+    ]
   },
   {
     "id": "0_1",
@@ -30,8 +33,9 @@ var wallData = [{
       "y": -5.874
     }],
     "linkedLineID": ["0_0", "0_2"],
-    "doors": [{
-      "points": [{
+    "otherobjs1": [{
+      id: "door_1",
+      points2d: [{
         "x": 1.62,
         "y": -4.124
       }, {