-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
200 lines (176 loc) · 7.03 KB
/
Copy pathindex.html
File metadata and controls
200 lines (176 loc) · 7.03 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>trains</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
<script src='railway_data.js'></script>
<script src='railway_path_data.js'></script>
<script src='trains_info_data.js'></script>
<script src='train.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.css' rel='stylesheet'/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
#pause {
width: inherit;
color: black;
text-align: center;
}
.pause-ctrl {
width: 100px;
}
</style>
<div id='map'></div>
<script>
class PauseControl {
onAdd(map) {
this._map = map;
this._container = document.createElement('div');
this._container.className = 'mapboxgl-ctrl mapboxgl-ctrl-group pause-ctrl';
this._container.innerHTML = "<button id='pause'></button>";
return this._container;
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
}
}
Train.load_railway_data(raw_railway_data);
Train.load_railway_path_data(railway_path_data);
trains = [];
trains_info.forEach(t => {
var train = new Train(t);
if (train.legal) {
trains.push(train);
}
});
mapboxgl.accessToken = 'pk.eyJ1IjoiYnJhbnpoYW5nIiwiYSI6ImNrM2U2dHh0ejE2YngzaXFlcjFvdG96b2EifQ.KSQQ0l2qGa1-nrEKn3YKPw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/branzhang/ck3e2bq8g1pj21cquzpiuhwmc',
center: [121.315625, 31.196169],
zoom: 16.5,
minZoom: 13,
maxZoom: 18
});
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(new PauseControl(), 'top-left');
// Set the map's max bounds.
map.setMaxBounds([
[121.115625, 30.996169], // [west, south]
[121.515625, 31.396169] // [east, north]
]);
// to store and cancel the animation
var animation;
var startTime = new Date(2019, 0, 1, 18, 3, 0);
var currentTime = startTime;
var endTime = new Date(2019, 0, 1, 18, 40, 0);
// 以 N 倍速播放
var speed = 5;
var resetTime = false;
var last_timestamp;
var timeDelay = 0;
var playing = true;
var pauseButton;
map.on('load', function() {
pauseButton = document.getElementById('pause');
var imageSource = {
'body': 'images/body.png',
'high_head': 'images/high_head.png',
'high_tail': 'images/high_tail.png',
'normal_head': 'images/normal_head.png',
'normal_tail': 'images/normal_tail.png',
}
for(var imageName in imageSource) {
(function(tmp) {
map.loadImage(imageSource[tmp], function (error, image) {
if (error) {
throw error;
}
map.addImage(tmp, image);
});
})(imageName);
}
map.addSource('train_source', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": []
}
});
map.addLayer({
'id': 'train_layer',
'type': 'symbol',
'source': 'train_source',
'layout': {
'icon-image': ["get", "train_type"],
'icon-allow-overlap': true,
'icon-ignore-placement': true,
'icon-rotation-alignment': 'map',
'icon-rotate': ["get", "icon_rotate"],
'icon-size': [
"interpolate",
["linear"],
["zoom"],
13, 0.2,
16, 0.5,
17, 0.9,
18.2, 2
],
},
'paint': {}
}, 'bridge-pedestrian-case');
animateTrain();
pauseButton.addEventListener('click', function() {
if (playing) {
cancelAnimationFrame(animation);
pauseButton.innerHTML = "Play<br>" + currentTime.toLocaleTimeString('en-US');
} else {
resetTime = true;
animateTrain();
}
playing = !playing;
});
function animateTrain(timestamp) {
if (timestamp !== undefined) {
if (resetTime) {
timeDelay = timeDelay + timestamp - last_timestamp;
resetTime = false;
}
timeChange = ((timestamp-timeDelay) * speed) % (endTime.getTime() - startTime.getTime());
currentTime = new Date(startTime.getTime() + timeChange);
last_timestamp = timestamp;
pauseButton.innerHTML = "Pause<br>" + currentTime.toLocaleTimeString('en-US');
}
var updatedFeatures = [];
trains.forEach(t => {
var tmp = t.getRealTimeLocation(currentTime);
updatedFeatures.push.apply(updatedFeatures, tmp);
});
map.getSource('train_source').setData({
"type": "FeatureCollection",
"features": updatedFeatures
});
if (currentTime >= endTime) {
console.log("one turn end.");
}
animation = requestAnimationFrame(animateTrain);
}
});
</script>
</body>
</html>