-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction_map.js
139 lines (114 loc) · 3.98 KB
/
action_map.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
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
const SVG_NS = 'http://www.w3.org/2000/svg';
const MAX_X = 816000;
const MAX_Y = 816000;
class ActionMap {
constructor(slider, match) {
this.slider_ = slider;
this.match_ = match;
this.boundMouseDrag_ = this.onMouseDrag_.bind(this);
this.dragging_ = false;
this.element_ = document.createElement('div');
this.element_.id = 'actionmap';
this.svg_ = document.createElementNS(SVG_NS, 'svg');
let map = document.createElementNS(SVG_NS, 'image');
map.setAttribute('width', MAX_X);
map.setAttribute('height', MAX_Y);
map.setAttribute('href', 'https://github.com/pubg/api-assets/raw/master/assets/maps/Erangel_Minimap_lowres.jpg');
this.svg_.appendChild(map);
this.playerCircles_ = [];
match.players().forEach(function(player) {
let circle = this.createCircleForPlayer_(player);
this.playerCircles_.push(circle);
this.svg_.appendChild(circle);
}, this);
let viewbox = this.svg_.createSVGRect();
viewbox.width = MAX_X;
viewbox.height = MAX_Y;
this.viewbox = viewbox;
slider.addListener(this.onUpdate_.bind(this));
this.svg_.addEventListener('wheel', this.onMouseWheel_.bind(this));
this.svg_.addEventListener('mousedown', this.onMouseDown_.bind(this));
document.addEventListener('mouseup', this.onMouseUp_.bind(this));
this.element_.appendChild(this.svg_);
}
render(parent) {
parent.appendChild(this.element_);
}
onUpdate_() {
let time = this.slider_.getValue();
this.match_.players().forEach(function(player, index) {
let pos = player.locationAtTime(time);
let circle = this.playerCircles_[index];
let scale = this.viewbox.width / MAX_X;
circle.setAttribute('transform', 'translate(' + pos.x + ' ' + pos.y + ') scale(' + scale + ')');
}, this);
}
createCircleForPlayer_(player) {
let group = document.createElementNS(SVG_NS, 'g');
group.classList.add('playercircle');
let circle = document.createElementNS(SVG_NS, 'circle');
let text = document.createElementNS(SVG_NS, 'text');
text.textContent = String(player.teamId);
group.appendChild(circle);
group.appendChild(text);
return group;
}
onMouseDown_(e) {
this.dragging_ = true;
this.lastX_ = e.pageX;
this.lastY_ = e.pageY;
document.addEventListener('mousemove', this.boundMouseDrag_);
}
onMouseUp_(e) {
if (this.dragging_) {
document.removeEventListener('mousemove', this.boundMouseDrag_);
this.dragging_ = false;
}
}
onMouseDrag_(e) {
e.stopPropagation();
e.preventDefault();
let newViewbox = this.viewbox;
newViewbox.x = newViewbox.x - (e.pageX - this.lastX_) * newViewbox.width / this.svg_.clientWidth;
newViewbox.y = newViewbox.y - (e.pageY - this.lastY_) * newViewbox.height / this.svg_.clientHeight;
this.viewbox = newViewbox;
this.lastX_ = e.pageX;
this.lastY_ = e.pageY;
}
onMouseWheel_(e) {
if (e.deltaY == 0) {
return;
}
let newViewbox = this.viewbox;
if (e.deltaY < 0) {
newViewbox.width /= 2;
newViewbox.height /= 2;
} else {
newViewbox.width *= 2;
newViewbox.height *= 2;
}
let clickOnScreenX = e.offsetX / this.svg_.clientWidth;
let clickOnScreenY = e.offsetY / this.svg_.clientHeight;
let clickX = this.viewbox.x + this.viewbox.width * clickOnScreenX;
let clickY = this.viewbox.y + this.viewbox.height * clickOnScreenY;
newViewbox.x = clickX - newViewbox.width * clickOnScreenX;
newViewbox.y = clickY - newViewbox.height * clickOnScreenY;
this.viewbox = newViewbox;
}
get viewbox() {
return this.copyViewbox_(this.viewBox_);
}
set viewbox(box) {
this.viewBox_ = this.copyViewbox_(box);
this.svg_.setAttribute('viewBox', box.x + ' ' + box.y + ' ' + box.width + ' ' + box.height);
this.onUpdate_();
}
copyViewbox_(viewbox) {
let ret = this.svg_.createSVGRect();
ret.x = viewbox.x;
ret.y = viewbox.y;
ret.width = viewbox.width;
ret.height = viewbox.height;
return ret;
}
}