-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
56 lines (50 loc) · 1.21 KB
/
main.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
import { canvas, draw } from './modules/canvas.js';
import { graph } from './modules/graph.js';
import { closeContextMenu } from './modules/context-menu.js';
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
function move(e) {
let toDraw = false;
if (graph.tmpEdge) {
graph.tmpEdge.to = e;
toDraw = true;
}
const selection = graph.getSelection();
if (selection && e.buttons) {
selection.x = e.x;
selection.y = e.y;
toDraw = true;
}
if (toDraw) {
draw();
}
}
function down(e) {
graph.resetSelection();
if (e.button === 1) {
return;
}
let target = graph.getNodeWithin(e.x, e.y);
if (target) {
graph.newSelection(target);
if (graph.tmpEdge) {
graph.addEdge(graph.tmpEdge.from, graph.getSelection());
graph.tmpEdge = undefined;
}
draw();
} else {
graph.tmpEdge = undefined;
}
}
function up() {
closeContextMenu();
draw();
}
window.onresize = resize;
resize();
canvas.addEventListener('mousemove', move);
canvas.addEventListener('mousedown', down);
canvas.addEventListener('mouseup', up);