Search code examples
javascripteventsjsxgraph

JSX Graph: Event for polygon borders get dragged


I need to catch events that change polygon's position. It's ok for points and a polygon itself:

polygon.on('drag', handler);
pointOfPolygon.on('drag', handler);

But these don't catch it when the polygon is dragged by its border. How can that be achieved? Thanks


Solution

  • The polygon's borders can be accessed through the array polygon.border, whose elements can handle the drag event. Thus, an elementary example would be:

    var cnt = 0;
    var pol = board.create('polygon', [[-2, -2], [2, -1], [1, 3]]);
    var txt = board.create('text', [2, 3, "Text"]);
    
    var handler = function(evt) {
            cnt++;
            txt.setText(cnt);
        };
    
    pol.borders[0].on('drag', handler);
    pol.borders[1].on('drag', handler);
    pol.borders[2].on('drag', handler);
    

    See it live at https://jsfiddle.net/125nfxL7/1/