Arbor js - Show the node ID as text when you move the mouse over a node

1.6k Views Asked by At

I am quite new to Javascript and Arbor and having some difficulties to draw a complete graph of nodes. I would like to start my question with showing my code:

 var handler = {
            clicked:function(e){
                var pos = $(canvas).offset();
                _mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
                selected = nearest = dragged = particleSystem.nearest(_mouseP);
                if (dragged.node !== null) dragged.node.fixed = true
                $(canvas).bind('mousemove', handler.dragged)
                $(window).bind('mouseup', handler.dropped)
                $(canvas).bind('mouseup', handler.newFunction)
            },
            newFunction:function(e){
                if (dragged===null || dragged.node===undefined) return
                if (dragged.node !== null){
                    dragged.node.fixed = false                  
                    var id=dragged.node.name;
                    //alert('Node selected: ' + id);
                }            
                return false
            },  
    }

I have created one node on the canvas. My question is: how can I show the node ID, for example, or any information as text as soon as I move the mouse over the node? I want to show the text ONLY when I move the mouse over that node. Your help would be very much appreciated.

1

There are 1 best solutions below

0
On

Maybe you can add a mousemove listener,then you can test whether mouse is over one node or not in your callback function.

    moved:function(e){
        var pos = $(canvas).offset();
        _mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
        nearestNode = particleSystem.nearest(_mouseP);

        if (!nearestNode.node) return false

        boxTuple = nodeBoxes[nearestNode.node.name];

        //judge whether mouse is on the node or not
        if(isInRectangle(boxTuple,_mouseP))
        {
                            var id = nearestNode.node.name;
            alert("Node selected:" + id);
        }
        return false
      },

     //add a mousemove listener 
    $(canvas).mousemove(handler.moved);

Hope it helps!