-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
296 lines (245 loc) · 7.59 KB
/
index.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
<html>
<header>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<style>
body, html {with: 100%; height:100%; margin:0;}
#mapid { height: 100%; width:100%;}
</style>
</header>
<body>
<div id="mapid"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script>
let bounds = [[0,0],[-(1<<17),1<<17]]
let maxZoom = 0;
let minZoom = -10;
let showDebug = false;
var l0 = L.tileLayer('https://vs43.ailove.ru/test_line-art/_map/faces_2/{z}-{y}-{x}.jpg', {
maxZoom,
minZoom,
bounds,
attribution: '<a href="https://github.com/NVlabs/ffhq-dataset">ffhq-dataset</a>',
id: 'artline',
tileSize: 256,
zoomOffset: 10,
zindex:100,
// bounds: bounds,
})
var l1 = L.tileLayer('https://vs43.ailove.ru/test_line-art/_map/artline/{z}-{y}-{x}.jpg', {
maxZoom,
minZoom,
bounds,
id: 'artline',
attribution: '<a href="https://github.com/vijishmadhavan/ArtLine">vijishmadhavan/ArtLine</a>',
tileSize: 256,
zoomOffset: 10,
zindex: 100,
// bounds: bounds,
})
var l2 = L.tileLayer('https://vs43.ailove.ru/test_line-art/_map/u2net/{z}-{y}-{x}.jpg', {
maxZoom,
minZoom,
bounds,
attribution: '<a href="https://github.com/NathanUA/U-2-Net">NathanUA/U-2-Net</a>',
id:'u2net',
tileSize: 256,
zoomOffset: 10,
zindex:100,
// bounds: bounds,
})
var l3 = L.tileLayer('https://vs43.ailove.ru/test_line-art/_map/u2net-denoise/{z}-{y}-{x}.jpg', {
maxZoom,
minZoom,
bounds,
attribution: '<a href="https://github.com/NathanUA/U-2-Net">NathanUA/U-2-Net</a>',
id:'u2net',
tileSize: 256,
zoomOffset: 10,
zindex:100,
// bounds: bounds,
})
var currentLayer = l2;
L.Map.BoxSelector = L.Map.BoxZoom.extend({
_onMouseDown: function (e) {
if (!(e.shiftKey || e.altKey || e.ctrlKey ) || ((e.which !== 1) && (e.button !== 1))) { return false; }
this._map.dragging.disable();
this._clearDeferredResetState();
this._resetState();
L.DomUtil.disableTextSelection();
L.DomUtil.disableImageDrag();
this._startPoint = this._map.mouseEventToContainerPoint(e);
L.DomEvent.on(document, {
contextmenu: L.DomEvent.stop,
mousemove: this._onMouseMove,
mouseup: this._onMouseUp,
keydown: this._onKeyDown
}, this);
},
_onMouseUp: function (e) {
if ((e.which !== 1) && (e.button !== 1)) { return; }
this._map.dragging.enable();
this._finish();
if (!this._moved) { return; }
// Postpone to next JS tick so internal click event handling
// still see it as "moved".
this._clearDeferredResetState();
this._resetStateTimeout = setTimeout(L.Util.bind(this._resetState, this), 0);
var bounds = new L.LatLngBounds(
this._map.containerPointToLatLng(this._startPoint),
this._map.containerPointToLatLng(this._point));
this._map
// .fitBounds(bounds)
.fire('boxselectend', {boxSelectBounds: bounds, shift: e.shiftKey, alt: e.altKey, ctrl: e.ctrlKey});
},
});
L.Map.mergeOptions({boxZoom: false});
L.Map.mergeOptions({boxSelector: true});
L.Map.addInitHook('addHandler', 'boxSelector', L.Map.BoxSelector);
var layers = [l0, l1, l2, l3];
// Создаем карту
var map = L.map(
'mapid',
{
crs: L.CRS.Simple,
minZoom: -9,
maxZoom: 0,
bounds,
layers:[l2],
}
)
map.fitBounds([[-512,512],[-1024,1024]]);
// Канвас маски
var s_cvs = L.DomUtil.create('canvas', 'leaflet-tile');
s_cvs.width = 512;
s_cvs.height = 512;
var s_ctx = s_cvs.getContext('2d');
function selectTiles(bounds, color) {
let w,n,e,s;
w = bounds.getWest()
n =-bounds.getNorth()
e = bounds.getEast()
s =-bounds.getSouth()
//console.log(w,n,e,s)
w = Math.floor(w/256.0)
n = Math.floor(n/256.0)
e = Math.ceil (e/256.0)
s = Math.ceil (s/256.0)
console.log(w,n,e,s)
console.log(e, n, e-w, s-n);
// Очищаем регион
s_ctx.beginPath();
s_ctx.fillStyle = "rgba(0,0,0,1)";
s_ctx.globalCompositeOperation = "destination-out";
s_ctx.rect(e, n, w-e, s-n);
s_ctx.closePath();
s_ctx.fill();
// Заполняем цветом
s_ctx.beginPath();
s_ctx.globalCompositeOperation = "source-over";
s_ctx.fillStyle = color;
s_ctx.rect(e, n, w-e, s-n);
s_ctx.closePath();
s_ctx.fill();
tiles.redraw();
}
// Отрисовка выбора
var tiles = new L.GridLayer({
zindex: 200,
minZoom,
bounds,
});
tiles.createTile = function(coords) {
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
var ctx = tile.getContext('2d');
var size = this.getTileSize()
tile.width = size.x
tile.height = size.y
// calculate projection coordinates of top left tile pixel
var nwPoint = coords.scaleBy(size)
// calculate geographic coordinates of top left tile pixel
var nw = map.unproject(nwPoint, coords.z)
ctx.imageSmoothingEnabled = false;
let s = 1 << -coords.z;
ctx.drawImage(s_cvs,
nw.lng/256, -nw.lat/256, s, s,
0, 0, 256, 256);
if (showDebug) {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, size.x, 50);
ctx.fillStyle = 'black';
ctx.fillText('x: ' + coords.x + ', y: ' + coords.y + ', zoom: ' + coords.z, 20, 20);
ctx.fillText('lat: ' + nw.lat + ', lon: ' + nw.lng, 20, 40);
ctx.strokeStyle = 'gray';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(size.x-1, 0);
ctx.lineTo(size.x-1, size.y-1);
// ctx.lineTo(0, size.y-1);
// ctx.closePath();
ctx.stroke();
}
return tile;
}
tiles.addTo(map);
L.control.layers({
"Originals": l0,
"ArtLine": l1,
"U2Net": l2,
"U2Net denoised": l3,
}).addTo(map);
// Превьюха
let Preview = L.Control.extend({
_container: null,
options: {
position: 'bottomleft'
},
onAdd: function (map) {
let img = L.DomUtil.create('img', 'preview')
this._img = img
return img
},
updateHTML: function(x, y) {
let url = currentLayer._url;
let layer = url.split('/')[5];
this._img.src = `https://vs43.ailove.ru/test_line-art/_map/${layer}/${10}-${y}-${x}.jpg`;
}
});
let preview = new Preview();
map.addControl(preview);
map.addEventListener('mousemove', (event) => {
let {lat, lng} = event.latlng;
x= lng/256|0
y=-lat/256|0
preview.updateHTML(x, y);
}
);
map.addEventListener('boxselectend', (e) => {
let color = e.shift ? "rgba(255,0,0,0.25)" :
e.ctrl ? "rgba(0,255,0,0.25)" :
e.alt ? "rgba(0, 0,0,0.0)" : null;
selectTiles(e.boxSelectBounds, color);
});
// Клавиатура
window.addEventListener("keypress", function(e) {
var pressedKey = Number(e.key);
// If button pressed is not a number, return.
if (isNaN(pressedKey)) return;
console.log(pressedKey)
// If the pressed number is greater than the length of layers, return.
if (pressedKey - 1 >= layers.length) return;
// If currentLayer is set, remove it from the map.
if (currentLayer) map.removeLayer(currentLayer);
// map.eachLayer(function (layer) {
// map.removeLayer(layer);
// })
currentLayer = layers[pressedKey - 1];
map.addLayer(currentLayer);
currentLayer.setZIndex(100);
tiles.setZIndex(200);
});
</script>
</html>