-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestForce.html
52 lines (43 loc) · 1.28 KB
/
testForce.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<canvas width="960" height="500"></canvas>
<script src="d3/d3.js"></script>
<script>
var n = 20;
var nodes = d3.range(n * n).map(function(i) {
return {
index: i,
color: i < 200 ? "brown" : "steelblue"
};
});
var canvas = document.querySelector("canvas"),
context = canvas.getContext("2d"),
width = canvas.width,
height = canvas.height;
var simulation = d3.forceSimulation(nodes)
.velocityDecay(0.4)
.force("y", d3.forceY())
.force("brown", isolate(d3.forceX(-width / 6), function(d) { return d.color === "brown"; }))
.force("steelblue", isolate(d3.forceX(width / 6), function(d) { return d.color === "steelblue"; }))
.force("charge", d3.forceManyBody().strength(-10))
.on("tick", ticked);
function ticked() {
context.clearRect(0, 0, width, height);
context.save();
context.translate(width / 2, height / 2);
nodes.forEach(drawNode);
context.restore();
}
function drawNode(d) {
context.beginPath();
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
context.fillStyle = d.color;
context.fill();
}
function isolate(force, filter) {
var initialize = force.initialize;
force.initialize = function() { initialize.call(force, nodes.filter(filter)); };
return force;
}
</script>