Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locate an element within SVG in Opera by coordinates

Tags:

How do I locate an element within SVG in Opera, given coordinates?

elementFromPoint(x,y) works fine with Firefox, but seems to fail with Opera, returning always the whole SVG and not the particular element.

One might wonder why do I need it at all. Well, simply because I'd like to highlight the SVG element under the cursor, and because Opera doesn't fire any event when an element under the cursor is added/deleted, before you make a move with a mouse. That is, when I add a new element, it is not highlighted before I move the mouse slightly, which doesn't look nice.

Cheers, Mikhail.

like image 922
Qnan Avatar asked Feb 13 '10 23:02

Qnan


1 Answers

In Opera there is SVG1.1's SVGSVGElement.getIntersectionList.

var element= document.elementFromPoint(pageX, pageY);
if (element.localName.toLowerCase()=='svg' && 'getIntersectionList' in element) {
    var svgxy= Element_getPageXY(svg); // by the usual offsetLeft/offsetParent etc. method
    var rect= svg.createSVGRect();
    rect.x= pageX-svgxy[0];
    rect.y= pageY-svgxy[1];
    rect.width=rect.height= 1;
    var hits= svg.getIntersectionList(rect, null);
    if (hits.length>0)
        element= hits[hits.length-1];
}

[Untested code, might even work.]

like image 88
bobince Avatar answered Oct 03 '22 19:10

bobince