Skip to content

Instantly share code, notes, and snippets.

@Fil
Last active November 4, 2016 07:53
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 Fil/2c2d73e07a711df017707f68320b569b to your computer and use it in GitHub Desktop.
Save Fil/2c2d73e07a711df017707f68320b569b to your computer and use it in GitHub Desktop.
getBoundingClientRect
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var text = svg.append('g')
.attr("transform", "translate(100,40) scale(0.31)")
.append("text")
.attr("x", 80)
.attr("y", 250)
.attr("dx", "5.35em")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style("font", "300 128px Helvetica Neue")
.text("Hello, getBoundingClientRect!");
var clientrect = text.node().getBoundingClientRect(),
svgrect = svg.node().getBoundingClientRect();
var rect = svg.append("rect")
.attr("x", clientrect.left - svgrect.left)
.attr("y", clientrect.top - svgrect.top)
.attr("width", clientrect.width)
.attr("height", clientrect.height)
.style("fill", "#ccc")
.style("fill-opacity", ".3")
.style("stroke", "#666")
.style("stroke-width", "1.5px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment