forked from Rycieos/factorio-leaflet-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
239 lines (205 loc) · 5.91 KB
/
example.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
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"
integrity="sha384-BF7C732iE6WuqJMhUnTNJJLVvW1TIP87P2nMDY7aN2j2EJFWIaqK89j3WlirhFZU"
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"
integrity="sha384-jV/9OyZzm1ZMHG4pfYVMmnnCtVUlZdWgZ8mNSX3CPFM4HSK2KUSP8xzLljzMDkWd"
crossorigin=""></script>
<style>
* {
margin: 0;
padding: 0;
font: inherit;
}
body {
border: 0;
overflow: hidden;
}
.container {
position: relative;
}
/* Leaflet modifications */
.leaflet-container {
background-color: #000;
}
.leaflet-control-attribution {
visibility: hidden;
}
#mapid {
height: 100%;
width: 100%;
}
.slide-container {
bottom: 3em;
--min: 0;
--track-w: 30em;
--uu: calc(var(--track-w) - 1em)/(var(--max) - var(--min));
display: grid;
align-content: center;
float: none;
margin: 1em auto;
width: var(--track-w);
max-width: 50%;
height: 0.5em;
opacity: 0.3; /* Set transparency (for mouse-over effects on hover) */
font: 1em/ 2 ubuntu mono, consolas, monaco, monospace;
cursor: pointer;
-webkit-transition: .5s; /* 0.2 seconds transition on hover */
transition: opacity .5s;
}
.slider {
-webkit-appearance: none; /* Override default CSS styles */
appearance: none;
width: 100%;
min-height: 1em;
background: linear-gradient(#5b5b5b, #828282 70%, #888);
border-radius: 0.5em;
outline: none;
}
.slider::-webkit-slider-thumb,
.slider::-ms-thumb,
.slider::-moz-range-thumb {
border-radius: 50%;
width: 15px;
height: 15px;
}
#layer-label {
--pos: calc((var(--layer-selector) - var(--min))*var(--uu));
position: absolute;
bottom: 100%;
left: 0.5em;
transform: translate(calc(var(--pos) - 50%));
filter: drop-shadow(0 2px 3px #afb0b4);
opacity: 0;
margin-bottom: .375em;
padding: 0 .5em;
border-radius: .5em;
background: linear-gradient(#fdfdfd, #cdcdd0) border-box;
color: #027bc1;
filter: grayScale();
}
.slide-container:hover,
.slide-container:hover #layer-label {
opacity: 1;
}
.invisible {
visibility: hidden;
}
</style>
</head>
<body>
<div class="container">
<div id="mapid"></div>
<div class="slide-container leaflet-control">
<input id="layer-selector" type="range" min="0" step="1" class="slider" oninput="loadLayer(this.value)">
<output id="layer-label" for="layer-selector"></output>
</div>
</div>
<script>
var pathPrefix = "images/"
var dates_json_file = "dates.json"
function HttpGet(url){
let Httpreq = new XMLHttpRequest();
Httpreq.open("GET", url, false);
Httpreq.send(null);
return Httpreq.responseText;
}
addEventListener('input', e => {
let _t = e.target,
_p = _t.parentNode;
_p.style.setProperty(`--${_t.id}`, +_t.value)
}, false);
// Adapted from: https://gist.github.com/barryhunter/e42f0c4756e34d5d07db4a170c7ec680
L.TileLayer2 = L.TileLayer.extend({
_refreshTileUrl: function(tile, url) {
//use a image in background, so that only replace the actual tile, once image is loaded in cache!
let img = new Image();
img.onload = function() {
L.Util.requestAnimFrame(function() {
tile.el.src = url;
L.DomUtil.removeClass(tile.el, "invisible");
});
}
img.onerror = function() {
L.Util.requestAnimFrame(function() {
L.DomUtil.addClass(tile.el, "invisible");
});
}
img.src = url;
},
refresh: function() {
//prevent _tileOnLoad/_tileReady re-triggering a opacity animation
var wasAnimated = this._map._fadeAnimated;
this._map._fadeAnimated = false;
for (var key in this._tiles) {
let tile = this._tiles[key];
if (tile.current && tile.active) {
let oldsrc = tile.el.src;
let newsrc = this.getTileUrl(tile.coords);
if (oldsrc != newsrc) {
this._refreshTileUrl(tile, newsrc);
}
}
}
if (wasAnimated)
setTimeout(function() { mymap._fadeAnimated = wasAnimated; }, 5000);
}
});
function loadLayer(id) {
label.value = dates[id];
current_layer_id = id
layer.setUrl(pathPrefix + dates[id] + "/{z}/{y}/{x}.jpg", true);
layer.refresh();
saveBoundary();
}
function loadBoundary() {
var qs = window.location.search;
if (qs.length > 1) {
qs = qs.substring(1);
qs = qs.split(',');
}
if (qs.length === 4) {
qs[0] = Number.parseFloat(qs[0]);
qs[1] = Number.parseFloat(qs[1]);
qs[2] = Number.parseInt(qs[2]);
qs[3] = Number.parseInt(qs[3]);
} else {
qs = [0, 0, 8, dates.length - 1] // Set default center and zoom here
}
return qs;
}
function saveBoundary() {
var center = mymap.getCenter();
var zoom = Number.parseInt(mymap.getZoom());
var url = '?' + center.lat.toPrecision(5) + ',' + center.lng.toPrecision(5) + ',' + zoom + ',' + current_layer_id;
window.history.replaceState({}, '', url);
}
var dates = JSON.parse(HttpGet(dates_json_file));
let slider = document.getElementById("layer-selector");
slider.max = dates.length - 1;
slider.parentNode.style.setProperty(`--max`, slider.max)
// If there is only one date in the array
if (slider.max == 0) {
slider.parentNode.style.display = "none";
}
let b = loadBoundary();
var current_layer_id = b[3];
slider.value = current_layer_id;
slider.parentNode.style.setProperty(`--layer-selector`, slider.value)
var label = document.getElementById("layer-label");
label.value = dates[current_layer_id];
var layer = new L.TileLayer2(pathPrefix + dates[current_layer_id] + "/{z}/{y}/{x}.jpg");
var mymap = L.map('mapid', {
crs: L.CRS.Simple,
maxZoom: 10,
minZoom: 1,
wheelPxPerZoomLevel: 500,
layers: [layer],
}).setView([b[0], b[1]], b[2]);
mymap.on('moveend', function (e) {
saveBoundary();
});
</script>
</body>
</html>