forked from mapbox/leaflet-image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaflet-image.js
More file actions
308 lines (266 loc) · 9.87 KB
/
Copy pathleaflet-image.js
File metadata and controls
308 lines (266 loc) · 9.87 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
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.leafletImage=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
var queue = _dereq_('./queue');
// leaflet-image
module.exports = function leafletImage(map, callback) {
var dimensions = map.getSize(),
layerQueue = new queue(1);
var canvas = document.createElement('canvas');
canvas.width = dimensions.x;
canvas.height = dimensions.y;
var ctx = canvas.getContext('2d');
// layers are drawn in the same order as they are composed in the DOM:
// tiles, paths, and then markers
map.eachLayer(drawTileLayer);
if (map._pathRoot) layerQueue.defer(handlePathRoot, map._pathRoot);
map.eachLayer(drawMarkerLayer);
layerQueue.awaitAll(layersDone);
function drawTileLayer(l) {
if (l.wmsParams || l._tiles) layerQueue.defer(handleTileLayer, l);
}
function drawMarkerLayer(l) {
if (l instanceof L.Marker && l.options.icon instanceof L.Icon) {
layerQueue.defer(handleMarkerLayer, l);
}
else if (l instanceof L.Marker && l.options.icon._icon.className.indexOf("marker-cluster") !== -1) {
layerQueue.defer(handleClusterLayer, l);
}
}
function done() {
callback(null, canvas);
}
function layersDone(err, layers) {
if (err) throw err;
layers.forEach(function(layer) {
if (layer && layer.canvas) {
ctx.drawImage(layer.canvas, 0, 0);
}
});
done();
}
function handleTileLayer(layer, callback) {
var canvas = document.createElement('canvas');
canvas.width = dimensions.x;
canvas.height = dimensions.y;
var ctx = canvas.getContext('2d'),
bounds = map.getPixelBounds(),
origin = map.getPixelOrigin(),
zoom = map.getZoom(),
tileSize = layer.options.tileSize;
if (zoom > layer.options.maxZoom ||
zoom < layer.options.minZoom) {
return callback();
}
var offset = new L.Point(
((origin.x / tileSize) - Math.floor(origin.x / tileSize)) * tileSize,
((origin.y / tileSize) - Math.floor(origin.y / tileSize)) * tileSize
);
var tileBounds = L.bounds(
bounds.min.divideBy(tileSize)._floor(),
bounds.max.divideBy(tileSize)._floor()),
tiles = [],
center = tileBounds.getCenter(),
j, i, point,
tileQueue = new queue(1);
for (j = tileBounds.min.y; j <= tileBounds.max.y; j++) {
for (i = tileBounds.min.x; i <= tileBounds.max.x; i++) {
tiles.push(new L.Point(i, j));
}
}
tiles.forEach(function(tilePoint) {
var originalTilePoint = tilePoint.clone();
layer._adjustTilePoint(tilePoint);
var tilePos = layer._getTilePos(originalTilePoint)
.subtract(bounds.min)
.add(origin);
if (tilePoint.y >= 0) {
var url = addCacheString(layer.getTileUrl(tilePoint));
tileQueue.defer(loadTile, url, tilePos, tileSize);
}
});
tileQueue.awaitAll(tileQueueFinish);
function loadTile(url, tilePos, tileSize, callback) {
var im = new Image();
im.crossOrigin = '';
im.onload = function() {
callback(null, {
img: this,
pos: tilePos,
size: tileSize
});
};
im.src = url;
}
function tileQueueFinish(err, data) {
data.forEach(drawTile);
callback(null, { canvas: canvas });
}
function drawTile(d) {
ctx.drawImage(d.img, Math.floor(d.pos.x), Math.floor(d.pos.y),
d.size, d.size);
}
}
function handlePathRoot(root, callback) {
var bounds = map.getPixelBounds(),
origin = map.getPixelOrigin(),
canvas = document.createElement('canvas');
canvas.width = dimensions.x;
canvas.height = dimensions.y;
var ctx = canvas.getContext('2d');
var pos = L.DomUtil.getPosition(root).subtract(bounds.min).add(origin);
ctx.drawImage(root, pos.x, pos.y);
callback(null, {
canvas: canvas
});
}
function handleMarkerLayer(marker, callback) {
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
pixelBounds = map.getPixelBounds(),
minPoint = new L.Point(pixelBounds.min.x, pixelBounds.min.y),
pixelPoint = map.project(marker.getLatLng()),
url = addCacheString(marker._icon.src),
im = new Image(),
options = marker.options.icon.options;
var size = options.iconSize;
var pos = pixelPoint.subtract(minPoint);
var anchor = L.point(marker.feature.geometry.coordinates),
x = pos.x - size[0] / 2,
y = pos.y - size[1] / 2;
canvas.width = dimensions.x;
canvas.height = dimensions.y;
im.crossOrigin = '';
im.onload = function() {
ctx.drawImage(this, x, y, size[0], size[1]);
callback(null, {
canvas: canvas
});
};
im.src = url;
}
function handleClusterLayer(marker, callback) {
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
pixelBounds = map.getPixelBounds(),
minPoint = new L.Point(pixelBounds.min.x, pixelBounds.min.y);
var pixelPoint = map.project(marker.getLatLng());
var size = marker._icon.clientWidth;
var pos = pixelPoint.subtract(minPoint);
var anchor = marker.getLatLng(),
x = pos.x,
y = pos.y;
canvas.width = dimensions.x;
canvas.height = dimensions.y;
var classd = marker.options.icon._icon.className.split(" ")[2];
var ocolor, icolor;
if (classd === "marker-cluster-small") {
icolor = "rgba(181, 226, 140, 0.6)";
ocolor = "rgba(110, 204, 57, 0.6)";
}
else if (classd === "marker-cluster-medium") {
icolor = "rgba(241, 211, 87, 0.6)";
ocolor = "rgba(240, 194, 12, 0.6)";
}
else if (classd === "marker-cluster-large") {
icolor = "rgba(253, 156, 115, 0.6)";
ocolor = "rgba(241, 128, 23, 0.6)";
}
else {
icolor = "white";
ocolor = "white";
}
// outter circle
ctx.arc(x, y, (size-4) / 2, 0, Math.PI * 2, true);
ctx.fillStyle = icolor;
ctx.fill();
ctx.arc(x, y, (size-4) / 2, 0, Math.PI * 2, true);
ctx.strokeStyle = ocolor;
ctx.lineWidth = 5;
ctx.stroke();
var number = marker.options.icon._icon.innerText;
ctx.font = "12px Helvetica, Arial, sans-serif";
ctx.fillStyle = "black";
var len = ctx.measureText(number).width;
ctx.fillText(number, x - len/2 + 2, y + 6);
callback(null, {
canvas: canvas
});
}
function addCacheString(url) {
return url + ((url.match(/\?/)) ? '&' : '?') + 'cache=' + (+new Date());
}
};
},{"./queue":2}],2:[function(_dereq_,module,exports){
(function() {
if (typeof module === "undefined") self.queue = queue;
else module.exports = queue;
queue.version = "1.0.4";
var slice = [].slice;
function queue(parallelism) {
var q,
tasks = [],
started = 0, // number of tasks that have been started (and perhaps finished)
active = 0, // number of tasks currently being executed (started but not finished)
remaining = 0, // number of tasks not yet finished
popping, // inside a synchronous task callback?
error = null,
await = noop,
all;
if (!parallelism) parallelism = Infinity;
function pop() {
while (popping = started < tasks.length && active < parallelism) {
var i = started++,
t = tasks[i],
a = slice.call(t, 1);
a.push(callback(i));
++active;
t[0].apply(null, a);
}
}
function callback(i) {
return function(e, r) {
--active;
if (error != null) return;
if (e != null) {
error = e; // ignore new tasks and squelch active callbacks
started = remaining = NaN; // stop queued tasks from starting
notify();
} else {
tasks[i] = r;
if (--remaining) popping || pop();
else notify();
}
};
}
function notify() {
if (error != null) await(error);
else if (all) await(error, tasks);
else await.apply(null, [error].concat(tasks));
}
return q = {
defer: function() {
if (!error) {
tasks.push(arguments);
++remaining;
pop();
}
return q;
},
await: function(f) {
await = f;
all = false;
if (!remaining) notify();
return q;
},
awaitAll: function(f) {
await = f;
all = true;
if (!remaining) notify();
return q;
}
};
}
function noop() {}
})();
},{}]},{},[1])
(1)
});