Overlay dragging
ImageOverlay Dragging with L.Path.Drag
by freeze2000f
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.3/leaflet.css">
<script src="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js"></script>
<div id="map"></div>
CSS
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 100%;
}
JavaScript
//start of L.Path.Drag-src.js
/**
* Leaflet vector features drag functionality
* @author Alexander Milevski <info@w8r.name>
* @preserve
*/
/**
* Matrix transform path for SVG/VML
* Renderer-independent
*/
L.Path.include({
/**
* Applies matrix transformation to SVG
* @param {Array.<Number>?} matrix
*/
_transform: function(matrix) {
if (this._renderer) {
if (matrix) {
this._renderer.transformPath(this, matrix);
} else {
// reset transform matrix
this._renderer._resetTransformPath(this);
this._update();
}
}
return this;
},
/**
* Check if the feature was dragged, that'll supress the click event
* on mouseup. That fixes popups for example
*
* @param {MouseEvent} e
*/
_onMouseClick: function(e) {
if ((this.dragging && this.dragging.moved()) ||
(this._map.dragging && this._map.dragging.moved())) {
return;
}
this._fireMouseEvent(e);
}
});
/**
* Drag handler
* @class L.Path.Drag
* @extends {L.Handler}
*/
L.Handler.PathDrag = L.Handler.extend( /** @lends L.Path.Drag.prototype */ {
statics: {
DRAGGING_CLS: 'leaflet-path-draggable',
},
/**
* @param {L.Path} path
* @constructor
*/
initialize: function(path) {
/**
* @type {L.Path}
*/
this._path = path;
/**
* @type {Array.<Number>}
*/
this._matrix = null;
/**
* @type {L.Point}
*/
this._startPoint = null;
/**
* @type {L.Point}
*/
this._dragStartPoint = null;
/**
* @type {Boolean}
*/
this._mapDraggingWasEnabled = false;
},
/**
* Enable dragging
*/
addHooks: function() {
this._path.on('mousedown', this._onDragStart, this);
this._path.options.className = this._path.options.className ?
(this._path.options.className + ' ' + L.Handler.PathDrag.DRAGGING_CLS) :
L.Handler.PathDrag.DRAGGING_CLS;
if (this._path._path) {
...