Search code examples
javascriptkonvajskonva

Get konva object X, Y not the pointerPosition after dragging


I'm using konvajs to create boxes and can be dragged and save the x,y coordinates after double click into the database via ajax, here is the code:

        layer.on('dblclick','Group',function(evt){
          var shape = evt.target;
          var bin_code = shape.name();
          var minibox = stage.findOne('#mbx_' + bin_code);
          var group = stage.findOne('#grp_'+ bin_code);
              $.ajax({
                url: '/iwms/_update_bin_coord',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify({'bin_code':bin_code,
                                      'x': group.x(),
                                      'y': group.y()}),
                contentType: "application/json; charset=utf-8",
                success: function(data){
                }
              });
              layer.draw();
        });

After I saved and reload the browser the coordinates is multiplying by 2 so the boxes is not saved where it supposed to. I try to divide x,y to 2, it works if my boxes initial coords is 0,0 but if my boxed initial coords is not originally in 0,0 this group.x() and group.y() giving me an absolute value from the last coords


Solution

  • I solved it now, by using absolutePosition() in https://konvajs.org/api/Konva.Node.html not the x() and y() of group object because x() and y() will return the distance changed from the original position