diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-04-30 18:38:28 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-04-30 18:38:28 -0400 |
| commit | dd51784e8620c97e8df8825fd7f1355297920570 (patch) | |
| tree | af24ab8fae57228e8382201b7369e076be85ddae | |
| parent | ed786fddd4e65f6c9dfb95371d57deb4696273f4 (diff) | |
snap to endpoints from other shapes
| -rw-r--r-- | public/assets/test/ortho3.html | 75 |
1 files changed, 62 insertions, 13 deletions
diff --git a/public/assets/test/ortho3.html b/public/assets/test/ortho3.html index 6bf2619..3ae0204 100644 --- a/public/assets/test/ortho3.html +++ b/public/assets/test/ortho3.html @@ -177,7 +177,7 @@ var PolylineTool = MapTool.extend(function (base) { } // compare to initial point - var p = new vec2( cursor.x.a, cursor.y.a ) + var p = last_point.clone() if (placing) { if (line.canCloseWith(p)) { line.close() @@ -202,6 +202,15 @@ var PolylineTool = MapTool.extend(function (base) { last_point.assign(line.points[0]) cursor.x.a = cursor.x.b = last_point.a cursor.y.a = cursor.y.b = last_point.b + return + } + var end_point = shapes.findClosestEndPoint(last_point) + if (end_point) { + document.body.style.cursor = "pointer" + last_point.assign(end_point.point) + cursor.x.a = cursor.x.b = last_point.a + cursor.y.a = cursor.y.b = last_point.b + return } else { document.body.style.cursor = "crosshair" @@ -308,6 +317,15 @@ var OrthoPolylineTool = MapTool.extend(function (base) { cursor.x.a = cursor.x.b = last_point.a cursor.y.a = cursor.y.b = last_point.b } + return + } + var end_point = shapes.findClosestEndPoint(last_point) + if (end_point) { + document.body.style.cursor = "pointer" + last_point.assign(end_point.point) + cursor.x.a = cursor.x.b = last_point.a + cursor.y.a = cursor.y.b = last_point.b + return } else { document.body.style.cursor = "crosshair" @@ -322,16 +340,35 @@ var OrthoPolylineTool = MapTool.extend(function (base) { }) -// prefer to have this on the object that keeps track of the polylines -function findClosestPoint (p){ - var point - for (var i = 0; i < shapes.length; i++) { - point = shapes[i].hasPointNear(p) - if (point) return { point: point, shape: shapes[i] } +var ShapeList = Fiber.extend(function(base){ + var exports = {} + exports.init = function(){ + this.shapes = [] } - return null -} - + exports.add = function(shape){ + this.shapes.push(shape) + } + exports.findClosestPoint = function (p){ + var point + for (var i = 0; i < this.shapes.length; i++) { + point = this.shapes[i].hasPointNear(p) + if (point) return { point: point, shape: this.shapes[i] } + } + return null + } + exports.findClosestEndPoint = function (p){ + var point + for (var i = 0; i < this.shapes.length; i++) { + point = this.shapes[i].hasEndPointNear(p) + if (point) return { point: point, shape: this.shapes[i] } + } + return null + } + exports.forEach = function(fn){ + this.shapes.forEach(fn) + } + return exports +}) var Polyline = Fiber.extend(function(base){ var exports = {} @@ -368,7 +405,17 @@ var Polyline = Fiber.extend(function(base){ } return null } - + exports.hasEndPointNear = function(p){ + if (this.closed) return null + if (this.firstPoint().distanceTo( p ) < 10/map.zoom) { + return this.firstPoint() + } + if (this.lastPoint().distanceTo( p ) < 10/map.zoom) { + return this.lastPoint() + } + return null + } + exports.hasSegmentNear = function(p, min_dist){ var p1, p2, d1, d2, sum, rat var dx, dy, new_x, new_y, x, y, closest_distance = min_dist || Infinity @@ -443,7 +490,7 @@ var Polyline = Fiber.extend(function(base){ exports.build = function(){ this.mx_points && this.mx_points.forEach(function(mx){ scene.remove(mx) }) this.mx = new MX.Polyline(this) - shapes.push(this) + shapes.add(this) } exports.rebuild = function(){ this.mx.rebuild() @@ -524,6 +571,8 @@ map = new Map ({ width: window.innerWidth/2, height: window.innerHeight, zoom: -2, + zoom_min: -6.2, + zoom_max: 1, }) map.ui.add_tool("arrow", new ArrowTool) map.ui.add_tool("polyline", new PolylineTool) @@ -538,7 +587,7 @@ $(window).resize(function(){ var wall_height = 180 var placing = false var line -var shapes = [] +var shapes = new ShapeList var ctx = map.draw.ctx var last_point = new vec2 (0,0) |
