Skip to content

Commit

Permalink
Merge pull request #249 from atombender/master
Browse files Browse the repository at this point in the history
Improve polyline/polygon drawing by accepting some motion on click
  • Loading branch information
jacobtoye committed Jan 13, 2014
2 parents b7c0318 + 8fa1432 commit fd30a0a
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/draw/handler/Draw.Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}

this._mouseMarker
.on('click', this._onClick, this)
.on('mousedown', this._onMouseDown, this)
.addTo(this._map);

this._map
.on('mousemove', this._onMouseMove, this)
.on('mouseup', this._onMouseUp, this)
.on('zoomend', this._onZoomEnd, this);
}
},
Expand All @@ -99,7 +100,9 @@ L.Draw.Polyline = L.Draw.Feature.extend({
this._map.removeLayer(this._poly);
delete this._poly;

this._mouseMarker.off('click', this._onClick, this);
this._mouseMarker
.off('mousedown', this._onMouseDown, this)
.off('mouseup', this._onMouseUp, this);
this._map.removeLayer(this._mouseMarker);
delete this._mouseMarker;

Expand Down Expand Up @@ -195,12 +198,6 @@ L.Draw.Polyline = L.Draw.Feature.extend({
L.DomEvent.preventDefault(e.originalEvent);
},

_onClick: function (e) {
var latlng = e.target.getLatLng();

this.addVertex(latlng);
},

_vertexChanged: function (latlng, added) {
this._updateFinishHandler();

Expand All @@ -211,6 +208,24 @@ L.Draw.Polyline = L.Draw.Feature.extend({
this._updateTooltip();
},

_onMouseDown: function (e) {
var originalEvent = e.originalEvent;
this._mouseDownOrigin = L.point(originalEvent.clientX, originalEvent.clientY);
},

_onMouseUp: function (e) {
if (this._mouseDownOrigin) {
// We detect clicks within a certain tolerance, otherwise let it
// be interpreted as a drag by the map
var distance = L.point(e.originalEvent.clientX, e.originalEvent.clientY)
.distanceTo(this._mouseDownOrigin);
if (Math.abs(distance) < 9 * (window.devicePixelRatio || 1)) {
this.addVertex(e.latlng);
}
}
this._mouseDownOrigin = null;
},

_updateFinishHandler: function () {
var markerCount = this._markers.length;
// The last marker should have a click handler to close the polyline
Expand Down

0 comments on commit fd30a0a

Please sign in to comment.