summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/map/draw.js
blob: 1dcd1ae9908b9a1892cb267418609626fd8d61de (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
map.draw = new function(){

	var base = this

	base.clear_canvas = function (){
		ctx.fillStyle = "rgba(255,255,255,0.99)"
		ctx.clearRect(0,0,w,h)
		ctx.fillRect(0,0,w,h)
	}

	base.ruler = function (){
		ctx.strokeStyle = "rgba(80,80,80,0.5)"
		ctx.lineWidth = 1
		var len = 5
		for (var i = 0.5; i < w; i += 10) {
			line(i, 0, i, len)
			line(0, i, len, i)
		}
	}

	base.regions = function(regions){
		for (var i = 0; i < regions.length; i++) {
			if (regions[i].dupe) continue
			ctx.fillStyle = colors[i % colors.length]
			fill_region(regions[i])
			stroke_sides(regions[i])
		}
	}

	base.mouse = function(mouse){
		ctx.fillStyle = "rgba(255,0,0,0.4)";
		ctx.beginPath();
		ctx.arc(mouse.x.b, mouse.y.b, 5, 0, 2*Math.PI, false);
		ctx.fill();

		if (mouse.width() != 0 && mouse.height() != 0) {
			if (map.ui.dragging) {
				stroke_rect(mouse)
			}
			else {
				ctx.fillStyle = "rgba(255,255,0,0.5)"
				fill_region( mouse.clone().translate() )
			}
		}
	}

	//

	function line (x,y,a,b,translation){
		if (translation) {
			x += translation.a
			a += translation.a
			y += translation.b
			b += translation.b
		}
		ctx.beginPath()
		ctx.moveTo(x,y)
		ctx.lineTo(a,b)
		ctx.stroke()
	}

	function fill_region(r){
		ctx.fillRect(r.x.a + r.translation.a,
								 r.y.a + r.translation.b,
								 r.x.length(),
								 r.y.length())
	}

	function stroke_sides (r){
		if (r.sides & FRONT) line(r.x.a, r.y.a, r.x.b, r.y.a)
		if (r.sides &  BACK) line(r.x.a, r.y.b, r.x.b, r.y.b)
		if (r.sides &  LEFT) line(r.x.a, r.y.a, r.x.a, r.y.b)
		if (r.sides & RIGHT) line(r.x.b, r.y.a, r.x.b, r.y.b)
	}

	function stroke_rect (r){
		line(r.x.a, r.y.a, r.x.b, r.y.b, r.translation)
	}
}