-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathDistortableImageOverlay.js
304 lines (251 loc) · 9.46 KB
/
DistortableImageOverlay.js
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
L.DistortableImageOverlay = L.ImageOverlay.extend({
options: {
alt: "",
height: 200,
crossOrigin: true,
// todo: find ideal number to prevent distortions during RotateScale, and make it dynamic (remove hardcoding)
edgeMinWidth: 520
},
initialize: function(url, options) {
this._toolArray = L.DistortableImage.EditToolbarDefaults;
this.edgeMinWidth = this.options.edgeMinWidth;
this._url = url;
this.rotation = 0;
// window.rotation = this.rotation;
L.DistortableImage._options = options;
L.setOptions(this, options);
},
onAdd: function(map) {
/* Copied from L.ImageOverlay */
this._map = map;
if (!this._image) { this._initImage(); }
if (!this._events) { this._initEvents(); }
map._panes.overlayPane.appendChild(this._image);
map.on("viewreset", this._reset, this);
/* End copied from L.ImageOverlay */
/* Use provided corners if available */
if (this.options.corners) {
this._corners = this.options.corners;
if (map.options.zoomAnimation && L.Browser.any3d) {
map.on("zoomanim", this._animateZoom, this);
}
/* This reset happens before image load; it allows
* us to place the image on the map earlier with
* "guessed" dimensions. */
this._reset();
}
/* Have to wait for the image to load because
* we need to access its width and height. */
L.DomEvent.on(this._image, "load", function() {
this._initImageDimensions();
this._reset();
/* Initialize default corners if not already set */
if (!this._corners) {
if (map.options.zoomAnimation && L.Browser.any3d) {
map.on("zoomanim", this._animateZoom, this);
}
}
}, this);
this.fire("add");
},
onRemove: function(map) {
this.fire("remove");
L.ImageOverlay.prototype.onRemove.call(this, map);
},
_initImage: function() {
L.ImageOverlay.prototype._initImage.call(this);
L.extend(this._image, {
alt: this.options.alt
});
},
_addTool: function(tool) {
this._toolArray.push(tool);
L.DistortableImage.EditToolbar = LeafletToolbar.Popup.extend({
options: {
actions: this._toolArray
}
});
},
_initImageDimensions: function() {
var map = this._map,
originalImageWidth = L.DomUtil.getStyle(this._image, "width"),
originalImageHeight = L.DomUtil.getStyle(this._image, "height"),
aspectRatio =
parseInt(originalImageWidth) / parseInt(originalImageHeight),
imageHeight = this.options.height,
imageWidth = parseInt(aspectRatio * imageHeight),
center = map.latLngToContainerPoint(map.getCenter()),
offset = L.point(imageWidth, imageHeight).divideBy(2);
if (this.options.corners) {
this._corners = this.options.corners;
} else {
this._corners = [
map.containerPointToLatLng(center.subtract(offset)),
map.containerPointToLatLng(
center.add(L.point(offset.x, -offset.y))
),
map.containerPointToLatLng(
center.add(L.point(-offset.x, offset.y))
),
map.containerPointToLatLng(center.add(offset))
];
}
this._initialDimensions = { 'height': imageHeight, 'width': imageWidth, 'offset': offset };
},
_initEvents: function() {
this._events = ["click"];
for (var i = 0, l = this._events.length; i < l; i++) {
L.DomEvent.on(this._image, this._events[i], this._fireMouseEvent, this);
}
},
/* See src/layer/vector/Path.SVG.js in the Leaflet source. */
_fireMouseEvent: function(event) {
if (!this.hasEventListeners(event.type)) { return; }
var map = this._map,
containerPoint = map.mouseEventToContainerPoint(event),
layerPoint = map.containerPointToLayerPoint(containerPoint),
latlng = map.layerPointToLatLng(layerPoint);
this.fire(event.type, {
latlng: latlng,
layerPoint: layerPoint,
containerPoint: containerPoint,
originalEvent: event
});
},
_updateCorner: function(corner, latlng) {
this._corners[corner] = latlng;
this._reset();
},
// fires a reset after all corner positions are updated instead of after each one (above). Use for translating
_updateCorners: function(latlngObj) {
var i = 0;
for (var k in latlngObj) {
this._corners[i] = latlngObj[k];
i += 1;
}
this._reset();
},
_updateCornersFromPoints: function(pointsObj) {
var map = this._map;
var i = 0;
for (var k in pointsObj) {
this._corners[i] = map.layerPointToLatLng(pointsObj[k]);
i += 1;
}
this._reset();
},
/* Copied from Leaflet v0.7 https://github.com/Leaflet/Leaflet/blob/66282f14bcb180ec87d9818d9f3c9f75afd01b30/src/dom/DomUtil.js#L189-L199 */
/* since L.DomUtil.getTranslateString() is deprecated in Leaflet v1.0 */
_getTranslateString: function(point) {
// on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
// makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
// (same speed either way), Opera 12 doesn't support translate3d
var is3d = L.Browser.webkit3d,
open = "translate" + (is3d ? "3d" : "") + "(",
close = (is3d ? ",0" : "") + ")";
return open + point.x + "px," + point.y + "px" + close;
},
_reset: function() {
var map = this._map,
image = this._image,
latLngToLayerPoint = L.bind(map.latLngToLayerPoint, map),
transformMatrix = this._calculateProjectiveTransform(latLngToLayerPoint),
topLeft = latLngToLayerPoint(this._corners[0]),
warp = L.DomUtil.getMatrixString(transformMatrix),
translation = this._getTranslateString(topLeft);
/* See L.DomUtil.setPosition. Mainly for the purposes of L.Draggable. */
image._leaflet_pos = topLeft;
image.style[L.DomUtil.TRANSFORM] = [translation, warp].join(" ");
/* Set origin to the upper-left corner rather than the center of the image, which is the default. */
image.style[L.DomUtil.TRANSFORM + "-origin"] = "0 0 0";
},
/*
* Calculates the transform string that will be correct *at the end* of zooming.
* Leaflet then generates a CSS3 animation between the current transform and
* future transform which makes the transition appear smooth.
*/
_animateZoom: function(event) {
var map = this._map,
image = this._image,
latLngToNewLayerPoint = function(latlng) {
return map._latLngToNewLayerPoint(latlng, event.zoom, event.center);
},
transformMatrix = this._calculateProjectiveTransform(
latLngToNewLayerPoint
),
topLeft = latLngToNewLayerPoint(this._corners[0]),
warp = L.DomUtil.getMatrixString(transformMatrix),
translation = this._getTranslateString(topLeft);
/* See L.DomUtil.setPosition. Mainly for the purposes of L.Draggable. */
image._leaflet_pos = topLeft;
if (!L.Browser.gecko) {
image.style[L.DomUtil.TRANSFORM] = [translation, warp].join(" ");
}
},
getCorners: function() {
return this._corners;
},
getCorner: function(i) {
return this._corners[i];
},
/*
* Calculates the centroid of the image.
* See http://stackoverflow.com/questions/6149175/logical-question-given-corners-find-center-of-quadrilateral
*/
getCenter: function(ll2c, c2ll) {
var map = this._map,
latLngToCartesian = ll2c ? ll2c : map.latLngToLayerPoint,
cartesianToLatLng = c2ll ? c2ll : map.layerPointToLatLng,
nw = latLngToCartesian.call(map, this._corners[0]),
ne = latLngToCartesian.call(map, this._corners[1]),
se = latLngToCartesian.call(map, this._corners[2]),
sw = latLngToCartesian.call(map, this._corners[3]),
nmid = nw.add(ne.subtract(nw).divideBy(2)),
smid = sw.add(se.subtract(sw).divideBy(2));
return cartesianToLatLng.call(
map,
nmid.add(smid.subtract(nmid).divideBy(2))
);
},
// Use for translation calculations - for translation the delta for 1 corner applies to all 4
_calcCornerPointDelta: function() {
return this._dragStartPoints[0].subtract(this._dragPoints[0]);
},
_calcCenterTwoCornerPoints: function(topLeft, topRight) {
var toolPoint = { x: "", y: "" };
toolPoint.x = topRight.x + (topLeft.x - topRight.x) / 2;
toolPoint.y = topRight.y + (topLeft.y - topRight.y) / 2;
return toolPoint;
},
_calculateProjectiveTransform: function(latLngToCartesian) {
/* Setting reasonable but made-up image defaults
* allow us to place images on the map before
* they've finished downloading. */
var offset = latLngToCartesian(this._corners[0]),
w = this._image.offsetWidth || 500,
h = this._image.offsetHeight || 375,
c = [],
j;
/* Convert corners to container points (i.e. cartesian coordinates). */
for (j = 0; j < this._corners.length; j++) {
c.push(latLngToCartesian(this._corners[j])._subtract(offset));
}
/*
* This matrix describes the action of the CSS transform on each corner of the image.
* It maps from the coordinate system centered at the upper left corner of the image
* to the region bounded by the latlngs in this._corners.
* For example:
* 0, 0, c[0].x, c[0].y
* says that the upper-left corner of the image maps to the first latlng in this._corners.
*/
return L.MatrixUtil.general2DProjection(
0, 0, c[0].x, c[0].y,
w, 0, c[1].x, c[1].y,
0, h, c[2].x, c[2].y,
w, h, c[3].x, c[3].y
);
}
});
L.distortableImageOverlay = function(id, options) {
return new L.DistortableImageOverlay(id, options);
};