-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyd3.html
139 lines (112 loc) · 4.37 KB
/
myd3.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
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/d3.min.js"></script>
</head>
<body>
<script>
var width = 900,
height = 600,
nbrBubbles = 20;
// var spaceCircles = [{ "x":130, "y":70, "r":34, "col":"#F29426" },
// { "x":40, "y":23, "r":12 , "col":"#E26446" },
// { "x":45, "y":43, "r":8 , "col":"#C23466" }];
var bubbles = d3.range(nbrBubbles).map( function (d,i) { return {"r":Math.random()**2 * 5 + 10,
"cat": i % 10}; });
var colors = d3.scaleOrdinal(d3.schemeCategory10);
bubbles[0].x = width / 2;
bubbles[0].y = height / 2;
bubbles[0].fixed = true;
var svgContainer = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "3 pt solid black")
.style("background", "white")
.style("border","2pt solid black");
// var circleGroup = svgContainer.append("g");
// var circles = circleGroup.selectAll("circle")
// .data(bubbles)
// .enter()
// .append("circle");
//
// var circleAttributes = circles
// .attr("cx", function (d) { return d.x; })
// .attr("cy", function (d) { return d.y; })
// .attr("r", function (d) { return d.r; } )
// .style("fill", function (d, i) { return colors(i % 10); } );
// var xCenter = d3.range(10).map ( function (i) { return width / 11 * (i+1); });
var xCenter = d3.range(10).map ( function () { return width / 2; });
var forceSimulation = d3.forceSimulation(bubbles)
.force("charge", d3.forceManyBody().strength( function (d,i) {
return i ? 0 : 300 ;}))
// .force("x", d3.forceX().x( function (d) {
// return xCenter[d.cat]; } ))
// .force("y", d3.forceY().y( function (d) {
// return height / 2; } ))
.force("collision", d3.forceCollide().radius(function(d) {
return d.r; }).strength(0.9))
.on("tick", ticked);
var score = 0;
var gameOver = false;
var scoreGroup = svgContainer.append("g")
.attr("transform", "translate(width * 0.8,height * 0.2)");
var scoreBox = scoreGroup.append("rect")
.attr("width", width * 0.2)
.attr("height", height * 0.1)
.style("fill", "white");
var scoreText = scoreGroup.append("text")
.attr("x",5)
.attr("y",25);
svgContainer.on("mousemove", function () {
var p = d3.mouse(this);
bubbles[0].fx = p[0];
bubbles[0].fy = p[1];
if (!gameOver)
{
forceSimulation.alpha(1);
forceSimulation.restart();
score++;
}
});
function ticked() {
var u = d3.select("svg")
.selectAll("circle")
.data(bubbles);
u.enter()
.append("circle")
.attr("r", function (d) { return d.r; })
.merge(u)
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; })
.style("fill", function (d,i) { return i ? colors(d.cat) : "red"; } );
u.exit().remove();
var dist = bubbles.map( function (d) {
return Math.hypot(d.x - bubbles[0].x, d.y - bubbles[0].y); })
.slice(1);
scoreText.text( function () { return "Score: " + (score / 10);});
if (Math.min(...dist) < 20)
{
console.log(Math.min(...dist))
forceSimulation.stop();
gameOver = true;
svgContainer.append("g")
.attr("transform","translate(" + width/2 + "," + height/2 + "), scale(2)")
.append("text")
.text("Game Over!")
.style("font-family","sans-serif");
};
}
// var axisScale = d3.scaleLinear()
// .domain([0, 100])
// .range([15, height-15]);
//
// var axis = d3.axisLeft(axisScale);
//
// var axisGroup = svgContainer.append("g")
// .attr("transform","translate(30,0)")
// .call(axis);
//
</script>
</body>
</html>