-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebgl-leaflet.html
298 lines (239 loc) · 7.53 KB
/
webgl-leaflet.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
<!DOCTYPE html PUBLIC>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css"></link>
<style>
#map {
width:100%;
height:100%;
}
#map a {
color:initial;
}
canvas {
opacity: 1
}
</style>
<!--[if lte IE 8]>
<link rel="stylesheet" href="leaflet/dist/leaflet.ie.css" />
<![endif]-->
<title>
Web GL Heatmap Leaflet Plugin
</title>
</head>
<body>
<div id="map"></div>
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script id="pointVertexShader" type="x-shader/x-vertex">
attribute vec4 worldCoord;
uniform mat4 mapMatrix;
void main() {
// transform world coordinate by matrix uniform variable
gl_Position = mapMatrix * worldCoord;
gl_PointSize = 10.;
}
</script>
<script id="pointFragmentShader" type="x-shader/x-fragment">
void main() {
// set pixels in points to green
gl_FragColor = vec4(.1, .8, 0.08, 1.);
}
</script>
<script type="text/javascript">
/*
* Add a new log message
*/
function addMessage(message, milliseconds) {
if (milliseconds === undefined) {
}
else {
message = message + ' [' + milliseconds.toString() + ' ms]';
}
console.log('message',message);
}
</script>
<script type="text/javascript">
var gl;
var pixelsToWebGLMatrix = new Float32Array(16);
var mapMatrix = new Float32Array(16);
var pointProgram;
var pointArrayBuffer;
var POINT_COUNT = 500000;
// Spherical Mercator projection Points Boundary (Thailand)
var MIN_X = 199.15235555555554;
var MAX_X = 199.84768;
var MIN_Y = 117.74998759079017;
var MAX_Y = 118.25784290489942;
var date1;
var date2;
L.TileLayer.WebGL = L.Class.extend({
initialize : function () {
this.data = [];
},
onAdd : function (map) {
var date1 = new Date().getTime();
this.map = map;
var mapsize = map.getSize();
var options = this.options;
var c = document.createElement("canvas");
c.id = 'webgl-leaflet';
c.width = mapsize.x;
c.height = mapsize.y;
c.style.position = 'absolute';
map.getPanes().overlayPane.appendChild(c);
// initialize WebGL
gl = c.getContext('experimental-webgl');
createShaderProgram();
loadData();
this.canvas = c;
map.on("move", this._plot, this);
/* hide layer on zoom, because it doesn't animate zoom */
map.on("zoomstart", this._hide, this);
map.on("zoomend", this._show, this);
map.on("resize", this._resize, this);
this._resize();
this._plot();
var date2 = new Date().getTime();
addMessage(POINT_COUNT + " Points Drawn", date2 - date1);
},
onRemove : function (map) {
map.getPanes().overlayPane.removeChild(this.canvas);
map.off("move", this._plot, this);
map.off("zoomstart", this._hide, this);
map.off("zoomend", this._show, this);
},
_hide : function () {
this.canvas.style.display = 'none';
},
_show : function () {
this.canvas.style.display = 'block';
},
_clear : function () {},
_resizeRequest : undefined,
_plot : function () {
// Set clear color
gl.clear(gl.COLOR_BUFFER_BIT);
// copy pixel->webgl matrix
mapMatrix.set(pixelsToWebGLMatrix);
// Scale to current zoom (worldCoords * 2^zoom)
var scale = Math.pow(2, map.getZoom());
scaleMatrix(mapMatrix, scale, scale);
//Change canvas top/left according to map bound
//Canvas is fixed to topleft during scrolling map
L.DomUtil.setPosition(this.canvas, map.latLngToLayerPoint(map.getBounds().getNorthWest()));
var offset = latLongToPixelXY( map.getBounds().getNorthWest().lat, map.getBounds().getNorthWest().lng);
//console.log('offset', offset);
translateMatrix(mapMatrix, -offset.x, -offset.y);
var matrixLoc = gl.getUniformLocation(pointProgram, 'mapMatrix');
gl.uniformMatrix4fv(matrixLoc, false, mapMatrix);
gl.drawArrays(gl.POINTS, 0, POINT_COUNT);
},
_resize : function () {
//helpful for maps that change sizes
var mapsize = this.map.getSize();
this.canvas.width = mapsize.x;
this.canvas.height = mapsize.y;
var width = this.canvas.width;
var height = this.canvas.height;
gl.viewport(0, 0, width, height);
// matrix which maps pixel coordinates to WebGL coordinates
pixelsToWebGLMatrix.set([2 / width, 0, 0, 0,
0, -2 / height, 0, 0,
0, 0, 0, 0,
-1, 1, 0, 1]);
},
addDataPoint : function (lat, lon) {
this.data.push([lat, lon]);
},
clearData : function () {
this.data = [];
},
update : function () {
this._plot();
}
});
L.TileLayer.webgl = function () {
return new L.TileLayer.WebGL();
};
// linear interpolate between a and b
function lerp(a, b, t) {
return a + t * (b - a);
}
function loadData() {
// this data could be loaded from anywhere, but in this case we'll
// generate some random x,y coords in a world coordinate bounding box
var rawData = new Float32Array(2 * POINT_COUNT);
for (var i = 0; i < rawData.length; i += 2) {
rawData[i] = lerp(MIN_X, MAX_X, Math.random());
rawData[i + 1] = lerp(MIN_Y, MAX_Y, Math.random());
}
pointArrayBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointArrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rawData, gl.STATIC_DRAW);
// enable the 'worldCoord' attribute in the shader to receive buffer
var attributeLoc = gl.getAttribLocation(pointProgram, 'worldCoord');
gl.enableVertexAttribArray(attributeLoc);
// tell webgl how buffer is laid out (pairs of x,y coords)
gl.vertexAttribPointer(attributeLoc, 2, gl.FLOAT, false, 0, 0);
}
function createShaderProgram() {
// create vertex shader
var vertexSrc = document.getElementById('pointVertexShader').text;
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexSrc);
gl.compileShader(vertexShader);
// create fragment shader
var fragmentSrc = document.getElementById('pointFragmentShader').text;
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentSrc);
gl.compileShader(fragmentShader);
// link shaders to create our program
pointProgram = gl.createProgram();
gl.attachShader(pointProgram, vertexShader);
gl.attachShader(pointProgram, fragmentShader);
gl.linkProgram(pointProgram);
gl.useProgram(pointProgram);
}
function scaleMatrix(matrix, scaleX, scaleY) {
// scaling x and y, which is just scaling first two columns of matrix
matrix[0] *= scaleX;
matrix[1] *= scaleX;
matrix[2] *= scaleX;
matrix[3] *= scaleX;
matrix[4] *= scaleY;
matrix[5] *= scaleY;
matrix[6] *= scaleY;
matrix[7] *= scaleY;
}
function translateMatrix(matrix, tx, ty) {
// translation is in last column of matrix
matrix[12] += matrix[0] * tx + matrix[4] * ty;
matrix[13] += matrix[1] * tx + matrix[5] * ty;
matrix[14] += matrix[2] * tx + matrix[6] * ty;
matrix[15] += matrix[3] * tx + matrix[7] * ty;
}
function latLongToPixelXY(latitude, longitude) {
var pi_180 = Math.PI / 180.0;
var pi_4 = Math.PI * 4;
var sinLatitude = Math.sin(latitude * pi_180);
var pixelY = (0.5 - Math.log((1 + sinLatitude) /
(1 - sinLatitude)) / (pi_4)) * 256;
var pixelX = ((longitude + 180) / 360) * 256;
var pixel = {
x : pixelX,
y : pixelY
};
return pixel;
}
</script>
<script type="text/javascript">
var map = new L.Map("map", {center: [13.743533,100.544082], zoom: 14})
.addLayer(new L.TileLayer("https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}"));
L.control.scale().addTo(map);
//custom size for this example, and autoresize because map style has a percentage width
var webGLLayer = new L.TileLayer.WebGL();
map.addLayer(webGLLayer);
</script>
</body>
</html>