-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.html
312 lines (282 loc) · 9.47 KB
/
builder.html
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<html>
<head>
<style>
canvas {
background-color: #000;
float: right;
cursor: crosshair;
}
input[type=number] {
width: 200px;
}
label {
display: inline-block;
text-align: right;
width: 100px;
}
a {
display: block;
width: 400px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
background-color: #ccc;
padding: 4px;
border-style
}
</style>
</head>
<body>
<canvas id='cnvs'></canvas>
<h1>Balls world builder</h1>
<h3>Instuctions</h3>
<ol>
<li>adjust global options below</li>
<li>select placing mode below</li>
<li>if selected ball spawn area, then:
<ol>
<li>adjust spawn settings [<strong>before</strong> placing]
<li>click and hold for top left corner</li>
<li>drag to bottom right corner</li>
<li>release to place</li>
</ol>
</li>
<li>if selected bollard, then:
<ol>
<li>click for first corner</li>
<li>click for second corner</li>
<li>click for fourth edge</li>
</ol>
</li>
<li>repeats steps (1)-(4) till constructed</li>
<li>run the simulation by clicking the link at the bottom</li>
<em>you can copy or bookmark this link to revisit your simulation</em>
</ol>
<hr>
<h3>Options</h3>
<h4>Global</h4>
<label>ball tracing?</label>
<input id='tracing_check' type='checkbox' oninput='update();'></input>
<br>
<label>ball collisions?</label>
<input id='collisions_check' checked=1 type='checkbox' oninput='update();'></input>
<br>
<label>gravity</label>
<input type='number' id='gravity_inpt' value=0 oninput='update()'></input>pix/sec^2
<br>
<label>coefficient of restitution</label>
<input type='number' id='cor_inpt' value=1 oninput='update();'></input>arb. unit
<br>
<h4>Placing Mode</h4>
<span>
<input id='spawn_area_but' type='button' value='ball spawn area'></input>
<input id='bollards_but' type='button' value='bollards'></input>
</span>
<hr>
<div id='spawn_param_div'>
<label>num balls</label>
<input type='number' id='no_balls_inpt' value=1>balls
<br>
<label >max radius</label>
<input type='number' id='max_radius_inpt' value=10></input>pix
<br>
<label>min radius</label>
<input type='number' id='min_radius_inpt' value=10></input>pix
<br>
<label>max velocity</label>
<input type='number' id='max_vel_inpt' value=0></input>pix/sec
<br>
<label>min velocity</label>
<input type='number' id='min_vel_inpt' value=0></input>pix/sec
<br>
<label>velocity angle</label>
<input type='number' id='vel_angle_inpt' value=-1></input>°
<em>[if this is -1 then random angle]</em>
<hr>
</div>
<h3>Run it!</h3>
<a id='run_link' target='_blank'></a>
<script>
'use strict';
let cnvs = document.getElementById('cnvs');
let ctx = cnvs.getContext('2d');
let run_link = document.getElementById('run_link');
//lots of DOM elements for options to get... (should've made func :/)
let tracing_check = document.getElementById('tracing_check');
let collisions_check = document.getElementById('collisions_check');
let gravity_inpt = document.getElementById('gravity_inpt');
let cor_inpt = document.getElementById('cor_inpt');
let spawn_param_div = document.getElementById('spawn_param_div');
let spawn_area_but = document.getElementById('spawn_area_but');
let bollards_but = document.getElementById('bollards_but');
let no_balls_inpt = document.getElementById('no_balls_inpt');
let max_radius_inpt = document.getElementById('max_radius_inpt');
let min_radius_inpt = document.getElementById('min_radius_inpt');
let max_vel_inpt = document.getElementById('max_vel_inpt');
let min_vel_inpt = document.getElementById('min_vel_inpt');
let vel_angle_inpt = document.getElementById('vel_angle_inpt');
cnvs.height = cnvs.width = innerHeight*4/5;
/*
* TODO:
* add ability to send a description, title etc. to simulation page
*/
/*
* bollards is array of objects, each have a 'type' property:
* 'type' | other properties
* =========================
* rect | points: [[{x,y},{x,y},...],...]
* circle | x: x-coord, y: y-coord, r: radius //// <--- not implemented yet (not sure will)
*/
let bollards = [];
//array of objects with x,y,w,h,no_balls,min_radius,max_radius,min_vel,max_vel,vel_angle
let spawn_areas = [];
let placing = 'spawn'; //... or 'bollard'
ctx.strokeStyle = '#f00';
ctx.fillStyle = '#0f0';
//updates all spawn_areas and bollards in canvas and updates link
function update(){
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
for (let bollard of bollards){
switch (bollard.type){
case 'rect':
fill_polygon(bollard.points)
break;
case 'circle':
//not implemented yet
break;
}
}
for (let spawn of spawn_areas){
ctx.strokeRect(spawn.x, spawn.y, spawn.w, spawn.h);
}
let data = JSON.stringify({bollards: bollards,
spawn_areas: spawn_areas,
tracing: tracing_check.checked,
collisions: collisions_check.checked,
gravity: parseInt(gravity_inpt.value),
cor: parseFloat(cor_inpt.value),
size: cnvs.width});
run_link.href = run_link.innerText = 'https://joeiddon.github.io/balls?data='+encodeURIComponent(data);
}
// call update once to set initial run_link (<a> tag)
update();
// mouse event listeners for placing spawn
cnvs.addEventListener('click', click_spawn);
let spawn_top_left;
function click_spawn(e){
if (spawn_top_left){
spawn_areas.push({
x: spawn_top_left.x,
y: spawn_top_left.y,
w: e.offsetX - spawn_top_left.x,
h: e.offsetY - spawn_top_left.y,
no_balls: parseInt(no_balls_inpt.value),
min_radius: parseInt(min_radius_inpt.value),
max_radius: parseInt(max_radius_inpt.value),
min_vel: parseInt(min_vel_inpt.value),
max_vel: parseInt(max_vel_inpt.value),
vel_angle: parseInt(vel_angle_inpt.value)
});
update();
spawn_top_left = 0;
cnvs.removeEventListener('mousemove', mouse_move_spawn);
} else {
spawn_top_left = {x: e.offsetX, y: e.offsetY};
cnvs.addEventListener('mousemove', mouse_move_spawn);
}
}
function mouse_move_spawn(e){
update();
ctx.strokeRect(spawn_top_left.x,
spawn_top_left.y,
e.offsetX - spawn_top_left.x,
e.offsetY - spawn_top_left.y);
}
// mouse event listeners for placing bollard
let bollard_first_corner;
let bollard_second_corner;
function click_bollard(e){
let this_corner = {x: e.offsetX, y: e.offsetY};
if (bollard_first_corner){
if (bollard_second_corner){
let corners = other_corners_of_rect(bollard_first_corner,
bollard_second_corner,
this_corner);
bollards.push({
type: 'rect',
points: [bollard_first_corner,
bollard_second_corner,
corners[0],
corners[1]]
});
update();
bollard_first_corner = bollard_second_corner = 0;
cnvs.removeEventListener('mousemove', mouse_move_bollard);
} else {
bollard_second_corner = this_corner;
}
} else {
bollard_first_corner = this_corner;
cnvs.addEventListener('mousemove', mouse_move_bollard);
}
}
function other_corners_of_rect(c1, c2, p){
//calculates corners 3 and 4 from a point on the fourth edge
//see paper for calculations for third and fourth corners
let m = (c2.y - c1.y) / (c2.x - c1.x);
let ax = (c2.y - p.y + p.x * m + c2.x / m) / (m + 1/m);
let ay = p.y + m * (ax - p.x);
let bx = (c1.y - p.y + p.x * m + c1.x / m) / (m + 1/m);
let by = p.y + m * (bx - p.x);
return [{x: ax, y: ay}, {x: bx, y: by}];
}
function mouse_move_bollard(e){
update();
let this_point = {x: e.offsetX, y: e.offsetY};
if (bollard_second_corner){
let corners = other_corners_of_rect(bollard_first_corner,
bollard_second_corner,
this_point);
fill_polygon([bollard_first_corner, bollard_second_corner, corners[0], corners[1]]);
} else{
fill_polygon([bollard_first_corner, this_point]);
}
}
function fill_polygon(points){
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++){
ctx.lineTo(points[i].x, points[i].y);
}
ctx.stroke();
ctx.fill();
}
set_button(spawn_area_but, 1);
spawn_area_but.onclick = function(){
placing = 'spawn';
cnvs.removeEventListener('click', click_bollard);
cnvs.removeEventListener('mousemove', mouse_move_bollard);
bollard_first_corner = bollard_second_corner = 0;
cnvs.addEventListener('click', click_spawn);
set_button(spawn_area_but, 1);
set_button(bollards_but, 0);
spawn_param_div.style.display = 'block';
}
bollards_but.onclick = function(){
placing = 'bollard';
cnvs.removeEventListener('click', click_spawn);
cnvs.removeEventListener('mousemove', mouse_move_spawn);
spawn_top_left = 0;
cnvs.addEventListener('click', click_bollard);
set_button(bollards_but, 1);
set_button(spawn_area_but, 0);
spawn_param_div.style.display = 'none';
}
function set_button(button, state){
button.style.borderStyle = state ? 'inset':'';
}
let fit_x_coord = x => x/cnvs.width*100;
let fit_y_coord = y => y/cnvs.height*100;
</script>
</body>
</html>