var iD = {}; iD.Draw = function() { var event = d3.dispatch(draw, 'draw'), x0, x1, y0, y1; var feature = { type: 'Feature', geometry: { type: 'LineString', coordinates: [] } }; function draw() { this .on('mousemove.draw', mousemove) .on('click.draw', mousedown); } // move an elastic band that shows where the next point // will be function mousemove(e) { var target = this; function mouse() { var touches = d3.event.changedTouches; return touches ? d3.touches(target, touches)[0] : d3.mouse(target); } var ll = projection.invert(mouse()); var elastic = { type: 'Feature', geometry: { type: 'LineString', coordinates: [] } }; if (!feature.geometry.coordinates.length) { feature.geometry.coordinates[0] = ll; event.draw(feature); feature.geometry.coordinates = []; } else { var l = feature.geometry.coordinates.length - 1; elastic.geometry.coordinates = [ feature.geometry.coordinates[l], ll]; event.draw(feature, elastic); } } // add a point function mousedown(e) { var target = this; function mouse() { var touches = d3.event.changedTouches; return touches ? d3.touches(target, touches)[0] : d3.mouse(target); } var ll = projection.invert(mouse()); feature.geometry.coordinates.push(ll); event.draw(feature); d3.event.stopPropagation(); } return d3.rebind(draw, event, "on"); };