-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex2.html
More file actions
538 lines (470 loc) · 15.6 KB
/
index2.html
File metadata and controls
538 lines (470 loc) · 15.6 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://www.kosmosnimki.ru/lib/geomixer_1.2/geomixer-src.js?key=E5FB6CCB5D23B5E119D2F1B26BCC57BD"></script>
<link href="http://www.kosmosnimki.ru/lib/geomixer_1.2/geomixer.css" rel="stylesheet" />
<!--
<script src="../src/L.ImageTransformWebGL.js"></script>
-->
<script src="./examples/tools/numeric-1.2.6.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec2 aVertCoord;
uniform mat4 uTransformMatrix;
varying vec2 vTextureCoord;
void main(void) {
vTextureCoord = aVertCoord;
gl_Position = uTransformMatrix * vec4(aVertCoord, 0.0, 1.0);
}
</script>
<style>
html, body, #map, #screenCanvas {
height: 100%;
width: 100%;
margin: 0px;
}
#controls {
position: absolute;
right: 20px;
bottom: 240px;
z-index: 2;
background-color: #ffffff;
border-radius: 5px;
box-shadow: 0 1px 7px rgba(0,0,0,0.65);
}
#controls button {
border-radius: 5px;
}
#controls > div{
padding: 6px;
}
#screenCanvas {
z-index: 1000;
position: absolute;
left: 0px;
top: 0px;
pointer-events: none;
opacity: 1;
}
#imageCont {
position: absolute;
bottom: 25px;
right: 5px;
z-index: 1;
background-color: #ffffff;
border-radius: 5px;
box-shadow: 0 1px 7px rgba(0,0,0,0.65);
}
#image {
width: 200px;
height: 200px;
padding: 5px;
padding-bottom: 0px;
cursor: move;
}
#controlHandles {
position: absolute;
left: 0px;
pointer-events: none;
}
</style>
<title>GeoMixer API - примеры подключения оперативных данных по пожарам</title>
</head>
<body>
<div id="map"></div>
<canvas id="screenCanvas"></canvas>
<script>
L.Icon.Default.imagePath = 'http://www.kosmosnimki.ru/lib/geomixer_1.2/images/';
var map = new L.Map('map', {
layers: [],
crs: L.CRS.EPSG3395,
center: new L.LatLng(56, 137.23),
attributionControl: false,
zoomControl: false,
zoom: 9
});
map
.addControl(L.control.gmxZoom()) // default options = {zoomslider: true}
.addControl(L.control.gmxBottom())
.addControl(L.control.gmxLocation()) // default options = {position: 'bottomright'}
.addControl(L.control.gmxCopyright()) // default options = {position: 'bottomleft'}
.addControl(L.control.gmxHide())
.addControl(L.control.gmxCenter()) // default options = {color: '#216b9c'}
.addControl(new L.Control.gmxIcon({
id: 'boxzoom',
toggle: true,
title: L.gmxLocale.addText({
'eng': {
'boxZoom': 'BoxZoom'
},
'rus': {
'boxZoom': 'Увеличение'
}
}).getText('boxZoom'),
onAdd: function (control) {
//console.log('onAdd', this, arguments);
var map = control._map,
_onMouseDown = map.boxZoom._onMouseDown;
map.boxZoom._onMouseDown = function (e) {
_onMouseDown.call(map.boxZoom, {
clientX: e.clientX,
clientY: e.clientY,
which: 1,
shiftKey: true
});
}
map.on('boxzoomend', function () {
map.dragging.enable();
map.boxZoom.removeHooks();
control.setActive(false);
});
},
stateChange: function (control) {
console.log('boxzoom', control.options.isActive);
var map = control._map;
if (control.options.isActive) {
map.dragging.disable();
map.boxZoom.addHooks();
} else {
map.dragging.enable();
map.boxZoom.removeHooks();
}
}
}))
.addControl(new L.Control.gmxDrawing());
var LayersControl = L.control.layers({
Google: L.tileLayer('//mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}'),
Map: L.tileLayer.Mercator('http://vec03.maps.yandex.net/tiles?l=map&v=17.09.21-1&x={x}&y={y}&z={z}&scale=1&lang=ru_RU', {
maxZoom: 21,
maxNativeZoom: 17
}).addTo(map)
}).addTo(map);
// The normalised texture co-ordinates of the quad in the screen image.
var gl = null;
var gUtils = {
_map: map,
_img: new Image(),
_canvas: document.getElementById('screenCanvas'),
_imageAttr: null,
_glOpts: { antialias: true, depth: false, preserveDrawingBuffer: true },
_qualityOptions: {},
_anisoExt: null,
_glResources: null,
srcPoints: null,
controlPoints: null,
// controlPoints: [
// { x: 100, y: 100 },
// { x: 300, y: 100 },
// { x: 100, y: 400 },
// { x: 300, y: 400 }
// ],
// _anchors: [
// [60.55, 30.53],
// [60.56, 30.53],
// [60.55, 30.54],
// [60.56, 30.54]
// ],
// _anchors: [
// [56.344192, 136.59558],
// [56.344192, 137.8782],
// [55.613245, 137.8782],
// [55.613245, 136.59558]
// ],
// var clipCoords = [[56.301281, 136.90579],[56.150009, 137.83902],[55.639533, 137.53169],[55.788635, 136.60979]];
// { x: 0, y: 0 }, // top-left
// { x: w, y: 0 }, // top-right
// { x: 0, y: h }, // bottom-left
// { x: w, y: h } // bottom-right
_clipCoords: [
[56.301281, 136.90579],
[56.150009, 137.83902],
[55.788635, 136.60979],
[55.639533, 137.53169]
],
_anchors: [
[56.344192, 136.59558],
[56.344192, 137.8782],
[55.613245, 136.59558],
[55.613245, 137.8782]
],
createGL: function (canvas) {
var pbbox = map.getPixelBounds();
canvas.width = pbbox.max.x - pbbox.min.x;
canvas.height = pbbox.max.y - pbbox.min.y;
gUtils.syncQualityOptions();
gl =
canvas.getContext('webgl', gUtils._glOpts) ||
canvas.getContext('experimental-webgl', gUtils._glOpts);
if(!gl) {
console.log("Your browser doesn't seem to support WebGL.");
}
gUtils._anisoExt =
gl.getExtension('EXT_texture_filter_anisotropic') ||
gl.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic');
if(!gUtils._anisoExt) {
console.log("Your browser doesn't support anisotropic filtering. Ordinary MIP mapping will be used.");
}
gUtils._glResources = gUtils.setupGlContext();
//initPoints();
gUtils.setAnchors();
gUtils._refresh();
},
setAnchors: function () {
var w = gUtils._img.width,
h = gUtils._img.height,
pbbox = map.getPixelBounds(),
pixelOrigin = map.getPixelOrigin(),
center = pbbox.getBottomLeft();
center.x += (pbbox.max.x - pbbox.min.x - w)/2;
center.y -= (pbbox.max.y - pbbox.min.y + h)/2;
gUtils._imageAttr = {
screenBbox: pbbox
,naturalWidth: gUtils._img.naturalWidth
,naturalHeight: gUtils._img.naturalHeight
,width: w
,height: h
,points: gUtils._anchors.map(function(it) {
var marker = L.marker(it, {draggable: true});
marker.on('drag', gUtils._refresh).addTo(map);
return marker;
})
};
},
_refresh: function () {
console.log('_refresh');
gUtils.controlPoints = gUtils._imageAttr.points.map(function(it) {
var pos = gUtils._map.latLngToLayerPoint(it.getLatLng());
return gUtils._map.layerPointToContainerPoint(pos);
}.bind(gUtils));
if (!gUtils.srcPoints) gUtils.loadScreenTexture();
gUtils.redrawImg();
},
getShader: function (type, id) {
var source = document.getElementById(id).innerHTML;
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert("Ошибка компиляции шейдера: " + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
},
setupGlContext: function () {
// Store return values here
var vertexShader = gUtils.getShader(gl.VERTEX_SHADER, 'shader-vs');
var fragmentShader = gUtils.getShader(gl.FRAGMENT_SHADER, 'shader-fs');
var rv = {};
// Compile the program
rv.shaderProgram = gl.createProgram();
gl.attachShader(rv.shaderProgram, vertexShader);
gl.attachShader(rv.shaderProgram, fragmentShader);
gl.linkProgram(rv.shaderProgram);
if (!gl.getProgramParameter(rv.shaderProgram, gl.LINK_STATUS)) {
console.log('Shader linking failed.');
}
// Create a buffer to hold the vertices
rv.vertexBuffer = gl.createBuffer();
// Find and set up the uniforms and attributes
gl.useProgram(rv.shaderProgram);
rv.vertAttrib = gl.getAttribLocation(rv.shaderProgram, 'aVertCoord');
rv.transMatUniform = gl.getUniformLocation(rv.shaderProgram, 'uTransformMatrix');
rv.samplerUniform = gl.getUniformLocation(rv.shaderProgram, 'uSampler');
// Create a texture to use for the screen image
rv.screenTexture = gl.createTexture();
return rv;
},
loadScreenTexture: function () {
if(!gl || !gUtils._glResources) { return; }
var image = gUtils._img;
var extent = { w: image.naturalWidth, h: image.naturalHeight };
gl.bindTexture(gl.TEXTURE_2D, gUtils._glResources.screenTexture);
// Flip the image's Y axis to match the WebGL texture coordinate space.
//gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
// Scale up the texture to the next highest power of two dimensions.
var canvas = document.createElement("canvas");
canvas.width = gUtils.nextHighestPowerOfTwo(extent.w);
canvas.height = gUtils.nextHighestPowerOfTwo(extent.h);
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0, extent.w, extent.h);
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
if(gUtils._qualityOptions.linearFiltering) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gUtils._qualityOptions.mipMapping
? gl.LINEAR_MIPMAP_LINEAR
: gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
} else {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gUtils._qualityOptions.mipMapping
? gl.NEAREST_MIPMAP_NEAREST
: gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
if(gUtils._anisoExt) {
// turn the anisotropy knob all the way to 11 (or down to 1 if it is
// switched off).
var maxAniso = gUtils._qualityOptions.anisotropicFiltering ?
gl.getParameter(gUtils._anisoExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1;
gl.texParameterf(gl.TEXTURE_2D, gUtils._anisoExt.TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
}
if(gUtils._qualityOptions.mipMapping) {
gl.generateMipmap(gl.TEXTURE_2D);
}
gl.bindTexture(gl.TEXTURE_2D, null);
// Record normalised height and width.
var w = extent.w / gUtils.nextHighestPowerOfTwo(extent.w), h = extent.h / gUtils.nextHighestPowerOfTwo(extent.h);
gUtils.srcPoints = [
{ x: 0, y: 0 }, // top-left
{ x: w, y: 0 }, // top-right
{ x: 0, y: h }, // bottom-left
{ x: w, y: h } // bottom-right
];
var pixelOrigin = map.getPixelOrigin(),
wh = L.point([w, h]);
// gUtils.srcPoints = gUtils._clipCoords.map(function(it) {
// var p = map.project(it).subtract(pixelOrigin),
// wh = L.point([gUtils.nextHighestPowerOfTwo(p.x), gUtils.nextHighestPowerOfTwo(p.y)]);
// p = p.unscaleBy(wh);
// return p;
// });
// setup the vertex buffer with the source points
var vertices = [];
for(var i=0, len = gUtils.srcPoints.length; i<len; i++) {
vertices.push(gUtils.srcPoints[i].x);
vertices.push(gUtils.srcPoints[i].y);
}
gl.bindBuffer(gl.ARRAY_BUFFER, gUtils._glResources.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// Redraw the image
gUtils.redrawImg();
},
nextHighestPowerOfTwo: function (x) {
--x;
for (var i = 1; i < 32; i <<= 1) {
x = x | x >> i;
}
return x + 1;
},
redrawImg: function () {
if(!gl || !gUtils._glResources || !gUtils.srcPoints) { return; }
var vpW = gUtils._canvas.width;
var vpH = gUtils._canvas.height;
// Find where the control points are in 'window coordinates'. I.e.
// where thecanvas covers [-1,1] x [-1,1]. Note that we have to flip
// the y-coord.
var dstPoints = [];
for(var i = 0, len = gUtils.controlPoints.length; i < len; i++) {
dstPoints.push({
x: (2 * gUtils.controlPoints[i].x / vpW) - 1,
// y: (2 * gUtils.controlPoints[i].y / vpH) - 1
y: -(2 * gUtils.controlPoints[i].y / vpH) + 1
});
}
// Get the transform
var v = gUtils.transformationFromQuadCorners(gUtils.srcPoints, dstPoints);
// set background to full transparency
gl.clearColor(0,0,0,0);
gl.viewport(0, 0, vpW, vpH);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(gUtils._glResources.shaderProgram);
// draw the triangles
gl.bindBuffer(gl.ARRAY_BUFFER, gUtils._glResources.vertexBuffer);
gl.enableVertexAttribArray(gUtils._glResources.vertAttrib);
gl.vertexAttribPointer(gUtils._glResources.vertAttrib, 2, gl.FLOAT, false, 0, 0);
/* If 'v' is the vector of transform coefficients, we want to use
the following matrix:
[v[0], v[3], 0, v[6]],
[v[1], v[4], 0, v[7]],
[ 0, 0, 1, 0],
[v[2], v[5], 0, 1]
which must be unravelled and sent to uniformMatrix4fv() in *column-major*
order. Hence the mystical ordering of the array below.
gl.uniformMatrix4fv(
gUtils._glResources.transMatUniform,
false, [
v[0], v[1], 0, v[2],
v[3], v[4], 0, v[5],
0, 0, 0, 0,
v[6], v[7], 0, 1
]);
*/
gl.uniformMatrix4fv(
gUtils._glResources.transMatUniform,
false, [
v[0], v[1], 0, v[2],
v[3], v[4], 0, v[5],
0, 0, 0, 0,
v[6], v[7], 0, 1
]);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, gUtils._glResources.screenTexture);
gl.uniform1i(gUtils._glResources.samplerUniform, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
},
transformationFromQuadCorners: function (before, after) {
/*
Return the 8 elements of the transformation matrix which maps
the points in *before* to corresponding ones in *after*. The
points should be specified as
[{x:x1,y:y1}, {x:x2,y:y2}, {x:x3,y:y2}, {x:x4,y:y4}].
Note: There are 8 elements because the bottom-right element is
assumed to be '1'.
*/
var b = numeric.transpose([[
after[0].x, after[0].y,
after[1].x, after[1].y,
after[2].x, after[2].y,
after[3].x, after[3].y ]]);
var A = [];
for(var i=0; i<before.length; i++) {
A.push([
before[i].x, 0, -after[i].x*before[i].x,
before[i].y, 0, -after[i].x*before[i].y, 1, 0]);
A.push([
0, before[i].x, -after[i].y*before[i].x,
0, before[i].y, -after[i].y*before[i].y, 0, 1]);
}
// Solve for T and return the elements as a single array
return numeric.transpose(numeric.dot(numeric.inv(A), b))[0];
},
syncQualityOptions: function () {
gUtils._qualityOptions.anisotropicFiltering =
gUtils._qualityOptions.mipMapping =
gUtils._qualityOptions.linearFiltering = true;
// re-load the texture if possible
gUtils.loadScreenTexture();
}
};
gUtils._img.onload = function(ev) {
// imageAttr = {
// naturalWidth: image.naturalWidth
// ,naturalHeight: image.naturalHeight
// ,width: image.width
// ,height: image.height
// ,points: []
// };
gUtils.createGL(gUtils._canvas);
gUtils._map
.on('move', function(ev) {
gUtils._refresh();
});
// console.log('image', imageAttr);
}
gUtils._img.crossOrigin = '';
gUtils._img.src = './examples/img/image.jpg';
</script>
</body>
</html>