Mouse events using Javascript with SVG

B

bryan.crompton

Hello!

I have a problem. I am trying to get the coordinates of the mouse
cursor when it clicks on an SVG (Scalable Vector Graphics) element.
The thing I am having trouble with, is that I can't seem to be able to
pass an event object to a function. For example, using HTML, the
following code works (this is all using firefox):

<html>
<head>
<title>event object parameter example</title>

<script type="text/javascript">

function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}

</script>
</head>

<body onmousedown="showCoords(event)">
<p>To display the mouse coordinates click anywhere on the page.</p>
</body>
</html>

When I copy this into a SVG file, as follows, its a no-go:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/
2000/svg" id="svgdoc">

<script type="text/ecmascript"><![CDATA[

function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}

]]>
</script>
<circle id="cir" cx="100" cy="200" r="40" fill="red"
onmousedown="showCoords(event)" />
</svg>

The javascript console says that event is undefined in the SVG
example.
Can anyone help me out?


-Bryan

P.S. Does anybody have a link to a comprehensive guide/reference of
javascript? Everything seems scattered across the web and I've only
been able to pick up bits and pieces of it.
 
M

Martin Honnen

<circle id="cir" cx="100" cy="200" r="40" fill="red"
onmousedown="showCoords(event)" />
</svg>

The javascript console says that event is undefined in the SVG
example.

Use
<circle onmousedown="showCoords(evt);" .../>
somehow the authors of the SVG specification decided to put up a lot of
hurdles for those moving from scripting of HTML to scripting of SVG so
they decided to make the event object available as the evt variable in
inline event handlers, not as event as we are used to from HTML.
 
D

dhtmlkitchen

Use
<circle onmousedown="showCoords(evt);" .../>
somehow the authors of the SVG specification decided to put up a lot of
hurdles for those moving from scripting of HTML to scripting of SVG so
they decided to make the event object available as the evt variable in
inline event handlers, not as event as we are used to from HTML.

Event buggle.

It sounds like what happens is that when the event bubbles, it loses
the clientX and clientY. To see if that's true, Try logging the event.
as captured on document.body, to Firebug using console.dir( event );

Please post the results of dir back to this forum (just copy paste
from Firebug). I am curious to know what the result is, but am not on
my dev box atm.

There may be a bug here.

Thank you,

Garrett
 
D

dhtmlkitchen

I said the very same thing some days ago.

Hrmm... and now for my new post question. It'll be "DHTMLKitchen.com's
suggestion of naming anonymous functions."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.- Hide quoted text -

- Show quoted text -

I don't know why comments about my website are showing up on this
thread.

But it looks like I'm a "Newbie" here and this does seem to be a
"vicious" place to some people who created another thread. I think I'm
starting to see why.

If anyone has a suggestion about my site, you can send me an email.
That would be the "Reply to author" button/link. I might actually make
a change to the site, time and energy permitting.

Garrett
 
B

bryan.crompton

Use
<circle onmousedown="showCoords(evt);" .../>
somehow the authors of the SVG specification decided to put up a lot of
hurdles for those moving from scripting of HTML to scripting of SVG so
they decided to make the event object available as the evt variable in
inline event handlers, not as event as we are used to from HTML.


The solution you gave worked. Thanks!

Again, does anyone have a good reference of the javascript language?
When I use it, I seem to be employing trial and error too much. I've
used several other languages, so in that regard I'm not having
trouble. Rather, I don't understand all the minute (but often
important) differences.

-Bryan
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 12 Jul 2007 04:37:12, (e-mail address removed)
posted:
Again, does anyone have a good reference of the javascript language?

The newsgroup FAQ, although ageing, is still your first reference. It
fails (IIRC) to link to ISO/IEC 16262, but it does link to ECMA 262. It
also links to Mozilla JavaScript 1.5 reference" and to good documents in
'.sun.'.
 
D

Dr J R Stockton

In comp.lang.javascript message <2uudnZQ8FtVkKAXbnZ2dnUVZ_v2unZ2d@comcas
What is the point in linking to both?

I've often wondered anyway, how the ECMA version is dated 1999, whereas
the ISO version is 2002 (if I remember correctly). Is the ISO version
independent of ECMA?


The ECMA one is more commonly quoted, but the more recent ISO one has
some of the errors changed. You have not, for example, read ECMA
section 15.9.1.7. And those who know of both have a choice of font
type, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top