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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
Map.Draw = function(map, opt){
opt = defaults(opt, { minimap: false })
var draw = this
var ctx = map.canvas.getContext("2d")
draw.animate = function(){
ctx.save()
draw.clear()
draw.fill("rgba(255,255,255,0.98)")
if (opt.minimap) {
ctx.translate( map.dimensions.a * 1/2, map.dimensions.b * 1/2)
ctx.scale( map.zoom, map.zoom )
ctx.translate( opt.center.x, - opt.center.z )
ctx.scale( -1, 1 )
draw.coords()
draw.regions(Rooms.regions, [ "#fff" ], "#000")
draw.camera(scene.camera)
}
else {
ctx.translate( map.dimensions.a * 1/2, map.dimensions.b * 1/2)
ctx.scale( map.zoom, map.zoom )
ctx.translate( map.center.a, map.center.b )
ctx.scale( -1, 1 )
draw.regions(Rooms.regions, [ "#f8f8f8" ], "#000")
draw.mouse(map.ui.mouse.cursor)
draw.coords()
scene && draw.camera(scene.camera)
}
ctx.restore()
}
// changes the ctx temporarily
draw.render = function(){
var thumbnail_width = 1000
var thumbnail_height = 750
var extent = Rooms.extent()
var center = extent.center()
var extent_width = extent.width()
var extent_height = extent.height()
var zoom
if (extent_width > extent_height) {
zoom = thumbnail_width / extent.width() * 0.9
}
else {
zoom = thumbnail_height / extent.height() * 0.9
}
var canvas = document.createElement("canvas")
ctx = canvas.getContext('2d')
canvas.width = thumbnail_width
canvas.height = thumbnail_height
draw.clear()
ctx.translate( thumbnail_width * 1/2, thumbnail_height * 1/2)
ctx.scale( zoom, zoom )
ctx.translate( center.a, -center.b )
ctx.scale( -1, 1 )
draw.regions(Rooms.regions, ["#fff"], null)
ctx.restore()
// invert opacity
var pixelData = ctx.getImageData(0, 0, canvas.width, canvas.height)
var pixels = pixelData.data
for (var i = 0, k, _len = pixels.length; i < _len; i++) {
k = i*4
if (pixels[k+3] == 0) {
pixels[k] = pixels[k+1] = pixels[k+2] = pixels[k+3] = 255
}
else {
pixels[k] = pixels[k+1] = pixels[k+2] = 255
pixels[k+3] = 0
}
}
ctx.putImageData(pixelData, 0, 0)
// reset the ctx
ctx = map.canvas.getContext("2d")
return canvas
}
draw.clear = function(){
ctx.clearRect(0, 0, map.dimensions.a, map.dimensions.b)
}
draw.fill = function(fillStyle){
ctx.fillStyle = fillStyle
ctx.fillRect(0, 0, map.dimensions.a, map.dimensions.b)
}
draw.regions = function(regions, colors, stroke){
if (stroke) {
ctx.strokeStyle = "#000"
ctx.lineWidth = (1 / map.zoom)
}
for (var i = 0; i < regions.length; i++) {
if (regions[i].dupe) continue
ctx.fillStyle = colors[i % colors.length]
fill_region(regions[i])
if (stroke) {
stroke_sides(regions[i])
}
}
}
draw.mouse = function(mouse){
var radius = 3 / map.zoom
ctx.fillStyle = "rgba(255,0,0,0.4)";
ctx.beginPath();
ctx.arc(mouse.x.b, mouse.y.b, radius, 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() )
}
}
}
draw.camera = function(cam){
ctx.lineWidth = 0.5
ctx.save()
ctx.translate(cam.x, cam.z)
ctx.rotate(cam.rotationY)
var radius = 3 / map.zoom
ctx.fillStyle = '#888';
ctx.beginPath();
ctx.moveTo(0,0)
ctx.lineTo(-radius,-radius/2)
ctx.lineTo(0,radius*3)
ctx.lineTo(radius,-radius/2)
ctx.moveTo(0,0)
ctx.fill()
ctx.restore()
}
draw.coords = function(){
/*
ctx.fillStyle = "#888";
dot_at(0,0)
ctx.fillStyle = "#bbb";
dot_at(100,0)
dot_at(0,100)
ctx.fillStyle = "#ddd";
dot_at(200,0)
dot_at(0,200)
ctx.fillStyle = "#eee";
dot_at(300,0)
dot_at(0,300)
*/
ctx.strokeStyle = "rgba(0,0,0,0.1)"
ctx.lineWidth = 1/map.zoom
var sides = map.sides()
var quant = sides.clone().quantize(200)
for (var x = quant.x.a - 200; x <= quant.x.b; x += 200) {
line(x, sides.y.a, x, sides.y.b)
}
for (var y = quant.y.a - 200; y <= quant.y.b; y += 200) {
line(sides.x.a, y, sides.x.b, y)
}
}
//
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)
}
function dot_at (x,z){
ctx.save()
ctx.translate(x,z)
var radius = 2 / map.zoom
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2*Math.PI, false);
ctx.fill();
ctx.restore()
}
}
|