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
|
MX.Minimap = function () {
var canvas = document.createElement("canvas")
var ctx = canvas.getContext("2d")
var w = canvas.width = 200
var h = canvas.height = 200
var gridSpace;
var zoom = 2.7
var gridStroke = '#ddd'
var boxFill = '#fff'
var boxStroke = '#000'
var playerColor = '#888'
var xmin, xmax, ymin, ymax, xpos, ypos, scale, side;
this.update = function(){
this.draw()
}
this.bounds = function(){
gridSpace = Math.pow(10, ~~(zoom-0.5)+0.5)
side = Math.pow(10, zoom+1)
scale = w / side
xpos = -cam.x
ypos = cam.z
xmin = side/-2 - xpos
xmax = side/2 - xpos
ymin = side/-2 - ypos
ymax = side/2 - ypos
}
this.draw = function(){
ctx.clearRect(0,0,w,h)
ctx.fillStyle = "#fff"
ctx.fillRect(0,0,w,h)
this.bounds()
this.grid()
this.boxes()
this.player()
}
this.grid = function(){
ctx.strokeStyle = gridStroke
ctx.lineWidth = 1
ctx.fillStyle = "transparent"
var xmod = xmin-(xmin % gridSpace)
var ymod = ymin-(ymin % gridSpace)
for (var x = xmin; x < xmax+gridSpace; x += gridSpace) {
var xline = (x-xmod) * scale
line(xline, 0, xline, h)
}
for (var y = ymin; y < ymax+gridSpace; y += gridSpace) {
var yline = (y-ymod) * scale
line(0, yline, w, yline)
}
function line(x0,y0,x1,y1) {
ctx.beginPath()
ctx.moveTo(x0, y0)
ctx.lineTo(x1, y1)
ctx.stroke()
}
}
this.player = function(){
ctx.save()
ctx.translate(~~(w/2),~~(h/2));
ctx.rotate(-cam.rotationY)
var radius = 5
ctx.fillStyle = playerColor;
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2*Math.PI, false);
ctx.fill();
ctx.beginPath();
ctx.moveTo(0,0)
ctx.lineTo(-radius,0)
ctx.lineTo(0,radius*3)
ctx.lineTo(radius,0)
ctx.moveTo(0,0)
ctx.fill()
ctx.fillStyle = "transparent"
ctx.restore()
}
this.boxes = function(){
ctx.save()
ctx.translate(~~(w/2),~~(h/2));
ctx.lineWidth = 0.5
var tx = ((-xpos) * scale),
ty = ((-ypos) * scale);
ctx.translate(tx, ty)
scene.inner.children.forEach(function(obj){
if (obj.type == "Box" || obj.type == "BoxDimensions" || obj.type == "ScaleBox") {
ctx.save()
ctx.fillStyle = obj.color
ctx.strokeStyle = "#222"
var obj_scale = (obj.scale || 1) * scale
var tx = ~~((obj.x) * scale),
ty = ~~((obj.z) * scale);
ctx.translate(-tx, ty)
ctx.rotate(-obj.rotationY)
var ww = ~~(obj.opt.width/2 * obj_scale)
var hh = ~~(obj.opt.depth/2 * obj_scale)
ctx.beginPath();
ctx.moveTo(ww, hh)
ctx.lineTo(ww, -hh)
ctx.lineTo(-ww, -hh)
ctx.lineTo(-ww, hh)
ctx.closePath()
ctx.fill()
ctx.stroke()
ctx.restore()
}
if (obj.type == "Image" || obj.type == "Video" || obj.type == "Cutout") {
ctx.save()
ctx.strokeStyle = "#444"
var obj_scale = (obj.scale || 1) * scale
var tx = ~~((obj.x) * scale),
ty = ~~((obj.z) * scale);
ctx.translate(-tx, ty)
ctx.rotate(-obj.rotationY)
var ww = ~~(obj.width/2 * obj_scale)
ctx.beginPath();
ctx.moveTo(ww, 0)
ctx.lineTo(-ww, 0)
ctx.closePath()
ctx.stroke()
ctx.restore()
}
})
ctx.restore()
}
var dragging = false, mx = 0, my = 0, mdx = 0, mdy = 0, cx, cy;
canvas.addEventListener("mousedown", function(e){
e.stopPropagation()
var rect = canvas.getBoundingClientRect()
dragging = true;
mx = e.pageX - rect.left
my = e.pageY - rect.top
mdx = (mx - w/2) / scale
mdy = (my - h/2) / scale
cx = cam.x // -= mdx
cy = cam.z // += mdy
minimap.update()
})
document.addEventListener("mousemove", function(e){
if (dragging) {
e.stopPropagation()
var rect = canvas.getBoundingClientRect()
var mnx = e.pageX - rect.left
var mny = e.pageY - rect.top
mdx = (mnx - mx) / scale
mdy = (mny - my) / scale
cam.x = cx + mdx
cam.z = cy - mdy
minimap.update()
}
})
document.addEventListener("mouseup", function(e){
dragging = false;
})
canvas.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
canvas.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
function onDocumentMouseWheel (e) {
// WebKit
if ( event.wheelDeltaY ) {
zoom -= event.wheelDeltaY * 0.0003;
}
// Opera / Explorer 9
else if ( event.wheelDelta ) {
zoom -= event.wheelDelta * 0.0003;
}
// Firefox
else if ( event.detail ) {
zoom += event.detail * 0.01;
}
minimap.update()
}
this.draw()
$("#minimap .el").append(canvas)
return this;
}
|