summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/shapes/ortho.js
blob: c1acae5d0aec8d63b2c412ebac7927b662bb6c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// An OrthoPolyline is a Polyline where all angles are 90 degrees.

var OrthoPolyline = Polyline.extend(function(base){
	var exports = {}
	exports.type = function(){
	  return "ortho"
	}
	exports.instantiate = function(){
	  return new OrthoPolyline
	}
	exports.canCloseWith = function(p){
    return (this.points.length > 2 && this.points[0].distanceTo( p ) < 10/map.zoom)
	}
  exports.draw = function(ctx, fillStyle, strokeStyle){
    var points = this.points
    if (! points.length) return
    if (points.length == 1) {
      ctx.fillStyle = "#f80"
      map.draw.dot_at(this.points[0].a, points[0].b, 5)
    }
    if (points.length > 1) {
      ctx.fillStyle = fillStyle
      ctx.strokeStyle = strokeStyle
      ctx.lineWidth = 2 / map.zoom
      ctx.beginPath()
      ctx.moveTo(points[0].a, points[0].b)
      points.forEach(function(point, i){
        i && ctx.lineTo(point.a, point.b)
      })
      strokeStyle && ctx.stroke()
      if (! map.ui.placing || this.closed) {
        fillStyle && ctx.fill()
      }
    }
  }
	exports.translateSegment = function(src, dest, dx, dy) {
	  if (src[0].a == src[1].a) {
	    dest[0].a = src[0].a + dx
	    dest[1].a = src[1].a + dx
	    if (src.length == 3) {
        dest[2].a = src[2].a + dx
	    }
	  }
	  else {
	    dest[0].b = src[0].b + dy
	    dest[1].b = src[1].b + dy
	    if (src.length == 3) {
        dest[2].b = src[2].b + dy
	    }
	  }
	}
  exports.close = function(){
    this.points[this.points.length] = this.points[0]
    this.closed = true
  }
  return exports
})