The big black rectangle captures mouse events, but sits behind the yellow circles.
Notice how you're able to zoom and pan while the mouse is over the black rectangle. It doesn't work when you're over the circle since its on top of the rectangle.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
</style> | |
<svg width="800" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var radius = 30; | |
var circle1 = {x: 200, y: height /2 } ; | |
var circle2 = {x: 600, y: height /2 } ; | |
//big black box surrounding everything that we can zoom on | |
var rect = svg.append("g") | |
.attr("class", "rect") | |
.append("rect") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "black") | |
.style("pointer-events", "all") ; | |
//something to hold our circles | |
var circle_group = svg.append("g") | |
var circle1 = circle_group.append("circle") | |
.attr("cx", circle1.x) | |
.attr("cy", circle1.y) | |
.attr("r", radius) | |
.attr("fill", "yellow"); | |
var circle2 = circle_group.append("circle") | |
.attr("cx", circle2.x) | |
.attr("cy", circle2.y) | |
.attr("r", radius) | |
.attr("fill", "yellow"); | |
//define zoom behaviour | |
var zoom_handler = d3.zoom() | |
.on("zoom", zoom_actions); | |
zoom_handler(rect); | |
function zoom_actions(){ | |
//get transform parameters | |
var transform = d3.zoomTransform(this); | |
//apply transform parameters to circle | |
circle_group.attr("transform", transform) | |
} | |
</script> |