Skip to content

Commit 2d2f436

Browse files
committed
add touch support of freehand mode of DrawTool, fix #1145
1 parent d12de0a commit 2d2f436

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

src/map/Map.DomEvents.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ Map.include(/** @lends Map.prototype */ {
199199
}
200200
let mimicClick = false;
201201
// ignore click lasted for more than 300ms.
202-
if (type === 'mousedown' || (type === 'touchstart' && e.touches.length === 1)) {
202+
// happen.js produce event without touches
203+
if (type === 'mousedown' || (type === 'touchstart' && (!e.touches || e.touches.length === 1))) {
203204
this._mouseDownTime = now();
204205
} else if ((type === 'click' || type === 'touchend' || type === 'contextmenu')) {
205206
if (!this._mouseDownTime) {

src/map/handler/Map.GeometryEvents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class MapGeometryEventsHandler extends Handler {
178178
let oneMoreEvent = null;
179179
const eventType = type || domEvent.type;
180180
// ignore click lasted for more than 300ms.
181-
if (eventType === 'mousedown' || (eventType === 'touchstart' && domEvent.touches.length === 1)) {
181+
if (eventType === 'mousedown' || (eventType === 'touchstart' && domEvent.touches && domEvent.touches.length === 1)) {
182182
this._mouseDownTime = now();
183183
} else if ((eventType === 'click' || eventType === 'touchend') && this._mouseDownTime) {
184184
const downTime = this._mouseDownTime;

src/map/tool/DrawTool.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ class DrawTool extends MapTool {
102102
*/
103103
this._events = {
104104
'click': this._clickHandler,
105-
'mousemove': this._mouseMoveHandler,
105+
'mousemove touchmove': this._mouseMoveHandler,
106106
'dblclick': this._doubleClickHandler,
107+
'mousedown touchstart': this._mouseDownHandler,
108+
'mouseup touchend': this._mouseUpHandler,
109+
'mousemove': this._mouseMoveHandler,
107110
'mousedown': this._mouseDownHandler,
108111
'mouseup': this._mouseUpHandler
109112
};
@@ -264,7 +267,14 @@ class DrawTool extends MapTool {
264267
'doubleClickZoom': this.options['doubleClickZoom']
265268
});
266269
const actions = this._getRegisterMode()['action'];
267-
if (actions.indexOf('mousedown') > -1) {
270+
let dragging = false;
271+
for (let i = 0; i < actions.length; i++) {
272+
if (actions[i].indexOf('mousemove') >= 0) {
273+
dragging = true;
274+
break;
275+
}
276+
}
277+
if (dragging) {
268278
const map = this.getMap();
269279
this._mapDraggable = map.options['draggable'];
270280
map.config({
@@ -569,7 +579,8 @@ class DrawTool extends MapTool {
569579
*/
570580
_getMouseContainerPoint(event) {
571581
const action = this._getRegisterMode()['action'];
572-
if (action === 'mousedown') {
582+
if (action[0].indexOf('mousedown') >= 0 || action[0].indexOf('touchstart') >= 0) {
583+
//prevent map's event propogation
573584
stopPropagation(event['domEvent']);
574585
}
575586
return event['containerPoint'];

src/map/tool/DrawToolRegister.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ DrawTool.registerMode('circle', extend({
3737
}, circleHooks));
3838

3939
DrawTool.registerMode('freeHandCircle', extend({
40-
'action': ['mousedown', 'mousemove', 'mouseup']
40+
'action': ['mousedown touchstart', 'mousemove touchmove', 'mouseup touchend']
4141
}, circleHooks));
4242

4343
const ellipseHooks = {
@@ -74,7 +74,7 @@ DrawTool.registerMode('ellipse', extend({
7474
}, ellipseHooks));
7575

7676
DrawTool.registerMode('freeHandEllipse', extend({
77-
'action': ['mousedown', 'mousemove', 'mouseup']
77+
'action': ['mousedown touchstart', 'mousemove touchmove', 'mouseup touchend']
7878
}, ellipseHooks));
7979

8080
const rectangleHooks = {
@@ -107,7 +107,7 @@ DrawTool.registerMode('rectangle', extend({
107107
}, rectangleHooks));
108108

109109
DrawTool.registerMode('freeHandRectangle', extend({
110-
'action': ['mousedown', 'mousemove', 'mouseup']
110+
'action': ['mousedown touchstart', 'mousemove touchmove', 'mouseup touchend']
111111
}, rectangleHooks));
112112

113113
DrawTool.registerMode('point', {
@@ -178,7 +178,7 @@ DrawTool.registerMode('polygon', extend({
178178
}, polygonHooks));
179179

180180
DrawTool.registerMode('freeHandPolygon', extend({
181-
'action': ['mousedown', 'mousemove', 'mouseup']
181+
'action': ['mousedown touchstart', 'mousemove touchmove', 'mouseup touchend']
182182
}, polygonHooks));
183183

184184
const lineStringHooks = {
@@ -210,7 +210,7 @@ DrawTool.registerMode('linestring', extend({
210210
}, lineStringHooks));
211211

212212
DrawTool.registerMode('freeHandLinestring', extend({
213-
'action': ['mousedown', 'mousemove', 'mouseup']
213+
'action': ['mousedown touchstart', 'mousemove touchmove', 'mouseup touchend']
214214
}, lineStringHooks));
215215

216216
DrawTool.registerMode('arccurve', {

test/map/tools/DrawToolSpec.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ describe('DrawTool', function () {
7979
});
8080
}
8181

82+
function dragDrawByTouch() {
83+
var center = map.getCenter();
84+
85+
var domPosition = GET_PAGE_POSITION(container);
86+
var point = map.coordinateToContainerPoint(center).add(domPosition);
87+
happen.touchstart(eventContainer, {
88+
'clientX':point.x,
89+
'clientY':point.y
90+
});
91+
for (var i = 0; i < 10; i++) {
92+
happen.touchmove(eventContainer, {
93+
'clientX':point.x - i,
94+
'clientY':point.y - i
95+
});
96+
}
97+
happen.touchend(eventContainer, {
98+
'clientX':point.x - 10,
99+
'clientY':point.y - 10
100+
});
101+
}
102+
82103
function drawPoint() {
83104
var center = map.getCenter();
84105

@@ -227,9 +248,10 @@ describe('DrawTool', function () {
227248
expect(param.geometry instanceof maptalks.Polygon).to.be.ok();
228249
var coordinates = param.geometry.getCoordinates()[0];
229250
expect(coordinates.length === 5).to.be.ok();
230-
if (!maptalks.Browser.ie) {
231-
expect(coordinates[1].toArray()).to.be.closeTo([118.846756, 32.046603]);
232-
}
251+
expect(coordinates[0].toArray()).not.to.be.eql(coordinates[1].toArray());
252+
expect(coordinates[1].toArray()).not.to.be.eql(coordinates[2].toArray());
253+
expect(coordinates[2].toArray()).not.to.be.eql(coordinates[3].toArray());
254+
expect(coordinates[0].toArray()).to.be.eql(coordinates[0].toArray());
233255
done();
234256
}
235257
var drawTool = new maptalks.DrawTool({
@@ -270,6 +292,20 @@ describe('DrawTool', function () {
270292
dragDraw(drawTool);
271293
});
272294

295+
it('can draw FreeHandLinestring with touch', function (done) {
296+
function drawEnd(param) {
297+
expect(param.geometry instanceof maptalks.LineString).to.be.ok();
298+
expect(param.geometry.getLength()).to.above(0);
299+
done();
300+
}
301+
var drawTool = new maptalks.DrawTool({
302+
mode : 'FreeHandLinestring'
303+
});
304+
drawTool.addTo(map);
305+
drawTool.on('drawend', drawEnd);
306+
dragDrawByTouch(drawTool);
307+
});
308+
273309
it('can draw FreeHandPolygon', function (done) {
274310
function drawEnd(param) {
275311
expect(param.geometry instanceof maptalks.Polygon).to.be.ok();

0 commit comments

Comments
 (0)