openlayers3如何始终启用徒手绘制(openlayers3 how to always enable freehand draw)

在OpenLayers3 v3.5中,你如何始终启用徒手画? 启用徒手绘制的默认设置是通过ol.interaction.Draw的freehandCondition属性完成的,默认情况下,该属性当前设置为shift键。

draw = new ol.interaction.Draw({ source: drawLayer.getSource(), type: 'LineString', freehandCondition: ol.events.condition.shiftKeyOnly });

但我不想那样。 我不希望按下shift键来启用徒手画。 我想在没有任何键修饰符的情况下通过单击并拖动来启用徒手画。

我试过了:

freehandCondition: ol.events.condition.always

freehandCondition: ol.events.condition.click

freehandCondition: ol.events.condition.noModifierKeys

但这些都不起作用。

您可能想知道通过这样做会平移地图,但我已经通过更改我的默认交互禁用平移,以便dragPan: false

In OpenLayers3 v3.5, how do you always enable freehand draw? The default for enabling freehand draw is done through the freehandCondition property of ol.interaction.Draw, which is currently set to the shift key by default.

draw = new ol.interaction.Draw({ source: drawLayer.getSource(), type: 'LineString', freehandCondition: ol.events.condition.shiftKeyOnly });

But I dont want that. I dont want the shift key to be pressed to enable freehand. I want freehand to be enabled by click-and-drag without any key modifiers.

I've tried:

freehandCondition: ol.events.condition.always

freehandCondition: ol.events.condition.click

freehandCondition: ol.events.condition.noModifierKeys

But none of these work.

You may wonder that by doing this would pan the map, but I've already disabled panning by changing my default interactions so that dragPan: false

最满意答案

你错过了文档 , ol.interaction.Draw的condition参数。 它与freehandCondition冲突。

它应该像下面(测试)

draw = new ol.interaction.Draw({ source: drawLayer.getSource(), type: 'LineString', condition: ol.events.condition.singleClick, freehandCondition: ol.events.condition.noModifierKeys });

看看这个小提琴进行演示。

我可能错过了一个更好的选择。 如果行为不完全符合预期,您可能还需要尝试其他条件。

You missed in the documentation, the condition parameter for the ol.interaction.Draw. It conflicts with freehandCondition.

It should be like below (tested)

draw = new ol.interaction.Draw({ source: drawLayer.getSource(), type: 'LineString', condition: ol.events.condition.singleClick, freehandCondition: ol.events.condition.noModifierKeys });

Look at this Fiddle for a demo.

I may missed a better option. You may also need to try with other conditions if the behaviour is not exactly the expected one.

更多推荐