ephtron
ephtron

Reputation: 1125

How can I pass ONLY clicks through a SVG with pointer-events?

I have a SVG overlaying a div with a button. I know that i can pass mouse-events through the SVG by setting "pointer-events: none;" for my SVG. However when I do this the SVG wont recognize mouse-events anymore.

<body>
  <div id="website">
    <form action="input_button.htm">
      <p>
        <textarea cols="20" rows="4" name="text"></textarea>
        <input type="button" name="Text 1" value="show text" 
         onclick="this.form.text.value='Test'">
      </p>
    </form>
  </div>
  <div id="svgRect">
    <svg width="1845" height="140">
      <rect id="r0"></rect>
    </svg>
  </div>
</body>

I want my SVG to be able to recognize when the mouse is over it but pass clicks to elements (divs/ buttons / ...) underneath itself. So my SVG should only be the target of hover-events and my button should be the target of click-events.

Among some other approaches I tried it like this: - Nothing worked.

.on("mousedown", function(d,i){
   d3.select("#r0")
     .style("pointer-events", "none");
   d3.select("#website")
     .style("pointer-events", "auto");}
.on("mouseup", function(d,i){
   d3.select("#r0")
     .style("pointer-events", "auto");
   d3.select("#website")
     .style("pointer-events", "none");
}

The idea was to disable pointer-events when I press the mouse-button and enable them again when I release it.

Does anyone know a solution or work-arround for this problem? Thanks!

Upvotes: 11

Views: 10325

Answers (4)

Jesus CMD
Jesus CMD

Reputation: 184

Here there is a way, I give you an example

body {
  margin: 0;
}

.wrapper {
  background-color: #e54d42;
  height: 100%;
  width: 100%;
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.link {
  cursor: pointer;
  color: #eee;
  font-family: Helvetica, Verdana;
  font-weight: 100;
  font-size: 40px;
}
.link:hover {
  color: #e59a95;
}

.mask {
  position: absolute;
  top: 0;
  width: 100%;
  right: 0;
}

.mask {
  pointer-events: none;
}

.svg-not-transparent {
  pointer-events: fill;
  cursor: pointer;
}
.svg-not-transparent:hover {
  fill: #333;
}
<div class="wrapper">
  <a class="link">En: I am reacheable even if there is a big SVG omer me <br>But not over the  SVG shape<br>Esp: Puedes darme click aún cuándo hay un SVG sobre mi.<br>Pero no sobre el poligono</a>
  <svg version="1.1" class="mask" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1980 1320" style="enable-background:new 0 0 1980 1320;" xml:space="preserve">
					<polygon class="svg-not-transparent" points="1277.7,0 567.8,1337.5 1980,1337.5 1980,0 "/>
  </svg>
</div>

: http://codepen.io/jesuscmd/pen/EyEyoP

Upvotes: 1

Jesus CMD
Jesus CMD

Reputation: 184

Here is a easier solution: Keep pointer-events: none; in the svg, but add pointer-events: fill to the poligon, path or whatever your button is, with this, the empty space is transparente, but the button is sensible to click

https://stackoverflow.com/a/29319009/2594391

Upvotes: 8

ephtron
ephtron

Reputation: 1125

I found a solution to my problem. It is possible to traverse through all underlying elements with the elementFromPoint(x,y); function. I wrote a helper-function that checks if the first selected element is a SVG - if it is one its' display is set to "none" and the next element is selected.

function get_element_under_svg(x,y){
    var resulting_element;
    var first_element = document.elementFromPoint(x,y);
    //check if first_element is a svg
    if (first_element.nodeName == "rect") {
      _display = first_element.style.display;    //save display of svg
      first_element.style.display = "none";      // make svg invisible
      resulting_element = document.elementFromPoint(x,y); 
      first_element.style.display = _display;    // reset display
    } else {
      resulting_element = first_element;
    }
    return resulting_element;
  }  
  return lower_element;
}

At the end of the day I set pointer-events: auto for my SVG and for my div:

#website{
  pointer-event: auto;
}
svg{
  pointer-event: auto;
}

And to my svg I added the following:

.on("click", function(d,i) {
  var element = get_element_under_rect( mouse.x, mouse.y );
  element.click(); // simulate click on the underlying element
}); 

With this approach my SVG is still capable to receive hover- or click-events, while it is able to pass clicks to underlying elements. See also https://developer.mozilla.org/en-US/docs/Web/API/document.elementFromPoint

Thanks for the other approaches!

Upvotes: 5

mfk
mfk

Reputation: 331

You could disable pointer events on the SVG, but add another div of the same size that is a transparent parent of both the SVG and the form. You could then listen to the hover events on the transparent parent:

var btn = document.getElementById("btn");
var container = document.getElementById("container");

container.addEventListener("mouseover", function() {
  log("SVG mouse over");
}, true);
container.addEventListener("mouseout", function() {
  log("SVG mouse out");
}, true);

btn.addEventListener("click", function() {
  log("BTN click");
}, false);

var out = document.getElementById("out");

function log(s) {
  out.innerHTML += s + "<br>";
}
#container {
  position: relative;
  width: 300px;
  height: 100px;
}
#svgOverlay {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  pointer-events: none;
}
#out {
  width: 200px;
  height: 300px;
  overflow: auto;
  border: 1px solid gray;
  position: absolute;
  top: 160px;
}
<body>
  <div id="container">
    <div id="website">
      <form action="input_button.htm">
        <textarea cols="20" rows="4" name="text"></textarea>
        <input type="button" name="Text 1" value="show text" onclick="this.form.text.value='Test'" id="btn"></input>
      </form>
    </div>
    <svg id="svgOverlay" width="300" height="100">
      <rect width="100%" stroke="black" height="100%" fill="gray" id="r0" opacity=".5"></rect>
    </svg>
  </div>
  <div id="out"></div>
</body>

Upvotes: 0

Related Questions