-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (59 loc) · 1.28 KB
/
index.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
import constant from 'd3-force/src/constant';
export default function () {
let nodes;
let sizes;
let bounds;
let sizeFn = constant([0, 0]);
function force() {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
const size = sizes[i];
const xi = node.x + node.vx;
const x0 = bounds[0][0] - xi + size[0];
const x1 = bounds[1][0] - (xi + size[0]);
const yi = node.y + node.vy;
const y0 = bounds[0][1] - yi + size[1];
const y1 = bounds[1][1] - (yi + size[1]);
if (x0 > 0 || x1 < 0) {
node.x += node.vx;
node.vx = -node.vx;
if (node.vx < x0) {
node.x += x0 - node.vx;
}
if (node.vx > x1) {
node.x += x1 - node.vx;
}
}
if (y0 > 0 || y1 < 0) {
node.y += node.vy;
node.vy = -node.vy;
if (node.vy < y0) {
node.vy += y0 - node.vy;
}
if (node.vy > y1) {
node.vy += y1 - node.vy;
}
}
}
}
force.initialize = function (_) {
nodes = _;
sizes = nodes.map(sizeFn);
};
force.bounds = function (_) {
bounds = _;
return arguments.length > 0 ? (bounds, force) : bounds;
};
force.size = function (_) {
if (arguments.length > 0) {
if (typeof _ === 'function') {
sizeFn = _;
} else {
sizeFn = constant(_);
}
return force;
}
return sizeFn;
};
return force;
}