-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx.js
368 lines (299 loc) · 9.63 KB
/
gfx.js
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* UI stuff: draw on the canvas, UI elements like text and buttons. Do not use the global 'world' struct here */
const UI_BUTTON_WIDTH = 160;
const UI_BUTTON_HEIGHT = 80;
var ui = {
"buttons": [],
"click_listeners": [],
"image_cache": {}, // url => (Image, loaded)
"render_list": [] // list of things to render that will be z sorted first.
}
// Things to render to the screen (canvas)
const GFX = {
"image": "image"
}
function Graphic( type, coordinates, z_index, attributes ) {
return Object.assign(
{
"type": type,
"p": coordinates,
"z_index": z_index
},
attributes
);
}
function ui_init() {
ui = {
"buttons": [],
"click_listeners": [],
"image_cache": {}, // url => (Image, loaded)
"render_list": [] // list of things to render that will be z sorted first.
}
ui_add_click_handler( process_button_click );
}
function ui_draw() {
// render things on the canvas
render();
// render UI elements like buttons
// ui_buttons_draw();
}
function render() {
// z-sort the items in the render list
// lower numbers are in front, z coordinates increase into the screen.
ui.render_list = ui.render_list.sort( function(a, b) {
if( a.z_index == b.z_index ) {
return 0;
}
return a.z_index < b.z_index ? -1 : 1;
} )
.reverse(); // draw from back to front
once( function renderOrder(){console.log(ui.render_list)});
ui.render_list.forEach( g => {
switch( g.type ) {
case "arc": {
draw_arc( g.p, g.radius, g.start_radians, g.stop_radians, g.width, g.color );
break;
}
case "arrow": {
let line_width = PPI * 0.1;
let arrowead_width = 6 * line_width;
let shortened = line_delta(g.p, g.head, -arrowead_width);
let text_center = lerp( g.p, shortened );
// Move the text a lineswidth away so it does not touch the vertical lines
// also move it up half the size because text is rendered baseline middle
fill_text( V(text_center.x + line_width, text_center.y - line_width - g.font_size/2), g.text, g.color, g.font_size + "px Menlo");
// line for the shaft
stroke_line( g.p, shortened, line_width, g.color, [] );
// triangle for the head: the head point, and two points perpdendicular to the direction of the arrow
let head_vector = add( shortened, minus(g.head) );
let perp = vector_normalize( V(head_vector.y, -head_vector.x) );
let corner_a = add( shortened, vector_mul( arrowead_width/2, perp ) );
let corner_b = add( shortened, vector_mul( -arrowead_width/2, perp ) )
draw_triangle( corner_a, corner_b, g.head, g.color);
break;
}
case "circle": {
draw_circle( g.p, g.radius, g.width, g.color );
break;
}
case "disk": {
draw_disk( g.p, g.radius, g.color );
break;
}
case "image": {
let i = ui.image_cache[g.url];
if( i.loaded ) {
draw_image( g.p, i.image, g.centered, g.scale );
}
break;
}
case "line": {
stroke_line( g.p, g.end, g.width, g.color, g.style );
}
case "rect": {
draw_rect( g.p, g.width, g.height, g.color );
break;
}
case "plane": {
draw_plane( g.p, g.width, g.height, g.color );
break;
}
case "text": {
fill_text( g.p, g.text, g.color, g.font );
break;
}
default:
once( function dontKnowHowToRender(){
console.log("Don't know how to draw: " + g.type);
});
}
});
}
function ui_add_click_handler( handler ) {
ui.click_listeners.push( handler );
}
function ui_register_handlers() {
canvas.addEventListener("click", canvas_onclick, false);
}
function canvas_onclick(e) {
var element = canvas;
var offsetX = 0, offsetY = 0
if (element.offsetParent) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while ((element = element.offsetParent));
}
x = e.pageX - offsetX;
y = e.pageY - offsetY;
const p = V(x,y);
console.log(ui.click_listeners);
ui.click_listeners.forEach( l => l(p) );
}
function process_button_click( p ) {
console.log(p);
const button = ui.buttons.find( b => p.x >= b.loc.x && p.x < (b.loc.x+UI_BUTTON_WIDTH) && p.y >= b.loc.y && p.y < (b.loc.y + UI_BUTTON_HEIGHT));
if( button != undefined ) {
console.log(button);
button.callback();
}
}
function create_button( top_left, text, callback ) {
const b = {
"loc": top_left,
"text": text,
"callback": callback
}
ui.buttons.push( b );
}
/**
* Create a fully expanded select with optgroups.
*
* @param select HTMLSelectElement
* @param groups {"foo": [a,b,c], "bar": [d,e] }
* @param optionSelectedHandler function that is called when an option is clicked
*/
function create_select_optgroups( select, groups, optionSelectedHandler ) {
while( select.firstChild ) {
select.removeChild( select.firstChild );
}
let options = 0;
for( const group of Object.keys(groups) ) {
options++;
const optgroup = document.createElement('optgroup');
optgroup.label = group;
for( const option of groups[group] ) {
const element = document.createElement("option");
element.innerText = option;
element.value = option;
optgroup.appendChild( element );
options++;
}
select.appendChild( optgroup );
}
select.setAttribute("size", options );
if( optionSelectedHandler != null ) {
select.onchange = optionSelectedHandler;
}
}
function onOptionSelect( fn ) {
return function( event ) {
console.log( event, event.target.selectedIndex );
console.log( "select: " + event.target.selectedIndex );
fn( event.target.selectedIndex, event.target[event.target.selectedIndex].value );
}
}
/** Load an image to the image list to draw it at a specified location
* Note: no z-index
* on_load_completed_callback: will be called after the image has been loaded with the HTMLImage as parameter
*/
function gfx_image_load( url, on_load_completed_callback ) {
if( ui.image_cache[url] == undefined ) {
// Note: using 'let' creates local scope so addEventlistener closes over this i, and not some global one.
let i = {
"image": new Image(), // Create new img element
"loaded": false
};
i.image.addEventListener("load", (e) => {
i.loaded = true;
if( DEBUG) {
console.log("Loaded " + url + " (" + i.image.width + "x" + i.image.height + ")");
}
if( on_load_completed_callback != undefined ) {
on_load_completed_callback( i.image );
}
});
i.image.src = url; // set source path to load image
ui.image_cache[url] = i; // cache it
}
}
/* Add an image to the draw list
* Note: loads it on demand
*/
function ui_image( p, z_index, url, centered = false, scale = V(1,1) ) {
gfx_image_load( url );
let attributes = {
"url": url,
"centered": centered,
"scale": scale
};
ui.render_list.push( Graphic(GFX.image, p, z_index, attributes) );
}
function gfx_circle( center, z_index, radius, color, width ) {
let attributes = {
"radius": radius,
"color": color,
"width": width
};
ui.render_list.push( Graphic( "circle", center, z_index, attributes ) );
}
function gfx_arc( center, z_index, radius, start_radians, stop_radians, color, width ) {
let attributes = {
"radius": radius,
"color": color,
"width": width,
"start_radians": start_radians,
"stop_radians": stop_radians
};
ui.render_list.push( Graphic( "arc", center, z_index, attributes ) );
}
function gfx_disk( center, z_index, radius, color ) {
let attributes = {
"radius": radius,
"color": color
};
ui.render_list.push( Graphic( "disk", center, z_index, attributes ) );
}
function gfx_rect( location, z_index, width, height, color, centered = false ) {
let attributes = {
"width": width,
"height": height,
"color": color,
"centered": centered
};
ui.render_list.push( Graphic( "rect", location, z_index, attributes ) );
}
function gfx_plane( location, z_index, width, height, color, centered = false ) {
let attributes = {
"width": width,
"height": height,
"color": color,
"centered": centered
};
ui.render_list.push( Graphic( "plane", location, z_index, attributes ) );
}
function gfx_arrow( tail, z_index, head, width, color, text, font_size ) {
let attributes = {
"head": head,
"color": color,
"text": text,
"font_size": font_size
};
ui.render_list.push( Graphic( "arrow", tail, z_index, attributes ) );
}
function gfx_line( p, z_index, end, width, color, style = [0] ) {
let attributes = {
"end": end,
"color": color,
"width": width,
"style": style
};
ui.render_list.push( Graphic( "line", p, z_index, attributes ) );
}
function gfx_text( p, z_index, text, color = "black", font = "30px Menlo" ) {
let attributes = {
"text": text,
"color": color,
"font": font
};
ui.render_list.push( Graphic( "text", p, z_index, attributes ) );
}
function ui_buttons_draw() {
render_shadow( true );
ctx.fillStyle = 'black';
ui.buttons.forEach( b => {
fill_rect( b.loc, UI_BUTTON_WIDTH, UI_BUTTON_HEIGHT, 'silver' );
ctx.fillStyle = 'black';
ctx.font = "bold 48px serif";
ctx.fillText( b.text, b.loc.x + 30, b.loc.y + 60 );
});
}