Hi Louis,

"louis-rémi BABE" <[email protected]> wrote on 12/29/2008 10:00:44 AM:

> I want to detect position of the cursor when the user click on a 
document.
> 
> For some reasons, this code doesn't work on an empty document.
> Hoever, if I create an element inside my document, when I click on 
> this element, the eventListener is triggered :

   This is correct.  Events are only delivered if they are over an
element in the SVG document (group and SVG element's don't count since
they don't have any geometry).  There may be some disagreement between
SVG implementations on this point BTW.

> // Create and append to the root a background to catch mouse events
> bg.setAttributeNS (null, "width", "100%");
> bg.setAttributeNS (null, "height", "100%");
> bg.setAttributeNS (null, "fill", "white");
> bg.setAttributeNS (null, "id", "bg"); 
> root.appendChild(bg);

> I don't really like this solution, I'm sure using this background 
> element could be avoided.
> Do you have any idea how I could achieve that ?

   The element can't be avoided.  However you can avoid having an
opaque background by using the pointer-events property.  Also I
typically make the rect larger than the viewport:

 bg.setAttributeNS (null, "x", "-100%");
 bg.setAttributeNS (null, "y", "-100%");
 bg.setAttributeNS (null, "width", "300%");
 bg.setAttributeNS (null, "height", "300%");
 bg.setAttributeNS (null, "fill", "none");
 bg.setAttributeNS (null, "pointer-events", "fill");
 bg.setAttributeNS (null, "id", "bg"); 
 root.appendChild(bg);

   Depending on the outcome of a WG decision it may be that
using pointer-events for this purpose may essentially be 
deprecated in favor of using 'opacity="0"' with 'fill="white"'.

Reply via email to