-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1055 lines (966 loc) · 32.9 KB
/
index.html
File metadata and controls
1055 lines (966 loc) · 32.9 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Node-Forces Example</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
background-color: #222;
}
html, body {
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif
}
html,
body,
svg {
width: 100%;
height: 100%;
}
.default {
stroke-width: 3;
stroke: #AAA;
fill: #90F;
}
.arrowhead {
stroke-width: 0;
fill: #000;
}
.connection {
stroke-width: 1.5;
}
.node {
cursor: pointer;
}
.node:hover {
fill: #A3F;
}
.node.active {
stroke: #FF0;
}
.node.active.drag {
stroke: #FFF;
}
.drag, .drag * {
cursor: none;
pointer-events: none;
}
.help {
position: absolute;
right: 12pt;
top: 12pt;
opacity: 0.8;
font-size: 10pt;
}
.help.active {
background-color: #666666;
color: #FFF;
border-radius: 4pt;
padding: 12pt;
max-height: calc(100% - 24pt);
overflow: auto;
}
.help .toggle {
cursor: pointer;
pointer-events: all;
position: absolute;
top: 8pt;
right: 8pt;
width: 18pt;
height: 18pt;
border-radius: 9pt;
text-align: center;
line-height: 18pt;
opacity: 0.5;
background-color: #FFF;
color: #000;
font-size: 14pt;
text-shadow: none;
}
.help .toggle::before {
content: "?";
}
.help.active .toggle {
border: 1px solid #FFF;
color: #FFF;
background-color: transparent;
}
.help.active .toggle::before {
content: "X";
}
.help:not(.active) .content {
display: none;
}
.help .content,
.help .content table {
color: inherit;
font-size: inherit;
}
.help .content h1 {
text-align: center;
font-size: 120%;
}
.help .content p {
max-width: 90ex;
margin-left: auto;
margin-right: auto;
padding: 2ex;
}
th, td {
padding: 2pt;
padding-bottom: 8pt;
}
th {
text-align: right;
}
.controls {
position: absolute;
left: 0;
bottom: 0;
padding-bottom: 16pt;
}
.controls button {
width: 40pt;
height: 40pt;
border-radius: 20pt;
text-align: center;
background-color: rgba(255,255,255,0.8);
border: 0;
padding: 8pt;
margin: 0 4pt;
margin-top: 0;
margin-bottom: 0;
float: left;
white-space: nowrap;
}
.controls button.active {
background-color: yellow;
}
.controls button * {
white-space: nowrap;
height: 100%;
line-height: 24pt;
width: auto;
display: inline-block;
font-size: 18pt;
font-weight: bold;
margin: 0;
padding: 0;
}
.controls button:focus {
outline: 0;
}
.control-icon {
fill: black;
}
</style>
<script>
// @ts-check
'use strict';
const SVG_NS = "http://www.w3.org/2000/svg";
const LATENCY = .8;
const EFFICIENCY = .99;
window.onload = () => {
document.body.oncontextmenu = e => {
e.preventDefault();
}
document.querySelector('.help .toggle').addEventListener("mousedown", e => {
document.querySelector('.help').classList.toggle('active');
});
let graph = new Graph();
// @ts-ignore
graph.redraw(document.getElementById('mainSvgCanvas'));
};
/**
* Superclass of anything that is involved with showing a graph.
*
* This includes nodes, connections, and the graph itself.
*/
class GraphChild {
/**
* @param {SVGElement} svgElement
* @param {Graph} graph
*/
constructor(svgElement, graph) {
/**
* @protected
*/
this._element = svgElement;
this.element.classList.add('default');
this.id = GraphChild._nextId++;
this.graph = graph;
this.x = 0;
this.y = 0;
this.signal = 0;
}
/**
* The SVG element that represents this object.
*/
get element() {
return this._element;
}
/**
* Update the SVG element of the GraphChild.
*
* This applies a translation relative to 0,0 so that the element
* appears at its internal x,y coordinate, and also ensures that the
* GraphChild's element is a child of the given parent element.
*
* @param {SVGElement} parentElement The element that should parent
* the current GraphChild's element.
*/
redraw(parentElement) {
this.element.setAttribute("transform", `translate(${this.x} ${this.y})`);
const sCol = (this.signal * 255)|0;
const { style } = this.element;
style.stroke = `rgb(${sCol}, ${sCol}, ${sCol})`;
style.strokeWidth = `${3 + this.signal * 6}`;
if (this.element.parentNode === parentElement) {
return;
}
if (this.element.parentNode) {
this.element.parentNode.removeChild(this.element);
}
parentElement.appendChild(this.element);
}
update() {
}
/**
* @param {number} strength
*/
sendSignal(strength) {
this.signal += strength;
}
}
GraphChild._nextId = 0;
/**
* A node in the graph.
*
* Contains most of the user interaction logic, except for the parts that
* don't involve any existing nodes, such as that which involves
* interacting with empty space.
*/
class GraphNode extends GraphChild {
/**
* @param {Graph} graph
* @param {number} x
* @param {number} y
*/
constructor(graph, x, y) {
super(document.createElementNS(SVG_NS, "circle"), graph);
this.element.setAttribute("r", "20");
this.element.classList.add('node');
this.element.addEventListener("mousedown", this.handleMouseDown());
this.x = x;
this.y = y;
/** @private */
this._mouseStartX = 0;
/** @private */
this._mouseStartY = 0;
/**
* @type {(e: MouseEvent) => void}
* @private
*/
this._mouseMoveHandler = null;
/**
* @type {(e: MouseEvent) => void}
* @private
*/
this._mouseUpHandler = null;
this.active = false;
this.drag = false;
}
/**
* Marks this node as the active node, deactivating all other nodes.
*/
activate() {
this.graph.nodes.forEach(node => {
if (node === this) {
return;
}
node.deactivate();
});
this.active = true;
}
/**
* De-marks the node as active.
*/
deactivate() {
if (this.active) {
this.active = false;
console.log('Deactivated ' + this.id);
}
}
/**
* Produces the handler that handles what happens when the user clicks
* down on a node.
*/
handleMouseDown() {
return (/** @type {MouseEvent} */ e) => {
console.log('Clicked ' + this.id);
e.stopPropagation();
if (e.button === 0) {
if (e.altKey) {
if (this.active || !this.graph.activeNode) {
return;
}
if (e.shiftKey) {
this.modeConnectInverse();
} else {
this.modeConnect();
}
} else {
this[this.graph.mode](e);
}
} else if (e.button == 1) {
this.modeNodeToggle();
} else if (e.button == 2) {
this.modeSignal();
}
};
}
modeSelect(/** @type {MouseEvent} */ e) {
this.mouseStartXOffset = e.clientX - this.x;
this.mouseStartYOffset = e.clientY - this.y;
this.activate();
this.drag = true;
this._mouseMoveHandler = this.handleMouseMove();
window.addEventListener("mousemove", this._mouseMoveHandler);
this._mouseUpHandler = this.handleMouseUp();
window.addEventListener("mouseup", this._mouseUpHandler);
}
modeNodeToggle() {
this.graph.deleteNode(this);
}
modeNodeToggleAlt() {
this.graph.deleteNode(this);
}
modeConnect() {
const { activeNode } = this.graph;
if (this.graph.getConnection(activeNode, this)) {
this.graph.disconnect(activeNode, this);
} else {
this.graph.connect(activeNode, this);
}
}
modeConnectInverse() {
const { activeNode } = this.graph;
if (this.graph.getConnection(this, activeNode)) {
this.graph.disconnect(this, activeNode);
} else {
this.graph.connect(this, activeNode);
}
}
modeSignal() {
this.sendSignal(10);
}
/**
* Produces the handler that handles what should happen when the mouse
* moves after the mouse has been clicked.
*/
handleMouseMove() {
return (/** @type {MouseEvent} */ e) => {
if (!this.drag) {
this.endDrag();
return
}
this.x = e.clientX - this.mouseStartXOffset;
this.y = e.clientY - this.mouseStartYOffset;
}
}
/**
* Produces the handler that handles what should happen when mouse
* button is released after having been clicked.
*/
handleMouseUp() {
return (/** @type {MouseEvent} */ e) => {
this.endDrag();
};
}
/**
* Performs all state change necessary when its time to stop dragging.
*/
endDrag() {
this.drag = false;
if (this._mouseUpHandler) {
window.removeEventListener("mouseup", this._mouseUpHandler);
this._mouseUpHandler = null;
}
if (this._mouseMoveHandler) {
window.removeEventListener("mousemove", this._mouseMoveHandler);
this._mouseMoveHandler = null;
}
}
update() {
const newSignal = this.signal * LATENCY;
const conns = this.graph.getConnectionsOut(this);
conns.forEach(conn => {
conn.sendSignal((this.signal - newSignal) / conns.length);
})
this.signal = newSignal;
}
/**
* @param {SVGElement} parentElement
*/
redraw(parentElement) {
if (
this.graph.nodes.indexOf(this) === -1
&& this.element.parentNode
) {
this.element.parentNode.removeChild(this.element);
return;
}
super.redraw(parentElement);
if (this.active) {
this.element.classList.add('active');
this.element.style.stroke = '#FFFF00';
} else {
this.element.classList.remove('active');
}
if (this.drag) {
this.element.classList.add('drag');
document.body.classList.add('drag');
} else {
this.element.classList.remove('drag');
document.body.classList.remove('drag');
}
}
}
/**
* Connects two nodes unidirectionally.
*
* Connections are direct property of the graph rather than nodes.
*
* Note that a bidirectional connection is really just two unidirectional
* connections, where the direction of one is the opposite of the other.
*/
class GraphConnection extends GraphChild {
/**
* Connects rwo graph nodes.
*
* @param {Graph} graph
* @param {GraphNode} nodeOut
* @param {GraphNode} nodeIn
*/
constructor (graph, nodeOut, nodeIn) {
super(document.createElementNS(SVG_NS, "line"), graph);
this.element.setAttribute("marker-end", "url(#arrowhead)");
this.element.classList.add('connection');
/**
* The node on the "out" side of the connection
* @type {GraphNode}
*/
this.nodeOut = nodeOut;
/**
* The node on the "in" side of the connection
* @type {GraphNode}
*/
this.nodeIn = nodeIn;
}
update() {
const newSignal = this.signal * LATENCY;
this.nodeIn.sendSignal((this.signal - newSignal) * EFFICIENCY);
this.signal = newSignal;
}
/**
* @param {SVGElement} parentElement
*/
redraw(parentElement) {
super.redraw(parentElement);
const sa = this.element.setAttribute.bind(this.element);
sa('x1', `${this.nodeOut.x}`);
sa('y1', `${this.nodeOut.y}`);
sa('x2', `${this.nodeIn.x}`);
sa('y2', `${this.nodeIn.y}`);
}
}
/**
* @typedef {Object} GraphNodeDescriptor
* @property {GraphConnection[]} connectionsIn
* @property {GraphConnection[]} connectionsOut
*/
/**
* @typedef {Object} NodeDescriptorMap = WeakMap<GraphNode, NodeDescriptor>
*/
/**
* Represents the graph as a whole.
*
* Owns all the nodes and all the connections between them.
*
* Also handles interactions with empty space.
*/
class Graph extends GraphChild {
constructor() {
super(document.createElementNS(SVG_NS, "g"), null);
/**
* SVG element where connections are drawn.
*/
this.elementConnectionLayer = document.createElementNS(SVG_NS, "g");
/**
* SVG element where nodes are drawn.
*/
this.elementNodeLayer = document.createElementNS(SVG_NS, "g");
/**
* Internal list of all nodes.
*
* @private
* @type {GraphNode[]}
*/
this._nodes = [];
/**
* Holds information about nodes that the nodes don't know about.
*
* @private
* @type {WeakMap.<GraphNode, GraphNodeDescriptor>}
*/
this._nodeDescriptors = new WeakMap();
/**
* The handler that handles what happens when empty space is clicked.
*/
this._mouseDownEmptySpaceHandler = this.handleMouseDownEmptySpace();
document.body.addEventListener("mousedown", this._mouseDownEmptySpaceHandler);
/**
* Collection of all the mode buttons.
* @type {{ [key: string]: HTMLButtonElement }}
*/
this.buttons = {
'modeSelect': document.getElementById('modeSelect'),
'modeNodeToggle': document.getElementById('modeNodeToggle'),
'modeNodeToggleAlt': document.getElementById('modeNodeToggleAlt'),
'modeConnect': document.getElementById('modeConnect'),
'modeConnectInverse': document.getElementById('modeConnectInverse'),
'modeSignal': document.getElementById('modeSignal'),
}
/**
* Current mode.
* @type {string}
*/
this.mode = 'modeSelect';
for (let btnId in this.buttons) {
this.buttons[btnId]
.addEventListener("mousedown", this.handleClickModeButton());
}
this.refreshButtons();
const cycle = () => {
this.update();
this.redraw();
requestAnimationFrame(cycle);
};
cycle();
}
/**
* Update the appearance of the buttons.
*/
refreshButtons() {
for (let btnId in this.buttons) {
const btn = this.buttons[btnId];
if (btn.id === this.mode) {
btn.classList.add('active');
} else {
btn.classList.remove('active');
}
}
}
/**
* Produces a handler for what happens when any mode button is clicked.
*/
handleClickModeButton() {
return (/** @type {MouseEvent} */ e) => {
e.stopImmediatePropagation();
// @ts-ignore
this.mode = e.currentTarget.id;
console.log(`Current mode: ${this.mode}`);
this.refreshButtons();
}
}
/**
* Produces the handler for when empty space is clicked.
*/
handleMouseDownEmptySpace() {
return (/** @type {MouseEvent} */ e) => {
e.stopImmediatePropagation();
const { activeNode } = this;
if (e.button == 1 || this.mode.startsWith('modeNodeToggle')) {
const node = this.createNode(e.clientX, e.clientY, activeNode);
if (!e.shiftKey && !this.mode.endsWith('Alt')) {
node.activate();
}
} else if (activeNode) {
activeNode.deactivate();
}
};
}
/**
* Exposes the internal list of nodes.
* @todo Probably don't need to abstract it; this could be a direct prop
*/
get nodes() {
return this._nodes;
}
/**
* Create new node at specified coordinate and connect to graph
*
* @param {number} x
* @param {number} y
* @param {GraphNode?} nodeOut Optional outgoing node to connect to
*/
createNode(x, y, nodeOut = null) {
const node = new GraphNode(this, x, y);
this._nodes.push(node);
this._nodeDescriptors.set(node, {
connectionsIn: [],
connectionsOut: []
});
if (nodeOut) {
this.connect(nodeOut, node);
}
return node;
}
/**
* Delete a node and remove it from the graph
*
* @param {GraphNode} node
*/
deleteNode(node) {
const i = this.nodes.indexOf(node);
this.nodes.splice(i, 1);
this.getConnectionsOut(node).slice().forEach(conn => {
this.disconnect(conn.nodeOut, conn.nodeIn);
});
this.getConnectionsIn(node).slice().forEach(conn => {
this.disconnect(conn.nodeOut, conn.nodeIn);
});
if (node.element.parentElement) {
node.element.parentElement.removeChild(node.element);
}
}
/**
* Connect an outgoing node to an incoming node
*
* This does NOT prevent duplicate connctions between nodes. Calling
* code is responsible for preventing this if it is a problem.
*
* @param {GraphNode} nodeOut
* @param {GraphNode} nodeIn
* @return {GraphConnection}
*/
connect(nodeOut, nodeIn) {
const conn = new GraphConnection(this, nodeOut, nodeIn);
const { _nodeDescriptors: nodeDescs } = this;
const { connectionsOut } = nodeDescs.get(nodeIn);
if (connectionsOut.indexOf(conn) === -1) {
connectionsOut.push(conn);
}
const { connectionsIn } = nodeDescs.get(nodeOut);
if (connectionsIn.indexOf(conn) === -1) {
connectionsIn.push(conn);
}
console.log(`Connected ${nodeOut.id} -> ${nodeIn.id}`);
return conn;
}
/**
* Disconnect an outgoing node from an incoming node.
*
* This does NOT check to see if the nodes are already connected. An
* error may occur if you attempt to disconnect nodes that are not
* connected.
*
* @param {GraphNode} nodeOut
* @param {GraphNode} nodeIn
*/
disconnect(nodeOut, nodeIn) {
const { _nodeDescriptors: nodeDescs } = this;
const { connectionsOut: connsO } = nodeDescs.get(nodeIn);
const connO = connsO.find(conn => conn.nodeOut === nodeOut);
connsO.splice(connsO.indexOf(connO), 1);
if (connO.element.parentElement) {
connO.element.parentElement.removeChild(connO.element);
}
const { connectionsIn: connsI } = nodeDescs.get(nodeOut);
const connI = connsI.find(conn => conn.nodeIn === nodeIn)
connsI.splice(connsI.indexOf(connI), 1);
if (connI.element.parentElement) {
connI.element.parentElement.removeChild(connI.element);
}
console.log(`Disconnected ${nodeOut.id} -> ${nodeIn.id}`);
}
/**
* Returns the connection object that connects two nodes.
*
* Order of nodes is important. It's possible for two nodes to have
* two connections and have those connection directions be different,
* in which case the connection you get will depend on the order of
* the nodes.
*
* @param {GraphNode} nodeOut
* @param {GraphNode} nodeIn
*/
getConnection(nodeOut, nodeIn) {
return this._nodeDescriptors.get(nodeIn).connectionsOut
.find(conn => conn.nodeOut === nodeOut);
}
/**
* The node that is currently marked as active.
*/
get activeNode() {
return this._nodes.find(({ active }) => active);
}
/**
* Returns all incoming connections to a node.
*
* @param {GraphNode} node
* @return {GraphConnection[]}
*/
getConnectionsOut(node) {
return this._nodeDescriptors.get(node).connectionsIn;
}
/**
* Returns all outgoing connections to a node.
*
* @param {GraphNode} node
* @return {GraphConnection[]}
*/
getConnectionsIn(node) {
return this._nodeDescriptors.get(node).connectionsOut;
}
update() {
const { innerWidth: ww, innerHeight: wh } = window;
const forces = this.nodes.map(node => {
const { x: x1, y: y1, signal: s1 } = node;
const pulls = this.getConnectionsOut(node).map(conn => {
conn.update();
const node2 = conn.nodeIn;
const [x2, y2, s2] = [
node2.x,
node2.y,
node2.signal
];
const [dx, dy] = [(x2 - x1), (y2 - y1)];
const q1 = .01 * (s1 + s2);
return [dx * q1, dy * q1];
});
const pushes = this.nodes.map(node2 => {
if (node === node2) {
return [0,0];
}
const [x2, y2, s2] = [
node2.x,
node2.y,
node2.signal
];
const q2 = -60 * (s1 + s2 + 0.1);
const [dx, dy] = [(x2 - x1), (y2 - y1)];
const m = Math.sqrt((dx * dx) + (dy * dy));
const [nx, ny] = [dx / m, dy / m];
const s = 1 / m * q2;
return [nx * s , ny * s];
});
const q3 = 0.001;
const [x2, y2] = [ww / 2, wh / 2];
const [dx, dy] = [(x2 - x1), (y2 - y1)];
const centerPull = [dx * q3, dy * q3]
return [...pushes, ...pulls, centerPull];
});
this.nodes.forEach((node, i) => {
if (node.drag) {
return;
}
forces[i].forEach(([dx, dy]) => {
node.x += dx;
node.y += dy;
if (node.x < 0) {
node.x = 0;
}
if (node.y < 0) {
node.y = 0;
}
if (node.x > ww) {
node.x = ww;
}
if (node.y > wh) {
node.y = wh;
}
});
node.update();
});
}
/**
* @param {SVGElement} parentElement
*/
redraw(parentElement = null) {
if (parentElement === null) {
// @ts-ignore
parentElement = this.element.parentElement;
}
super.redraw(parentElement);
const {
elementConnectionLayer: connLayer,
elementNodeLayer: nodeLayer
} = this;
if (connLayer.parentNode !== parentElement) {
if (connLayer.parentNode) {
connLayer.parentNode.removeChild(connLayer);
}
parentElement.appendChild(connLayer);
}
if (nodeLayer.parentNode !== parentElement) {
if (nodeLayer.parentNode) {
nodeLayer.parentNode.removeChild(nodeLayer);
}
parentElement.appendChild(nodeLayer);
}
this._nodes.forEach(node => {
node.redraw(nodeLayer);
this.getConnectionsIn(node).forEach(conn => {
conn.redraw(connLayer);
})
})
}
}
</script>
</head>
<body>
<svg id="mainSvgCanvas">
<defs>
<marker id="arrowhead"
markerWidth="15"
markerHeight="16"
refX="30"
refY="8"
orient="auto"
markerUnits="userSpaceOnUse"
>
<polygon class="default arrowhead" points="0,0, 10,8, 0,16" />
</marker>
</defs>
</svg>
<div class="help">
<div class="toggle"></div>
<div class="content">
<h1>Controls</h1>
<p>All controls are available as buttons at the bottom-left. However,
while the left-most button is selected, you can also use any of those
function via different mouse and key combos:</p>
<table>
<tr>
<th>Middle-Click Empty Space</th>
<td>
Create a new node with incoming connection from selected node,
then select the new node
</td>
</tr>
<tr>
<th>Shift-Middle-Click Empty Space</th>
<td>
Create a new node with incoming connection from selected node,
but keep the old node selected
</td>
</tr>
<tr>
<th>Click Empty Space</th>
<td>Deactivate current node</td>
</tr>
<tr>
<th>Click Node</th>
<td>Activate the node</td>
</tr>
<tr>
<th>Drag Node</th>
<td>Move the node</td>
</tr>
<tr>
<th>Middle-Click Node</th>
<td>Delete the node, severing all connections to and from it</td>
</tr>
<tr>
<th>Alt-Click Node</th>
<td>
Toggle incoming connection to the clicked node from the
active node
</td>
</tr>
<tr>
<th>Shift-Alt-Click Node</th>
<td>
Toggle outgoing connection from the clicked node to the
active node
</td>
</tr>
<tr>
<th>Right-Click Node</th>
<td>Send signal</td>
</tr>
</table>
<p>
Note that a bidirectional connection between two nodes can be
created by toggling on each direction individually.
</p>
</div>
</div>
<div class="controls">
<button id="modeSelect">
<svg
width="24.519518"
height="32"
viewBox="0 0 24.519516 31.999999"
>