javascript - Detecting Mouse Events on Multiple Overlapping SVG Elements -


i'm trying detect mousemove events on partially overlapping svg elements, in image

enter image description here

fiddle

<svg>     <rect id="red"    x=10 y=10 width=60 height=60 style="fill:#ff0000" />     <rect id="orange" x=80 y=10 width=60 height=60 style="fill:#ffcc00" />     <rect id="blue"   x=50 y=30 width=60 height=60 style="fill:#0000ff; fill-opacity: 0.8" /> </svg>  $('rect').on('mousemove', function() {     log(this.id); }); 

now, when hovering mouse on blue/red intersection i'd detect mouse events on both elements, , same blue/orange combo. can see in logs, in cases event fired blue box on top.

this has pointer-events, can red , orange elements fire event while hovering blue element setting blue element's pointer-events none. don't events blue box, not viable option either.

i use whichever library solves problem. looked @ event bubbling in this d3 example, works elements nested in dom. have lots of independent elements may overlap lots of other elements , can therefore not structure dom way.

i'm guessing last resort find elements @ current mouse position, , manually firing events. therefore, looked @ document.elementfrompoint(), yield 1 element (and may not work in svg?). found jquerypp function within, finds elements @ given position, see here. example looks great, except it's divs , not inside svg. when replacing divs svg rectangle elements, fiddle seems break.

what do?!

the great comments here gave me answer: it's possible propagate event underlying elements manually finding them using getintersectionlist() @ cursor positon.

$('svg').on('mousemove', function(evt) {     var root = $('svg')[0];     var rpos = root.createsvgrect();     rpos.x = evt.clientx;     rpos.y = evt.clienty;     rpos.width = rpos.height = 1;     var list = root.getintersectionlist(rpos, null);      for(var = 0; < list.length; i++)     {         if(list[i] != evt.target)         {             $(list[i]).mousemove();         }     } }); 

working example: http://jsfiddle.net/michaschwab/w0wufbtn/6/

if other listeners need original event object, check out http://jsfiddle.net/michaschwab/w0wufbtn/13/.

thanks lot!!


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -