Add click tolerance to allow dragging while drawing

This commit is contained in:
Tim Schaub
2013-11-11 14:35:47 -07:00
parent 2f07433593
commit 62b44f3c73
+26 -8
View File
@@ -78,6 +78,15 @@ ol.interaction.Draw = function(options) {
*/ */
this.sketchPoint_ = null; this.sketchPoint_ = null;
/**
* Squared tolerance for handling click events. If the squared distance
* between a down and click event is greater than this tolerance, click events
* will not be handled.
* @type {number}
* @private
*/
this.squaredClickTolerance_ = 4;
}; };
goog.inherits(ol.interaction.Draw, ol.interaction.Interaction); goog.inherits(ol.interaction.Draw, ol.interaction.Interaction);
@@ -138,15 +147,24 @@ ol.interaction.Draw.prototype.handleMapBrowserEvent = function(event) {
* @private * @private
*/ */
ol.interaction.Draw.prototype.handleClick_ = function(event) { ol.interaction.Draw.prototype.handleClick_ = function(event) {
if (goog.isNull(this.finishCoordinate_)) { var downPx = event.map.getEventPixel(event.target.getDown());
this.startDrawing_(event); var clickPx = event.getPixel();
} else if (this.mode_ === ol.interaction.DrawMode.POINT || var dx = downPx[0] - clickPx[0];
this.atFinish_(event)) { var dy = downPx[1] - clickPx[1];
this.finishDrawing_(event); var squaredDistance = dx * dx + dy * dy;
} else { var pass = true;
this.addToDrawing_(event); if (squaredDistance <= this.squaredClickTolerance_) {
if (goog.isNull(this.finishCoordinate_)) {
this.startDrawing_(event);
} else if (this.mode_ === ol.interaction.DrawMode.POINT ||
this.atFinish_(event)) {
this.finishDrawing_(event);
} else {
this.addToDrawing_(event);
}
pass = false;
} }
return false; return pass;
}; };