ed.blog

You will be vectorized.

How to get the svg elements intersected by a given rectangle

, , , ,

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.

Getting screen boundingboxes in svgOn mythbusting and the web... and why SVG Fonts are sometimes useful

Comments

Spadar ShutSpShut Monday, February 1, 2010 5:21:17 PM

Actually in Opera 10.10 i can't see any id's when hovering anything in the sandbox svg on this page.

Erik DahlströmMacDev_ed Monday, February 1, 2010 6:59:37 PM

@Sp.Shut:
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

Now it's working fine! I don't know what was the matter.

Spadar ShutSpShut Monday, February 1, 2010 7:48:32 PM

Erik, can you tell if there is an (easy) way to zoom in-out/pane parts of a svg embedded as an <object>? Only Opera support is required.

Erik DahlströmMacDev_ed Monday, February 1, 2010 7:56:45 PM

@Sp.Shut:
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

Thanks a lot!

Charles SchlossChas4 Tuesday, February 2, 2010 5:57:48 AM

Cool

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

Saeed writes: I think the code is running for me on Firefox, because i see 'uu(root)' and 'uu(path32xx)' under the image...but firebug gives me not implemented errors for getIntersectionList. I'd really like to be able to use that function with firefox, any ideas whats going on?

Erik DahlströmMacDev_ed Friday, September 17, 2010 12:30:48 PM

@saeed:
I'd suggest that you file a bug at https://bugzilla.mozilla.org/.

Write a comment

New comments have been disabled for this post.