如何在JointJS中以交互方式创建链接

Sil*_*ley 5 javascript jointjs

我想以交互方式添加指向基于JointJS的图表的链接.

我的想法是pointerdown使用从原始节点到此临时节点的链接创建一个小的临时节点,将其拖到另一个节点的顶​​部,然后pointerup创建删除临时链接和节点的真实链接.

不幸的是,我不知道如何说服指针移动临时元素而不是pointerdown发生事件的节点.任何的想法?谢谢!

var tmp_obj;
paper.on('cell:pointerdown', function(cellView, evt, x, y) {
    if(evt.button == 1) {
        // Freeze the selected node so it does not move
        paper.findViewByModel(cellView.model).options.interactive = false;

        // Create a small temporary node on top of the selected node
        tmp_obj = new joint.shapes.basic.Rect({
            position: { x: x, y: y },
            size: { width: 5, height: 5 }
        }

        // Create the link between the original node and the small temporary node

        // And now?
    }
}

paper.on('cell:pointerup', function(cellView, evt, x, y) {

    if(evt.button == 1) {
        // Unfreeze the origin node
        paper.findViewByModel(cellView.model).options.interactive = true;

        // Delete the temporary object (and temporary link)
        tmp_obj.remove()

        // Find the first element below that is not a link nor the dragged element itself.
        var elementBelow = graph.get('cells').find(function(cell) {
            if (cell instanceof joint.dia.Link) return false; // Not interested in links.
            if (cell.id === cellView.model.id) return false; // The same element as the dropped one.
            if (cell.getBBox().containsPoint(g.point(x, y))) {
                return true;
            }
            return false;
        });

        // create the link from cellView to elementBelow 
    }
});
Run Code Online (Sandbox Code Playgroud)

dav*_*ave 9

也许你可以使用JointJS的磁铁功能?如果设置magnet: true了JointJS元素的SVG子元素,则该元素将变为活动状态,并允许用户创建从该元素开始的链接.例如:

var r = new joint.shapes.basic.Rect({
    position: { x: 50, y: 50 },
    size: { width: 100, height: 40 },
    attrs: { text: { text: 'basic.Rect', magnet: true } }
});
Run Code Online (Sandbox Code Playgroud)

这将使元素表现得像一个端口.

  • 太棒了!也许应该记录得更好一些.现在我必须找到一种方法来防止在节点和任何东西之间创建悬空链接. (7认同)