Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active August 24, 2018 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romsson/757ad6ea093000bf1900d6c0e2a9b126 to your computer and use it in GitHub Desktop.
Save romsson/757ad6ea093000bf1900d6c0e2a9b126 to your computer and use it in GitHub Desktop.
detect olverapped SVG elements intersections
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// https://stackoverflow.com/questions/16799116/handling-mouse-events-in-overlapping-svg-layers
function passThru(d) {
console.log("selected color: ", d3.select(this).style("fill"))
var e = d3.event;
var prev = this.style.pointerEvents;
this.style.pointerEvents = 'none';
var el = document.elementFromPoint(d3.event.x, d3.event.y);
var e2 = document.createEvent('MouseEvent');
e2.initMouseEvent(e.type,e.bubbles,e.cancelable,e.view, e.detail,e.screenX,e.screenY,e.clientX,e.clientY,e.ctrlKey,e.altKey,e.shiftKey,e.metaKey,e.button,e.relatedTarget);
el.dispatchEvent(e2);
this.style.pointerEvents = prev;
}
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
svg.append("circle")
.attr("cy", 150)
.attr("cx", 150)
.attr("r", 150)
.style("fill", "red")
.style("opacity", .2)
.on("click", passThru);
svg.append("circle")
.attr("cy", 200)
.attr("cx", 350)
.attr("r", 150)
.style("fill", "green")
.style("opacity", .2)
.on("click", passThru);
svg.append("circle")
.attr("cy", 300)
.attr("cx", 250)
.attr("r", 150)
.style("fill", "yellow")
.style("opacity", .2)
.on("click", passThru);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment