2015-11-25 175 views
2

我想在釋放鼠標按鈕而不是雙擊時完成繪製。我怎樣才能做到這一點?如何在鼠標釋放時在Openlayers中完成徒手繪製

現在,我使用此代碼,默認情況下激活的OpenLayers drawing feature徒手畫:

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

但我不知道如何避免雙擊完成線串,並使用鼠標釋放來代替。

回答

1

UPDATE - https://jsfiddle.net/jonataswalker/frwfuzzn/

要同時啓用寫意直線,我計算抽選開始和pointerup使用此功能之間所經過的時間:

var isStraightLine = function(){ 
    var now = new Date(); 
    // `start_time` was set at the drawing beginning 
    var diff = now - start_time; 
    // strip the ms 
    diff /= 1000; 
    already_checked = true; 
    // an average time I found here 
    // not sure if this is completely reliable 
    return diff < 0.07; 
}; 

如果我發現這是寫意畫完成draw.finishDrawing()。爲了理解起見,更好地檢查代碼。


我可以,但只有當你留在freehandCondition默認條件 - ol.events.condition.shiftKeyOnly

pointerupol.Map,如果你正在繪製,完成ol.interaction.Draw#finishDrawing

var start_drawing = false; 
var draw = new ol.interaction.Draw({ 
    source: vectorSource, 
    type: 'LineString', 
    condition: ol.events.condition.singleClick 
}); 
map.addInteraction(draw); 

draw.on('drawstart', function(evt){ 
    start_drawing = true; 
}); 
draw.on('drawend', function(evt){ 
    start_drawing = false; 
}); 
map.on('pointerup', function(evt){ 
    if(start_drawing) { 
    draw.finishDrawing(); 
    } 
}); 

http://jsfiddle.net/jonataswalker/ewv0mo3c/

+0

其實它的工作也與freehandCondition noModifierKeys,但你要刪除的地圖dragPan:https://jsfiddle.net/geraldo/tfcy010q/1/ – geraldo

+0

現在唯一的改進是使工作也直線畫。正如你可以在jsfiddle中看到的那樣,當你點擊時,線條開始,但是沒有完成,因爲drawend立即被調用。問題是我無法弄清楚如何區分不同位置上的mousedown和mouseup。 – geraldo

+0

並且直線將如何完成?標準'dblclick'? –