-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmapbox-gl-grid-heatmap2.html
364 lines (335 loc) · 18 KB
/
mapbox-gl-grid-heatmap2.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<!doctype html>
<head>
<title>网格热图</title>
<script src="./lib/Tween.min.js" type="text/javascript"></script>
<script src="./lib/threebox.js" type="text/javascript"></script>
<script src='./lib/mapbox-gl.js' type="text/javascript"></script>
<link href='./lib/mapbox-gl.css' rel='stylesheet' />
<script src="./lib/jquery.js" type="text/javascript"></script>
<script src="./lib/lodash.js" type="text/javascript"></script>
<style>
body,
html {
width: 100%;
height: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
background: #000;
}
</style>
</head>
<body>
<div id='map' class='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibGltemdpc2VyIiwiYSI6ImNqZXFvemJlcjB1bWYyd2x0eGxjeGdvcXIifQ.gSsj63R-2VZV7L7mpSw0Uw';
let mapboxmap = new mapboxgl.Map({
container: 'map',
style: 'http://webres.cityfun.com.cn/szmap/szmap_dark/3857/map_dark.json',
center: [120.68271397300009, 31.297051986000042],
zoom: 12,
pitch: 60,
bearing: 80
});
let row = 201;
let column = 198;
mapboxmap.on('style.load', function () {
let tb;
mapboxmap.addLayer({
id: 'custom_layer',
type: 'custom',
onAdd: function (map, mbxContext) {
let timeInCount = 60;
let timeInterv = 3000;
let scale = 0.2;
let aniindexArr = new Array(8).fill({
endPointIndex: 0
})
this.map = map;
tb = new Threebox(
map,
mbxContext, {
defaultLights: true
}
);
let lineGroup = new THREE.Group();
tb.add(lineGroup);
jQuery.get(`./data/grid.txt`).then(res => {
let alllines = arrSplit(tb, res);
// 绘制行
let tween = null;
let lineMesh = null;
alllines.forEach((line, index) => {
lineMesh = drawLine(line);
lineMesh.userdata = { index: index }
lineGroup.add(lineMesh)
});
lineGroup.traverse(function (child) {
let userdata = child.userdata;
if (userdata) {
let lineIndex = userdata.index;
let tween1 = new TWEEN.Tween(aniindexArr[0])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[2] * scale;
let end = item[3] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[0].endPointIndex = 0;
})
let tween2 = new TWEEN.Tween(aniindexArr[1])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[3] * scale;;
let end = item[4] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[1].endPointIndex = 0;
})
let tween3 = new TWEEN.Tween(aniindexArr[2])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[4] * scale;;
let end = item[5] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[2].endPointIndex = 0;
})
let tween4 = new TWEEN.Tween(aniindexArr[3])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[5] * scale;;
let end = item[6] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[3].endPointIndex = 0;
})
let tween5 = new TWEEN.Tween(aniindexArr[4])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[6] * scale;
let end = item[7] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[4].endPointIndex = 0;
})
let tween6 = new TWEEN.Tween(aniindexArr[5])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[7] * scale;
let end = item[8] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[5].endPointIndex = 0;
})
let tween7 = new TWEEN.Tween(aniindexArr[6])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[8] * scale;
let end = item[9] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[6].endPointIndex = 0;
})
let tween9= new TWEEN.Tween(aniindexArr[6])
.to({ endPointIndex: timeInCount }, timeInterv)
.onUpdate(function (iii) {
let vertices = [];
let colors = [];
for (let i = 0; i < alllines[lineIndex].length; i++) {
let item = alllines[lineIndex][i];
let begain = item[9] * scale;
let end = item[0] * scale;
let h = begain + (end - begain) / timeInCount * iii.endPointIndex;
vertices.push(new THREE.Vector3(item[0], item[1], h))
colors.push(getColorByValue(h));
}
child.geometry.vertices = vertices;
child.geometry.colors = colors;
child.geometry.verticesNeedUpdate = true;
child.geometry.colorsNeedUpdate = true;
}).onComplete(function () {
aniindexArr[7].endPointIndex = 0;
})
tween1.chain(tween2)
tween2.chain(tween3)
tween3.chain(tween4);
tween4.chain(tween5)
tween5.chain(tween6)
tween6.chain(tween7)
tween7.chain(tween1)
tween1.start();
}
})
});
},
render: function (gl, matrix) {
if (this.map)
this.map.triggerRepaint();
if (tb)
tb.update();
TWEEN.update();
}
});
});
function arrSplit(tb, datStr) {
let resArr = dataFormat(tb, datStr);
let rows = _.chunk(resArr, row);
let colums = [];
let tmpColums = [];
for (let i = 0; i < row; i++) {
for (let j = 0; j < column; j++) {
let item = resArr[row * j + i]
tmpColums.push(item);
}
}
colums = _.chunk(tmpColums, column);
return [...rows, ...colums];
}
function drawLine(row) {
let vertices = [];
let colors = [];
let geometry = new THREE.Geometry();
row.forEach(coordinate => {
let [x, y, z] = [coordinate[0], coordinate[1], coordinate[2]];
vertices.push(new THREE.Vector3(x, y, z))
colors.push(getColorByValue(z))
});
let material = new THREE.LineBasicMaterial({
opacity: 1,
linewidth: 1,
vertexColors: THREE.VertexColors,
// blending: THREE.AdditiveBlending
});
geometry.vertices = vertices;
geometry.colors = colors;
let lineMesh = new THREE.Line(geometry, material);
lineMesh.geometry.verticesNeedUpdate = true;
lineMesh.geometry.colorsNeedUpdate = true;
return lineMesh;
}
function dataFormat(tb, dataStr) {
let points = [];
dataStr.split('\n').map(function (s, i) {
let splitArray = s.split(',');
var ll = [parseFloat(splitArray[0]), parseFloat(splitArray[1])];
let {
x,
y,
z
} = tb.projectToWorld(ll)
points.push([...[x, y], ...splitArray.splice(2).map(i => Number(i))])
});
return points;
}
let colors = [
new THREE.Color(`rgb( 113, 196, 54)`),
new THREE.Color(`rgb( 113, 196, 54)`),
new THREE.Color(`rgb( 171, 190, 52)`),
new THREE.Color(`rgb( 201, 155, 52)`),
new THREE.Color(`rgb( 205, 122, 45)`),
new THREE.Color(`rgb( 214, 96, 53)`),
new THREE.Color(`rgb( 234, 57, 45)`),
new THREE.Color(`rgb( 234, 57, 45)`)
]
function getColorByValue(value) {
let tvalue = Number(value)
if (tvalue < 3) {
return colors[0];
} else if (tvalue < 5) {
return colors[1];
} else if (tvalue <= 8) {
return colors[2];
} else if (tvalue < 10) {
return colors[3];
} else if (tvalue < 15) {
return colors[4];
} else if (tvalue < 30) {
return colors[5];
} else if (tvalue < 50) {
return colors[6];
} else {
return colors[7];
}
}
</script>
</body>