-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionSimulation.html
308 lines (238 loc) · 8.47 KB
/
positionSimulation.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html>
<head>
<title>DistanceGraph</title>
<meta charset="utf-8">
<link href=css/bootstrap.min.css rel=stylesheet media="screen">
</head>
<style>
svg {
font: 10px sans-serif;
}
.background path {
fill: none;
stroke: #fff;
stroke-opacity: .4;
shape-rendering: crispEdges;
}
.foreground path {
fill: none;
stroke: steelblue;
stroke-width:1;
stroke-opacity: .7;
}
.brush .extent {
fill-opacity: .4;
shape-rendering: crispEdges;
}
.axis line,
.axis path {
fill: none;
stroke: #393b79;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 2px 0 #fff;
cursor: move;
}
.outer {
fill: rgba(255,255,255,0.33);
stroke: #000;
}
.legend-box {
display: inline-block;
min-width: 1em;
min-height: 1em;
background-color: red;
}
.legend-circle {
display: inline-block;
min-height: 1em;
min-height: 1em;
background-color: red;
}
</style>
<script src="js/d3.min.js"></script>
<script src="js/jquery-1.11.1.min.js"></script>
<body>
<a class="btn btn-default" href="index.html"> Back </a>
<div class="container">
<h1>Gameplay loop <small>(10x)</small></h1>
<h5>Game progress: <span id="gtime"></span></h5>
<div id="svgHere" class="col-lg-6"></div>
<div class="col-lg-6">
<h3><small>Legend</small></h3>
<hr>
<h5><small>Players</small></h5>
<div id="players-legend">
</div>
<hr>
<h5><small>Teams</small></h5>
<div>
<div><span id="legend-radiant" class="legend-box"></span> Radiant (Won)</div>
<div><span id="legend-dire" class="legend-box"></span> Dire (Lost)</div>
</div>
<div>
<h3><small>Options</small></h3>
<label><input type="checkbox" id="teamcolor" checked> Toggle player lines</label>
</div>
</div>
</div>
<script>
$(function(){
var drawLinesCondition = true;
$('#teamcolor').change(function(){
if(!$(this).is(":checked")){
drawLinesCondition = false;
$('.trajectory').remove();
} else {
drawLinesCondition = true;
}
});
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 552 - margin.left - margin.right,
height = 552 - margin.top - margin.bottom;
// Defines your svg element and also as a "g" (graphic) element that you can translate for your visualization
var svg = d3.select("#svgHere").append("svg")// where are you placing your graphic (body) (div)
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); //
var field = svg.selectAll("image").data([0]);
field.enter()
.append("svg:image")
.attr("width", 512)
.attr("height", 512)
.attr("xlink:href", "img/map.jpg");
// We draw the outer rectangle based on the outer class
var overlay = svg.append("rect")
.attr("class", "outer")
.attr("width", width)
.attr("height", height);
var trajectories = svg.append("g");
var playersGroup = svg.append("g");
//------------------------------------------------------------//
// We read the data
d3.csv("sample-data/zones_single.csv",function(error, data) {
data.forEach(function(d) {
d.t = +d.t;
d.x = +d.x;
d.y = +d.y;
d.match = +d.match;
d.team = d.team;
d.player = d.player;
d.won = (+d.won==1);
d.tstd = +d.tstd;
d.tsync = +d.tsync;
d.tper = +d.tper;
d.tier = d.tier;
d.zone = d.zone;
});
var timestampData = new Object();
data.forEach(function(d){
timestampData[d.tsync] = timestampData[d.tsync] || [];
timestampData[d.tsync].push(d);
});
//console.log(timestampData);
var vis = svg,
xRange = d3.scale.linear().range([margin.left, width - margin.right]).domain([d3.min(data, function(d) {
return d.x;
}), d3.max(data, function(d) {
return d.x;
})]),
yRange = d3.scale.linear().range([height - margin.top, margin.bottom]).domain([d3.min(data, function(d) {
return d.y;
}), d3.max(data, function(d) {
return d.y;
})]),
color = d3.scale.category10(),
color20 = d3.scale.category20();
//colors for legend (hardcoded for simplicity)
$('#legend-radiant').css('background-color', color('radiant'));
$('#legend-dire').css('background-color', color('dire'));
//set up legend for players
var setLegends = (function() {
var playersInGame = timestampData[0];
console.log(playersInGame);
playersInGame.forEach(function(player){
$('#players-legend').append(
'<div><span class="legend-'+player.team+'"> </span> '+
'<span class="legend-box" style="background-color: '+color(player.player)+'"></span> '+
unescape(player.player)+' ('+player.team+')</div>');
});
})();
var clearPlayers = function() {
svg.selectAll('.player').remove();
};
var render = function(dataframe) {
if(!dataframe)
return;
clearPlayers();
var player = playersGroup.selectAll(".player")
.data(dataframe)
.enter()
.append("g")
.attr("class", "player")
.call(translate)
player.append("circle")
.style("fill", function(d) { return color(d.player); })
.style("stroke-width", 4)
.style("stroke", function(d) { return color(d.team); })
.attr("r", 10);
function translate(player) {
player.attr("transform", function (d) {
return "translate("+xRange(d.x)+","+yRange(d.y)+")";
});
}
drawLines(dataframe);
}
var previousTimePoint;
var i=0;
var drawLines = function(timePoint){
if(!previousTimePoint){
previousTimePoint = timePoint;
return;
}
//now we have previous and next timepoint - draw a connecting line
for(var i=0; i<timePoint.length;i++){
if((Math.abs(previousTimePoint[i].x - timePoint[i].x) > 7) ||
Math.abs(previousTimePoint[i].y - timePoint[i].y) > 7)
continue;
//this will become really inefficient, but hey! that is not a point of this assignment :)
trajectories.append("line")
.attr("class", "trajectory")
.attr("x1", xRange(previousTimePoint[i].x))
.attr("y1", yRange(previousTimePoint[i].y))
.attr("x2", xRange(timePoint[i].x))
.attr("y2", yRange(timePoint[i].y))
.attr("stroke-width", "2")
.call(teamOrindividualLineCheck);
function teamOrindividualLineCheck(line){
if(drawLinesCondition){
line.attr("stroke", color(timePoint[i].team));
}
}
}
previousTimePoint = timePoint;
}
var deleteLines = function(){
$('.trajectory').remove();
}
var currentFrame = -1;
var totalFrames = Object.keys(timestampData).length;
var animationLoop = function() {
setTimeout(function() {
currentFrame = ++currentFrame % totalFrames;
if(currentFrame == 0)
deleteLines();
$('#gtime').html(parseInt((currentFrame/totalFrames)*100)+'%');
requestAnimationFrame(animationLoop);
render(timestampData[currentFrame]);
}, 1000 / 10);
}
animationLoop();
});
});
</script>
</body>
</html>