summaryrefslogtreecommitdiff
path: root/public/assets/test/ortho3.html
blob: 37bf620c507383d4e0aca25f050310b944030ea3 (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
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
235
236
237
238
239
240
<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/util/coords.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>
Map.UI = Map.UI || {}
Map.UI.Ortho = function(map){

	var base = this

	base.creating = base.dragging = base.resizing = false

	base.mouse = new mouse({
		el: map.el,
		down: function(e, cursor){
			cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)

      if (e.ctrlKey || e.which === 3) {
        if (placing) {
          // close polyline or cancel
          return
        }
        e.preventDefault()
        e.stopPropagation()
        cursor.quantize(1/map.zoom)
        map.center.a = cursor.x.a
        map.center.b = -cursor.y.a
        cursor.x.b = cursor.x.a
        cursor.y.b = cursor.y.a
        base.mouse.down = false
        return
      }

			// compare to initial point
			var p = new vec2( cursor.x.a, cursor.y.a )
			if (placing) {
				if (points.length > 2 && points[0].distanceTo( p ) < 10/map.zoom) {
					points.push( points[0].clone() )
					placing = false
				}
				else {
					points.push( p )
				}
			}
			else {
				placing = true
				points = []
				points.push( p )
			}
		},
		move: function(e, cursor){
			cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)
			last_point = new vec2( cursor.x.a, cursor.y.a )
			if (placing && points.length > 1 && points[0].distanceTo( last_point ) < 10/map.zoom) {
				document.body.style.cursor = "pointer"
				last_point.assign(points[0])
				cursor.x.a = cursor.x.b = last_point.a
				cursor.y.a = cursor.y.b = last_point.b
			}
			else {
				document.body.style.cursor = "crosshair"
			}
		},
		drag: function(e, cursor){
			cursor.x.b = ((cursor.x.b/map.dimensions.a)+0.5) * map.dimensions.a / map.zoom + map.center.a
			cursor.y.b = ((cursor.y.b/map.dimensions.b)-0.5) * map.dimensions.b / map.zoom - map.center.b
		},
		up: function(e, cursor, new_cursor){
			new_cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			new_cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)
		}
	})

	base.wheel = new wheel({
		el: map.el,
		update: mousewheel,
	})

	function mousewheel (e, deltaY, deltaX){
		map.set_zoom(map.zoom_exponent - deltaY/20)
	}
}


var scene, map, controls

map = new Map ({
	type: "ortho",
	el: document.querySelector("#orthographic"),
	width: window.innerWidth/2,
	height: window.innerHeight,
	zoom: -2,
})
$(window).resize(function(){
  scene.width = window.innerWidth/2
  map.canvas.width = map.dimensions.a = window.innerWidth/2
})


var placing = false
var 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()
    }
  }
}

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

  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()

  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>