简体   繁体   English

使用svg.draw.js时多边形中的重复点

[英]Repeated points in polygon when using svg.draw.js

When I create a polygon using draw() it inserts some points 2 times. 当我使用draw()创建多边形时,它会插入一些点2次。 Eg When I create a polygon with 4 points 例如,当我创建一个包含4个点的多边形时

<polygon id="SvgjsPolygon1063" points="179,177 179,177 397,147 323,381 323,381 88,350 88,350" class="labelbox shape"></polygon>

If I delete repeated points manually from the element I see there is no difference in the shape. 如果我从元素中手动删除重复的点,我会发现形状没有差异。 But if I move or reshape the polygon it again inserts 7 points. 但是,如果我移动或重塑多边形,它将再次插入7点。

  • In case of 3 points (triangle) polygon, total points are 5. 在3个点(三角形)多边形的情况下,总点数为5。
  • In case of 4 points polygon, total points are 7. 如果多边形为4点,则总点为7。
  • In case of 5 points polygon, total points are 8. 如果多边形是5点,则总点是8。

It'll be helpful if you can explain the logic so I can persist unique points and restore them properly. 如果您可以解释逻辑,这样对我很有帮助,我可以保留独特的观点并正确地还原它们。

Simplified Code from main application ; 主要应用的简化代码;

var createPolygon = function(){//TODO: bug: creating duplicate points
    var poly =  myCanvas.nested().polygon().addClass('labelpolygon shape').draw();
    poly.resize();
    poly.parent().draggable();

    poly.on('drawstart', function(e){
        document.addEventListener('keydown', function(e){
            if(e.keyCode == 13){
                poly.draw('done');
                poly.off('drawstart');
            }
        });
    });

    return poly;
}
myCanvas.on('mousedown', function(event){

    if(!alreadyDrawing){
        var currentTool = createPolygon(event,myCanvas);

        currentTool.on('drawstart', function(){
            alreadyDrawing = true;
        });
        currentTool.on('resizedone', function(){
            //update data
        });
        currentTool.draw(event);
        selectedElement = currentTool;
    }
});

myCanvas.on('mouseup', function(event){
    selectedElement.draw(event);
});

function attachEvents(currentTool){
    currentTool.parent().on('click',function(e) {
        currentTool.selectize({ rotationPoint: false});
        e.stopPropagation();
    });
}

draw has a standard behavior when you call it without an event. 在没有事件的情况下调用它时, draw具有标准行为。 It automatically binds to the click event in that case. 在这种情况下,它会自动绑定到click事件。 When you then start to call draw with an event, too. 然后,当您也开始通过事件调用draw时。 It will mess up the logic and produces the result you are getting. 它将弄乱逻辑并产生您得到的结果。

To solve this issue, decide for one option. 要解决此问题,请选择一个选项。 If you want more control, pass the event. 如果您想要更多控制权,请传递事件。 If you are fine with the click behavior, just call draw without any parameters 如果您对点击行为感到满意,只需调用不带任何参数的draw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM