How to get the svg elements intersected by a given rectangle
Monday, February 1, 2010 8:48:56 AM
Yet another sandbox svg! This time we're going to list all the graphic elements that are intersected by a one-pixel rectangle positioned at the current mousepointer location.
Below is an example of a function that returns the list of elements under the broomsti^Wmousecursor by using the SVG DOM method getIntersectionList. Pass in a MouseEvent object from a mouse event handler to get a static NodeList object back.
function getInteresectedElements(evt)
{
var rpos = svgroot.createSVGRect();
rpos.x = evt.clientX;
rpos.y = evt.clientY;
rpos.width = rpos.height = 1;
return root.getIntersectionList(rpos, null);
}
Here's an modified variant of the above running in the browser. Hovering the graphic will show the id's of the currently intersected elements. Note that to work this script requires that the SVG 1.1 DOM method getIntersectionList is implemented, which at the time of writing is not the case in Firefox or Safari.
View live example.
The script that shows the intersected elements and the current mouse position can be viewed here. To use it in another svg file just download that script and add a 'script' element to the svg file you want to use it in, like this:
... <script xlink:href="showintersectedelms.js"/> ...Tested in Opera 10.10.








Spadar ShutSpShut # Monday, February 1, 2010 5:21:17 PM
Erik DahlströmMacDev_ed # Monday, February 1, 2010 6:59:37 PM
Can you report your details to the Opera bugtracker please?
The example has been tested on three different machines/OS'es, osx, linux and winxp all using the latest available Opera 10.10 release. All worked fine for me.
Spadar ShutSpShut # Monday, February 1, 2010 7:34:19 PM
Spadar ShutSpShut # Monday, February 1, 2010 7:48:32 PM
Erik DahlströmMacDev_ed # Monday, February 1, 2010 7:56:45 PM
You can change the zoomfactor by doing something like this:
yourObjectElm.contentDocument.documentElement.currentScale *= 2;That should work fine in all the browsers (untested though).
Or if you want you can change the root svg element's 'viewBox' to zoom into a particular location, like in this example.
Spadar ShutSpShut # Monday, February 1, 2010 8:49:20 PM
Charles SchlossChas4 # Tuesday, February 2, 2010 5:57:48 AM
Safari 4.0.4 does not show the path 32xx, but it does show the other things next to the word; screen : uu
Got here because it showed up in the Planet Opera feed:
http://feeds.feedburner.com/planetopera
Unregistered user # Friday, June 25, 2010 10:00:58 PM
Erik DahlströmMacDev_ed # Friday, September 17, 2010 12:30:48 PM
I'd suggest that you file a bug at https://bugzilla.mozilla.org/.