-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d.js
138 lines (110 loc) · 3.38 KB
/
2d.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
const myColors2D = [
[0, 0, 120],
[0, 0, 255],
[0, 120, 0],
[0, 120, 120],
[0, 120, 255],
[120, 0, 0],
[120, 120, 0],
[120, 120, 255],
[120, 255, 0],
[120, 255, 120],
[120, 255, 255],
];
function init2D() {
const canvas = document.querySelector('canvas#two');
const h = canvas.clientHeight;
const w = canvas.clientWidth;
const ctx = canvas.getContext('2d');
const pxRatio = window.devicePixelRatio;
const W = w * pxRatio;
const H = h * pxRatio;
canvas.width = W;
canvas.height = H;
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
ctx.translate(W / 2, H / 2);
const idMap = data.nodes.reduce(function (acc, node, i) {
acc[node.id] = i;
return acc;
}, {});
const Rmax = 5 * pxRatio;
const bounds = [-W / 2 + Rmax, -H / 2 + Rmax, W / 2 - Rmax, H / 2 - Rmax];
const myData = {...data};
// gonna be randomized anyway, that's just for the demo
som.randomize(myData.nodes, bounds);
render();
function render() {
const nodes = myData.nodes;
const edges = myData.edges;
ctx.clearRect(-W/2, -H/2, W, H);
ctx.beginPath();
edges.forEach((e) => { drawLink(e, ctx, idMap); });
ctx.strokeStyle = '#aaa';
ctx.stroke();
ctx.beginPath();
nodes.forEach((n) => { drawNode(n, ctx); });
ctx.fill();
ctx.strokeStyle = 'transparent';
ctx.stroke();
}
function drawLink(d, ctx, idMap) {
const source = myData.nodes[idMap[d.source]];
const target = myData.nodes[idMap[d.target]];
ctx.moveTo(source.x, source.y);
ctx.lineTo(target.x, target.y);
}
function drawNode(d, ctx) {
const r = d.size || Rmax;
ctx.moveTo(d.x + r, d.y);
ctx.arc(d.x, d.y, r, 0, 2 * Math.PI);
}
setTimeout(() => {
this.som(myData, {
dontRandomize: true,
bounds: bounds,
onUpdate: render,
onEnd: function () {
console.log('2D done');
},
iterationsPerUpdate: 5,
updateDelay: 16.666666
});
}, 1000);
let dragging = false;
let tolerance = Rmax * 2;
let draggedNode = null;
let dragTimer = 0;
canvas.addEventListener('mousedown', function (evt) {
dragging = true;
const ex = bounds[0] + evt.clientX * pxRatio,
ey = bounds[1] + evt.clientY * pxRatio;
setTimeout(function () {
for (let i = 0, len = myData.nodes.length; i < len; i++) {
const node = myData.nodes[i];
const dx = ex - node.x,
dy = ey - node.y;
if (Math.sqrt(dx * dx + dy * dy) < tolerance) {
draggedNode = node;
break;
}
}
}, 0)
});
canvas.addEventListener('mousemove', (evt) => {
if (dragging && draggedNode) {
draggedNode.x = bounds[0] + evt.clientX * pxRatio;
draggedNode.y = bounds[1] + evt.clientY * pxRatio;
clearTimeout(dragTimer);
dragTimer = setTimeout(render, 16);
}
});
canvas.addEventListener('mouseup', () => {
dragging = false;
draggedNode = null;
});
window.onresize = () => {
render();
};
}
init2D();