On Mon, 26 Oct 2009 10:44:49 +0100, HODAC, Olivier wrote in
<1950_1256550419_4ae57013_1950_34_1_0471c9ac78f671458c5e3f9c69b73c8609879...@fr0-mailmb17.res.airbus.corp>:

>I'd like to add a mouse listener on my JSVGCanvas to select an element
>of my rendered SVG.
>How can I find the right node using x,y mouse postion?


I have explored that, but the problem is that you have
only the bounds of the element to locate your mouse point
in. This is a rectangle. If now the element is, say, an
ellipse or a line or another shape that is not rectangular, 
the function won't work with mouse points inside that 
rectangle, but outside the shape.

You need to add an event listener to each element as
you create it. If you load your drawing from a file,
you need to loop through the whole element tree and
add it to each element. It's not so much code, as 
you do it all in a loop.

Here is the code that I am using, after I have retrieved
my document string from a database and created the document. 

I have the special situation that I skip the first element, 
which is a background rectangle that I don't want to be
moveable. It is always the first in the document.


            BackG = doc.getElementById("BackgroundRectangle");
            EventTarget etr = null;
            Node no = (Node) BackG;
            Node no2 = no.getNextSibling();
            if (no2!=null) {
                do {
                    etr = (EventTarget) no2; 
                    etr.addEventListener("click",new 
org.w3c.dom.events.EventListener()
                    {
                        org.w3c.dom.events.Event evt;

                        public void handleEvent(org.w3c.dom.events.Event evt) {
                            EventTarget etr2 = evt.getTarget();

                            ActivateSelEl(etr2); // function that selects the 
element,
                                                 // retrieves its bounds and 
sets a flag 
                                                 // that tells the canvas to 
paint a 
                                                 // selection rectangle with 
mouse 
                                                 // anchors on top of the SVG 
drawing 
                                                 // in its paint function
                            repaint(); 
                        }
                        public void run() {
                            handleEvent(evt);
                        }

                    },false
                    );
                    no2=no2.getNextSibling();
                } while (no2!=null);
            }



Best,


Heidrun Beer

Workgroup for Fundamental Spiritual Research and Mental Training
http://www.sgmt.at
http://www.RecastReality.org


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to