-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce-definitions.js
80 lines (68 loc) · 2.51 KB
/
force-definitions.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
const forceDefinitions = (function(){
const geoPositionForce = (function(projection) {
const force = (alpha) => {
if(force.savedNodes && force.savedNodes.length) {
for(const node of force.savedNodes) {
if(node.lat !== undefined) {
const target = projection([node.lng, node.lat]);
const difference = {
x: target[0] - node.x,
y: target[1] - node.y
};
node.x += difference.x * alpha * force.currentStrength;
node.y += difference.y * alpha * force.currentStrength;
}
}
}
};
force.savedNodes = [];
force.strencurrentStrengthgth = 1;
force.initialize = (nodes) => {
if(!nodes || !nodes.length) {
return;
}
force.savedNodes = nodes;
};
force.strength = (strength) => {
force.currentStrength = strength;
return force;
};
return force;
});
const centeringForce = (function(cx, cy) {
const force = (alpha) => {
if(force.savedNodes && force.savedNodes.length) {
for(const node of force.savedNodes) {
const difference = {
x: cx - node.x,
y: cy - node.y
};
const differenceLength = Math.sqrt(Math.pow(difference.x, 2) + Math.pow(difference.y, 2));
const normalizedDifference = {
x: difference.x / differenceLength,
y: difference.y / differenceLength
};
node.x += normalizedDifference.x * Math.pow(differenceLength, 1.4) * alpha * force.currentStrength;
node.y += normalizedDifference.y * Math.pow(differenceLength, 1.4) * alpha * force.currentStrength;
}
}
};
force.savedNodes = [];
force.strencurrentStrengthgth = 1;
force.initialize = (nodes) => {
if(!nodes || !nodes.length) {
return;
}
force.savedNodes = nodes;
};
force.strength = (strength) => {
force.currentStrength = strength;
return force;
};
return force;
});
return {
geoPositionForce,
centeringForce
};
})();