Search code examples
javascriptdraw2ddraw2d-js

draw2d on drag start event


I want to call my custom callback function every time, when user starts dragging figures. I tried to do it like so:

figure.onDragStart = function (x, y, shiftKey, ctrlKey) {
    myfunc();
};

But the problem is, every time when my function is called, figure is positioned to 0,0 coordinate. I'm not sure what is wrong with that and how can I fix it.


Solution

  • Instead of redefining the onDragStart method, you can handle the dragstart event:

    figure.on("dragstart", function(event, ui) {
        // Do something
    });
    

    According to the draw2d documentation, that event is triggered by onDragStart. The ui parameter contains x, y, shiftKey and ctrlKey.

    You can see the code in action in this jsfiddle. A message is displayed in the console when you start dragging a rectangle. The green rectangle uses the dragstart event and responds correctly. The red one redefines the onDragStart method, with the problem mentioned in your question.