Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve polyline/polygon drawing by accepting some motion on click #249

Merged
merged 1 commit into from
Jan 13, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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