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
|
<style type="text/css">
html,body{width:100%;height:100%;margin:0;padding:0;}
body {
font-family: Menlo, monospace;
overflow: hidden;
}
#perspective {
position: absolute;
left:0%;
top:0px
}
#orthographic {
position: absolute;
left:50%;
top:0px
}
#hud {
white-space: pre;
}
</style>
<div id="perspective"></div>
<div id="orthographic"></div>
<div id="hud"></div>
<script src="/assets/javascripts/util.js"></script>
<script src="/assets/javascripts/vendor/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/assets/javascripts/vendor/bower_components/lodash/lodash.min.js"></script>
<script src="/assets/javascripts/vendor/bower_components/hidpi-canvas/dist/hidpi-canvas.js"></script>
<script src="/assets/javascripts/vendor/polyfill.js"></script>
<script src="/assets/javascripts/vendor/tube.js"></script>
<script src="/assets/javascripts/mx/mx.js"></script>
<script src="/assets/javascripts/mx/extensions/mx.scene.js"></script>
<script src="/assets/javascripts/mx/extensions/mx.orbitCamera.js"></script>
<script src="/assets/javascripts/mx/primitives/mx.grid.js"></script>
<script src="/assets/javascripts/mx/primitives/mx.image.js"></script>
<script src="/assets/javascripts/rectangles/util/constants.js"></script>
<script src="/assets/javascripts/rectangles/util/mouse.js"></script>
<script src="/assets/javascripts/rectangles/util/wheel.js"></script>
<script src="/assets/javascripts/rectangles/models/vec2.js"></script>
<script src="/assets/javascripts/rectangles/models/rect.js"></script>
<script src="/assets/javascripts/rectangles/models/rect.js"></script>
<script src="/assets/javascripts/rectangles/engine/map/_map.js"></script>
<script src="/assets/javascripts/rectangles/engine/map/ui_ortho.js"></script>
<script src="/assets/javascripts/rectangles/engine/map/draw.js"></script>
<script>
var scene, map, controls
map = new Map ({
type: "ortho",
el: document.querySelector("#orthographic"),
width: window.innerWidth/2,
height: window.innerHeight,
zoom: -2,
})
var placing = false
var points, mx_points = []
var shapes = []
var ctx = map.draw.ctx
var last_point
function polyline (points, finished) {
if (! points) return
if (points.length == 1) {
ctx.fillStyle = "#f80"
map.draw.dot_at(points[0].a, points[0].b, 5)
}
if (points.length > 1) {
ctx.fillStyle = "#ff0"
ctx.strokeStyle = "#f80"
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)
})
ctx.stroke()
if (! placing || finished) {
ctx.fill()
}
}
}
function add_mx_polyline (points) {
mx_points.forEach(function(mx){ scene.remove(mx) })
var faces = []
for (var i = 1; i < points.length; i++) {
var head = points[i-1]
var tail = points[i]
face = add_mx_polyline_face(head, tail)
faces.push(face)
}
shapes.push({
mx: faces,
points: points,
})
points = []
}
function add_mx_polyline_face(head, tail){
var mx = new MX.Object3D()
var mid_x = (head.a + tail.a)
var mid_z = (head.b + tail.b)
var len = head.distanceTo( tail )
var angle = atan2( head.b - tail.b, head.a - tail.a )
mx.move({
x: mid_x / 2,
y: 25 + 1,
z: mid_z / 2,
width: ceil(len),
height: 50,
rotationY: angle
})
var hue = abs(round( angle / PI * 90 + 300))
mx.el.style.backgroundColor = 'hsl(' + [hue, "100%", "50%"] + ')'
scene.add(mx)
return mx
}
function add_mx_point (p, i) {
var mx = new MX.Object3D()
mx.updateChildren = false
mx.move({
x: p.a,
y: 11,
z: p.b,
width: 20,
height: 20,
rotationX: PI/2,
})
mx.el.style.backgroundColor = 'rgb(' + [abs(floor(p.a*30)), 0, abs(floor(p.b*30))] + ')'
mx.el.style.backfaceVisibility = "visible"
mx.el.style.borderRadius = "50%"
scene.add(mx)
return mx
}
document.addEventListener('DOMContentLoaded', build)
function build () {
scene = new MX.Scene().addTo("#perspective")
scene.camera.radius = 20
scene.width = window.innerWidth/2
scene.height = window.innerHeight
scene.perspective = window.innerHeight
// grid = new MX.Grid()
// scene.add(grid)
floorplan = new MX.Image({
src: "https://s3.amazonaws.com/luckyplop/fbf4295da80f1f66c5e4a248f2ea3e1ce7a22c3d.jpg",
keepImage: true,
rotationX: -PI/2,
rotationY: PI,
})
scene.add(floorplan)
scene.update()
controls = new MX.OrbitCamera({
el: scene.el,
radius: 3000,
radiusRange: [ 10, 10000 ],
rotationX: PI/4,
rotationY: PI/2,
})
controls.init()
animate(0)
}
function animate(time){
requestAnimationFrame(animate)
map.update(time)
controls.update()
scene.update()
map.draw.ctx.save()
map.draw.translate()
// grid.draw(map.draw.ctx, true)
floorplan.draw(map.draw.ctx, true)
map.draw.coords()
polyline(points)
if (placing && last_point) {
ctx.strokeStyle = "#f80"
ctx.lineWidth = 2 / map.zoom
ctx.beginPath()
ctx.moveTo(points[points.length-1].a, points[points.length-1].b)
ctx.lineTo(last_point.a, last_point.b)
ctx.stroke()
}
shapes.forEach(function(shape){
polyline(shape.points, true)
})
ctx.strokeStyle = "#f00";
map.draw.x_at(0,0)
map.draw.mouse(map.ui.mouse.cursor)
map.draw.camera(scene.camera)
map.draw.ctx.restore()
}
</script>
|