Skip to content

Instantly share code, notes, and snippets.

@mforando
Created November 15, 2018 16:38
Show Gist options
  • Save mforando/d5befb383a3b6d0e82458f4ac38e28c6 to your computer and use it in GitHub Desktop.
Save mforando/d5befb383a3b6d0e82458f4ac38e28c6 to your computer and use it in GitHub Desktop.
bbBox vs. getBoundingClientRect
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>
<div id="headerDiv">
<h1>Mouse Coords: <span id="mouse" style="color:red"></span></h1>
<h1>bbBox Coords: <span id="bbBox" style="color:red"></span></h1>
<h1>getBoundingClientRect Coords: <span id="clientRect" style="color:red"></span></h1>
</div>
<body>
<script>
d3.select("body").on("mousemove",function(){
console.log(d3.event)
d3.select("#mouse").text("x: " + d3.event.clientX + ", y:" + d3.event.clientY)
})
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
.style("border","1px solid black")
var circle = svg.append("circle")
.attr("cy", 200)
.attr("cx", 120)
.attr("r",10)
.attr("font-size", 36)
.attr("font-family", "monospace")
d3.select("#bbBox").text("x: " + circle.node().getBBox().x + ", y:" + circle.node().getBBox().y)
d3.select("#clientRect").text("x: " + circle.node().getBoundingClientRect().x + ", y:" + circle.node().getBoundingClientRect().y)
console.log(circle.node().getBBox())
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment