You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by André de Brito <an...@evolve.pt> on 2011/02/11 12:41:18 UTC

Mouse EventListener on SVG group elements

Hello,

I'm using batik to display SVG files on a JSVGCanvas and I'm trying to
respond to mouse click events over SVG elements with tags <a>
(SVGOMAElement) and <g> (SVGOMGElement).
Here is the code I'm using to add the event listener to a Node:

if ((no.getNodeName().equalsIgnoreCase("a")) ||
(no.getNodeName().equalsIgnoreCase("g"))) {
    EventTarget etr = (EventTarget) no;
    etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new EventListener() {
                    public void handleEvent(Event evt) {
                        Log.debug("HANDLE EVENT");
                    }
    }, false);

What happens is that the listener is correcly called when an <a> element is
clicked, but nothing happens when a <g> element is clicked!
I debuged the code above and verified that the "click" listener is being
added to the both node's bubblingListeners table
(etr->eventSupport->bubblingListeners). However, I noticed that the <a>
elements seem to have two other (default) listeners - for events "mouseout"
and "mouseover" - which are not present on the <g> elements. I don't believe
this is the cause, but ...

Any ideas on what could be causing this behaviour?

Regards,
ABrito

Re: Mouse EventListener on SVG group elements

Posted by André de Brito <an...@evolve.pt>.
Hello Thomas,

You are absolutely right! I set the canvas to always dynamic and it works.
Thanks very much for the help.

To any future readers, you need to add the following line
"myJSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);"
before loading the document (with setURI, setDocument, setSVGDocument,
etc.).
(as explained in the batik FAQ 3.2)

Thanks again Thomas!

Kind regards,
ABrito

On 16 February 2011 22:42, <th...@kodak.com> wrote:

> Hi Andre,
>
>         I think the problem is that you need to tell the Canvas to be
> interactive/dynamic.  If the Canvas looks at the document and doesn't
> see any dynamic content it uses a quicker lower memory build that doesn't
> support event handlers at all.
>
>         Check out FAQ 3.2:
>                 http://xmlgraphics.apache.org/batik/faq.html
>
>         I had no idea what the problem was for quite some time
> since the difference isn't between clicking on a 'g' or an 'a' element
> it's between a document that has 'a' element or a document that doesn't
> have an 'a' element.
>
> André de Brito <an...@evolve.pt> wrote on 02/16/2011 04:48:44 PM:
>
>
> > I'm using batik to display SVG files on a JSVGCanvas and I'm trying
> > to respond to mouse click events over SVG elements with tags <a>
> > (SVGOMAElement) and <g> (SVGOMGElement).
> > Here is the code I'm using to add the event listener to a Node:
> >
> > if ((no.getNodeName().equalsIgnoreCase("a")) || (no.getNodeName
> > ().equalsIgnoreCase("g"))) {
> >     EventTarget etr = (EventTarget) no;
> >     etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new
> EventListener() {
> >                     public void handleEvent(Event evt) {
> >                         Log.debug("HANDLE EVENT");
> >                     }
> >     }, false);
> >
> > What happens is that the listener is correcly called when an <a>
> > element is clicked, but nothing happens when a <g> element is clicked!
> > I debuged the code above and verified that the "click" listener is
> > being added to the both node's bubblingListeners table
> > (etr->eventSupport->bubblingListeners). However, I noticed that the
> > <a> elements seem to have two other (default) listeners - for events
> > "mouseout" and "mouseover" - which are not present on the <g>
> > elements. I don't believe this is the cause, but ...
> >
> > Any ideas on what could be causing this behaviour?
>
> *Thomas DeWeese* | CDG Advanced Development |
> Eastman Kodak Company | 343 State Street | Rochester, NY 14650-0128 | *
> **Thomas.DeWeese@Kodak.com* <Th...@Kodak.com> | 585 724-0294 | *
> **www.kodak.com* <http://www.kodak.com/>
>

Re: Mouse EventListener on SVG group elements

Posted by th...@kodak.com.
Hi Andre,

        I think the problem is that you need to tell the Canvas to be
interactive/dynamic.  If the Canvas looks at the document and doesn't
see any dynamic content it uses a quicker lower memory build that doesn't
support event handlers at all.

        Check out FAQ 3.2:
                http://xmlgraphics.apache.org/batik/faq.html

        I had no idea what the problem was for quite some time
since the difference isn't between clicking on a 'g' or an 'a' element
it's between a document that has 'a' element or a document that doesn't
have an 'a' element.

André de Brito <an...@evolve.pt> wrote on 02/16/2011 04:48:44 PM:

> I'm using batik to display SVG files on a JSVGCanvas and I'm trying 
> to respond to mouse click events over SVG elements with tags <a> 
> (SVGOMAElement) and <g> (SVGOMGElement).
> Here is the code I'm using to add the event listener to a Node:
> 
> if ((no.getNodeName().equalsIgnoreCase("a")) || (no.getNodeName
> ().equalsIgnoreCase("g"))) {
>     EventTarget etr = (EventTarget) no;
>     etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new 
EventListener() {
>                     public void handleEvent(Event evt) {
>                         Log.debug("HANDLE EVENT");
>                     }
>     }, false);
> 
> What happens is that the listener is correcly called when an <a> 
> element is clicked, but nothing happens when a <g> element is clicked!
> I debuged the code above and verified that the "click" listener is 
> being added to the both node's bubblingListeners table 
> (etr->eventSupport->bubblingListeners). However, I noticed that the 
> <a> elements seem to have two other (default) listeners - for events
> "mouseout" and "mouseover" - which are not present on the <g> 
> elements. I don't believe this is the cause, but ...
> 
> Any ideas on what could be causing this behaviour?

Thomas DeWeese | CDG Advanced Development | 
Eastman Kodak Company | 343 State Street | Rochester, NY 14650-0128 | 
Thomas.DeWeese@Kodak.com | 585 724-0294 | 
www.kodak.com 

Re: Mouse EventListener on SVG group elements

Posted by André de Brito <an...@evolve.pt>.
No, it does not specify that attribute. I tried adding it manually to a
sample svg file, but it didn't help - same behaviour as before.

ABrito


Does your <g> specify the attribute pointer-events= "all"?
>
> http://www.w3.org/TR/SVG/interact.html#PointerEventsProperty
>
>
> Jonathan
>
>
>
> Sorry to insist, but does anyone have a clue on this subject? Or can you at
>> least point me to some other place where I can ask for help?
>>
>> Thanks.
>>
>> ---
>>
>>  Hello,
>>>
>>> I'm using batik to display SVG files on a JSVGCanvas and I'm trying to
>>> respond to mouse click events over SVG elements with tags <a>
>>> (SVGOMAElement) and <g> (SVGOMGElement).
>>> Here is the code I'm using to add the event listener to a Node:
>>>
>>> if ((no.getNodeName().equalsIgnoreCase("a")) ||
>>> (no.getNodeName().equalsIgnoreCase("g"))) {
>>>     EventTarget etr = (EventTarget) no;
>>>     etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new
>>> EventListener() {
>>>                     public void handleEvent(Event evt) {
>>>                         Log.debug("HANDLE EVENT");
>>>                     }
>>>     }, false);
>>>
>>> What happens is that the listener is correcly called when an <a> element
>>> is clicked, but nothing happens when a <g> element is clicked!
>>> I debuged the code above and verified that the "click" listener is being
>>> added to the both node's bubblingListeners table
>>> (etr->eventSupport->bubblingListeners). However, I noticed that the <a>
>>> elements seem to have two other (default) listeners - for events "mouseout"
>>> and "mouseover" - which are not present on the <g> elements. I don't believe
>>> this is the cause, but ...
>>>
>>> Any ideas on what could be causing this behaviour?
>>>
>>> Regards,
>>> ABrito
>>>
>>
>>
>

Re: Mouse EventListener on SVG group elements

Posted by jonathan wood <jo...@gmail.com>.
Does your <g> specify the attribute pointer-events= "all"?

http://www.w3.org/TR/SVG/interact.html#PointerEventsProperty


Jonathan

2011/2/15 André de Brito <an...@evolve.pt>

> Sorry to insist, but does anyone have a clue on this subject? Or can you at
> least point me to some other place where I can ask for help?
>
> Thanks.
>
> ---
>
>  Hello,
>>
>> I'm using batik to display SVG files on a JSVGCanvas and I'm trying to
>> respond to mouse click events over SVG elements with tags <a>
>> (SVGOMAElement) and <g> (SVGOMGElement).
>> Here is the code I'm using to add the event listener to a Node:
>>
>> if ((no.getNodeName().equalsIgnoreCase("a")) ||
>> (no.getNodeName().equalsIgnoreCase("g"))) {
>>     EventTarget etr = (EventTarget) no;
>>     etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new EventListener()
>> {
>>                     public void handleEvent(Event evt) {
>>                         Log.debug("HANDLE EVENT");
>>                     }
>>     }, false);
>>
>> What happens is that the listener is correcly called when an <a> element
>> is clicked, but nothing happens when a <g> element is clicked!
>> I debuged the code above and verified that the "click" listener is being
>> added to the both node's bubblingListeners table
>> (etr->eventSupport->bubblingListeners). However, I noticed that the <a>
>> elements seem to have two other (default) listeners - for events "mouseout"
>> and "mouseover" - which are not present on the <g> elements. I don't believe
>> this is the cause, but ...
>>
>> Any ideas on what could be causing this behaviour?
>>
>> Regards,
>> ABrito
>>
>
>

Re: Mouse EventListener on SVG group elements

Posted by André de Brito <an...@evolve.pt>.
Sorry to insist, but does anyone have a clue on this subject? Or can you at
least point me to some other place where I can ask for help?

Thanks.

---

> Hello,
>
> I'm using batik to display SVG files on a JSVGCanvas and I'm trying to
> respond to mouse click events over SVG elements with tags <a>
> (SVGOMAElement) and <g> (SVGOMGElement).
> Here is the code I'm using to add the event listener to a Node:
>
> if ((no.getNodeName().equalsIgnoreCase("a")) ||
> (no.getNodeName().equalsIgnoreCase("g"))) {
>     EventTarget etr = (EventTarget) no;
>     etr.addEventListener(SVGConstants.SVG_EVENT_CLICK, new EventListener()
> {
>                     public void handleEvent(Event evt) {
>                         Log.debug("HANDLE EVENT");
>                     }
>     }, false);
>
> What happens is that the listener is correcly called when an <a> element is
> clicked, but nothing happens when a <g> element is clicked!
> I debuged the code above and verified that the "click" listener is being
> added to the both node's bubblingListeners table
> (etr->eventSupport->bubblingListeners). However, I noticed that the <a>
> elements seem to have two other (default) listeners - for events "mouseout"
> and "mouseover" - which are not present on the <g> elements. I don't believe
> this is the cause, but ...
>
> Any ideas on what could be causing this behaviour?
>
> Regards,
> ABrito
>