Skip to content

Instantly share code, notes, and snippets.

@danner
Forked from mbostock/.block
Last active January 12, 2018 21:28
Show Gist options
  • Save danner/2ac1a12a96b8854b355462b7bb143c32 to your computer and use it in GitHub Desktop.
Save danner/2ac1a12a96b8854b355462b7bb143c32 to your computer and use it in GitHub Desktop.
getBBox
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("text")
.attr("x", 480)
.attr("y", 250)
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style("font", "300 128px Helvetica Neue");
text.append('tspan')
.attr('x', '0')
.text("Hello");
text.append('tspan')
.attr('x', '0')
.attr('dy', '1.1em')
.text("World");
var bbox = text.node().getBBox();
var rect = svg.append("rect")
.attr("x", bbox.x)
.attr("y", bbox.y)
.attr("width", bbox.width)
.attr("height", bbox.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