-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
468 lines (410 loc) · 16.1 KB
/
index.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="default.css" rel="stylesheet" type="text/css"/>
<script src="highlight.pack.js"></script>
<script>hljs.highlightAll();</script>
<script>
function resizeCanvas(){
let new_width = parseFloat(window.getComputedStyle(homeHeader, null).getPropertyValue("width"));
console.log(new_width)
pad.width = new_width;
}
</script>
</head>
<body>
<div class="main">
<H1>Welcome to the Graph Editor Library (DEMO)</H1>
<br>
<br>
<script src="Graphs.js"></script>
<div class="demoContainer">
<canvas width="600" height="500" id="canvas">not supported</canvas>
<div class="textSection">
<div class="switch-widget">
<table>
<tr>
<td>Example</td>
<td>
<label class="switch">
<input id="switch" class="switchbox" type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>Useage</td>
</tr>
</table>
</div>
<div class="fun">
<p><i>Right click drag</i> to create nodes</p>
<br>
<p><i>Left click drag</i> while hovering over a node to make a connection</p>
<br>
<p>To change edge values or node text simply <i>type</i> while <i>hovering</i> over them</p>
<br>
<p>To <i>delete</i> a node or edge simply mouse over and press the <i>delete key</i></p>
</div>
<div class="docs">
<p><i>creating</i> a graph</p>
<pre>
<code class="javascript">
let graphobj = new Graph("canvasid", fps=60, editable=true, buildable=true)
let startNode = graphobj.node( x_positon , y_position , radius , "text")
</code>
</pre>
making <i>connections</i>
<pre>
<code class="javascript">
startNode.connect(node, weight=0)
startNode.directional(node, weight=0)
startNode.biDirectional(node, weight=0)
</code>
</pre>
</div>
</div>
</div>
<br>
<br>
<div class="demoContainer">
<canvas width="600" height="500" id="searchGraph">not supported</canvas>
<div class="textSection">
<div class="switch-widget">
<table>
<tr>
<td>Example</td>
<td>
<label class="switch">
<input id="switch" class="switchbox" type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>Useage</td>
</tr>
</table>
</div>
<div class="fun">
<br>
<p>Graph Editor Library also supports built in <i>diijkstra</i>, <i>depth first</i>, <i>breadth first</i>, and <i>Astar</i> search</p>
<br>
<button id="dfs">Depth first search</button>
<button id="breadth first">Breadth First Search</button>
<button id="diijkstra">Diijkstra</button>
<button id="Astar">A*</button>
<br>
<br>
<p>this widget is set up to find the path from Start to End. Feel free to edit the graph</p>
<br>
<br>
<p>These allow you to draw <i>directional</i> or <i>biDirectional</i> connections</p>
<button id="setDirectional">set Directional</button>
<button id="setBiDirectional">set BiDirectional</button>
<br>
<button id="reset_button">reset</button>
</div>
<div class="docs">
<h3><i>Graphs.js</i> also has built in graph <i>search functions</i></h3>
<p>specify the start node id, the end node id, and weather you would like to draw a path</p>
<p>the function returns a list of <i>node objects</i> in the order they were traversed</p>
<pre>
<code class="javascript">
graphInstance.diijkstra(startNode.id, endNode.id, draw_path=true)
graphInstance.Astar(startNode.id, endNode.id, draw_path=true)
graphInstance.depthFirstSearch(startNode.id, endNode.id, draw_true=true)
graphInstance.breadthFirstSearch(startNode.id, endNode.id, draw_path=true)
</code>
</pre>
</div>
</div>
<script>
let searchGraph = new Graph("searchGraph");
let beginNode = searchGraph.node(100, 100, 25, "start")
let bNode = searchGraph.node(180, 80,25, "b");
let cNode = searchGraph.node(175, 300, 30, "c");
let dNode = searchGraph.node(220, 380, 25, "d");
let eNode = searchGraph.node(240, 260, 25, "e");
let fNode = searchGraph.node(400, 250, 25, "f");
let gNode = searchGraph.node(300, 100, 25, "g");
let rNode = searchGraph.node(300, 270, 25, "r");
let endNode = searchGraph.node(450, 450, 25, "end");
gNode.connect(rNode);
rNode.connect(endNode);
endNode.connect(gNode, 25);
endNode.connect(fNode, 17);
gNode.directional(fNode, 4);
eNode.connect(gNode);
cNode.connect(eNode, 6);
bNode.connect(eNode, 3);
bNode.connect(cNode);
dNode.connect(fNode, 16);
beginNode.directional(cNode, 6);
beginNode.connect(bNode, 2);
eNode.connect(dNode, 3);
cNode.connect(dNode, 2);
let depth_first_button = document.getElementById("dfs")
let breadth_first_button = document.getElementById("breadth first");
let reset_button = document.getElementById("reset_button");
let diijkstra_button = document.getElementById("diijkstra");
let Astar_button = document.getElementById("Astar");
let setDirectional_button = document.getElementById("setDirectional");
let setBiDirectional_button = document.getElementById("setBiDirectional");
depth_first_button.addEventListener('click', depth_wrapper);
breadth_first_button.addEventListener('click', breadth_wrapper);
diijkstra_button.addEventListener('click', diijkstra_wrapper);
Astar_button.addEventListener('click', Astar_wrapper);
reset_button.addEventListener('click', reset);
setDirectional.addEventListener('click', ()=>searchGraph.setDirectional());
setBiDirectional.addEventListener('click', ()=>searchGraph.setBiDirectional());
function getStartId(){
let start = searchGraph.getChildrenByText("start");
return start[0].id;
}
function getEndId(){
let end = searchGraph.getChildrenByText("end");
return end[0].id;
}
function reset(){
Object.values(searchGraph.edges).map(edge => edge.color = "black");
}
async function depth_wrapper(){
reset();
console.log("here")
let path = await searchGraph.depthFirstSearch(getStartId(), getEndId(), true)
console.log(path);
}
async function diijkstra_wrapper(){
reset();
let thing = await searchGraph.diijkstra(getStartId(), getEndId(), true);
}
async function Astar_wrapper(){
reset();
let thing = await searchGraph.Astar(getStartId(), getEndId(), true);
}
async function breadth_wrapper(){
reset();
let thing = await searchGraph.breadthFirstSearch(getStartId(), getEndId(), true);
}
function log_all_values(){
searchGraph.objs.map((node)=>console.log(node.value));
}
</script>
</div>
<br>
<br>
<div class="demoContainer">
<canvas width="600" height="300" id="canvas2">not supported</canvas>
<div class="textSection" id="baconSection">
<div class="switch-widget">
<table>
<tr>
<td>Example</td>
<td>
<label class="switch">
<input id="switch" class="switchbox" type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>Useage</td>
</tr>
</table>
</div>
<div class="fun">
<p>This Library also supports Images</p>
<p>Go ahead and drag a new node onto the canvas</p>
</div>
<div class="docs">
<p>simply specify the image you want on the node with the <i>setImage method</i></p>
<pre>
<code class="javascript">
node.setImage("https://someimage.jpg")
</code>
</pre>
</div>
</div>
<script>
let nodeking2 = new Graph("canvas2");
nodeking2.setNodeSetupCallback((node)=>{
let images = ["https://media1.popsugar-assets.com/files/thumbor/LfOJxhNfU8EftMFF_YPT5Fm5PwQ/263x100:2721x2558/fit-in/2048xorig/filters:format_auto-!!-:strip_icc-!!-/2020/04/06/953/n/1922398/d81efb315e8ba4da48b3b7.11171110_/i/Kevin-Bacon.jpg",
"https://pbs.twimg.com/profile_images/1240039886343688192/ddEgtzqr_400x400.jpg",
"https://m.media-amazon.com/images/M/MV5BOTQxMTEyMjI0NV5BMl5BanBnXkFtZTgwODE4ODAzMjE@._V1_.jpg",
"https://media.npr.org/assets/img/2011/11/22/kevinbacon_custom-008ac9d4aa108bcc152d5813a006362d286d6984-s800-c85.jpg"
]
node.setImage(images[Math.floor(Math.random()*images.length)]);
})
nodeking2.node(137, 137, 35);
</script>
</div>
<br>
<br>
<div class="demoContainer">
<canvas width="600" height="600" id="canvas3">not supported</canvas>
<div class="textSection" id="animatedTextSection">
<div class="switch-widget">
<table>
<tr>
<td>Example</td>
<td>
<label class="switch">
<input id="switch" class="switchbox" type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>Useage</td>
</tr>
</table>
</div>
<div class="fun">
<p>With the help of <i>callback hooks</i> you can create <i>beautiful</i> animated <i>creations</i></p>
<p>drag new nodes in to make the <i>graph larger</i></p>
<br>
<br>
<p>this partucular graph uses <i>diijkstra's shortest path</i> to constantly highlight the shortest path between A and B</p>
<p>Meanwhile the <i>edge weight</i> is set to the <i>euclidean distance</i> between nodes</p>
</div>
<div class="docs">
<p>graph editor Library allows for callback hooks</p>
<br>
<pre>
<code class="javascript">
let g = new Graph("somecanvasid")
g.setTickCallback(function)
</code>
</pre>
<p style="margin-left:15px;">the tickCallback is called every frame</p>
<p style="margin-left:15px;">and the callback is passed the entire graphInstance as an argument</p>
<pre>
<code class="javascript">
g.setNodeSetupCallback(function)
</code>
</pre>
<p style="margin-left:15px;">the nodeSetupCallback is called everytime a new node is created either programatically or created by user</p>
<p style="margin-left:15px;">nodeSetupCallback is passed just the created node as an argument</p>
<pre>
<code class="javascript">
g.setNodeCreatedCallback(function)
</code>
</pre>
<p style="margin-left:15px;">the nodeCreatedCallback is only called when ui nodes are drawn in</p>
<p style="margin-left:15px;">if nodeCreatedCallback is set and nodeSetupCallback is set both will be called</p>
<p style="margin-left:15px;">nodeCreatedCallback is passed just the created node as an argument</p>
<pre>
<code class="javascript">
g.setConnectionSetupCallback(function)
</code>
</pre>
<p style="margin-left:15px;">the ConnectionSetupCallback is called whenever a new connection is made programatically or by user</p>
<p style="margin-left:15px;">the ConnectionSetupCallback is passed just the new edge created as an argument</p>
<pre>
<code class="javascript">
g.setConnectionCreatedCallback(function)
</code>
</pre>
<p style="margin-left:15px;">the connectionCreatedCallback is called only when a new connection is made through the UI</p>
<p style="margin-left:15px;">the connectionCreatedCallback is passed just the new edge as an argument</p>
</div>
</div>
</div>
<br>
<script>
let switchers = Array.from(document.getElementsByClassName("switchbox"));
function flipper(event){
let flip = "fun";
let flop = "docs";
if(this.checked){
}
else{
flip = "docs";
flop = "fun";
}
let funs = Array.from(document.getElementsByClassName(flip));
funs.map((item)=>item.style.display = "none");
let docs = Array.from(document.getElementsByClassName(flop));
docs.map((item)=>item.style.display = "block");
if(flip === "fun"){
switchers.map((item)=>{item.checked = true})
}
else{
switchers.map((item)=>{item.checked = false})
}
}
switchers.map((switchbox) => switchbox.addEventListener("change", flipper));
</script>
<script>
let canvas = document.getElementById("canvas");
let graph = new Graph("canvas");
let start = graph.node(100, 100, 40, "start");
let medium = graph.node(300, 200, 35, "thing");
let end = graph.node(100, 400, 40, "end");
start.directional(medium, 13);
medium.biDirectional(end, 5);
end.directional(start, 1);
//Graph.load(graph, canvas);
let nodeking3 = new Graph("canvas3");
function animateNode(node){
console.log("-->")
console.log(node.contextid)
let graph = Graph.getContext(node.contextid);
console.log(graph)
let x = (Math.random()*6)-3;
let y = (Math.random()*6)-3;
function move(){
node.x += x;
node.y += y;
if(node.x-node.r < 0){
if(x < 0){
x *= -1;
}
}
if(node.x+node.r > graph.canvas.width){
if(x > 0){
x *= -1;
}
}
if(node.y+node.r > graph.canvas.width){
if(y > 0){
y *= -1
}
}
if(node.y-node.r < 0){
if(y < 0){
y *= -1
}
}
for(let key of Object.keys(node.edges)){
graph.getEdgeById(key).setText(Math.round(nodeking3.getDistance(node.id, node.edges[key])))
}
setTimeout(move, 1000/60)
}
move()
}
nodeking3.setNodeSetupCallback(animateNode)
nodeking3.vars.tick = 0;
function doDiijkstra(graph){
graph.vars.tick +=1
if(graph.vars.tick % 20 == 0){
Object.values(graph.edges).map(edge => edge.setColor("black"));
graph.diijkstra(graph.vars.A.id, graph.vars.B.id)
}
}
nodeking3.setTickCallback(doDiijkstra)
nodeking3.vars.A = nodeking3.node(100, 100, 20, "A")
nodeking3.vars.B = nodeking3.node(200, 200, 20, "B")
nodeking3.vars.C = nodeking3.node(46, 60, 20)
nodeking3.vars.D = nodeking3.node(370, 285, 20)
nodeking3.vars.E = nodeking3.node(250, 90, 20)
nodeking3.vars.F = nodeking3.node(400, 370, 20)
nodeking3.vars.A.connect(nodeking3.vars.C)
nodeking3.vars.C.connect(nodeking3.vars.D)
nodeking3.vars.C.connect(nodeking3.vars.E)
nodeking3.vars.E.directional(nodeking3.vars.D)
nodeking3.vars.A.connect(nodeking3.vars.E)
nodeking3.vars.D.connect(nodeking3.vars.F)
nodeking3.vars.F.connect(nodeking3.vars.B)
nodeking3.vars.B.connect(nodeking3.vars.D)
</script>
</div>
</body>
</html>