Created
September 14, 2017 03:00
-
-
Save sebg/fc7ba54aaf95447e14b60b01c2e679ff to your computer and use it in GitHub Desktop.
An example that uses the SVG DOM function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
</head> | |
<body> | |
<h1> | |
An example that uses the SVG DOM function <a href="http://www.w3.org/TR/SVG11/types.html#__svg__SVGLocatable__getBBox">getBBox()</a> | |
</h1> | |
<p> | |
To show what happens when an SVG object (circle in this case) is outside of the SVG "viewport" | |
<br> | |
Note that the key here is putting everything in a <g> element which we can then use the getBBox() on the specific node. | |
</p> | |
<hr> | |
<div id="contains-inside-view-port"> | |
<svg width="100" height="100"> | |
<g id="inside-view-port"> | |
<circle cx="15" cy="15" r="10"></circle> | |
<circle cx="85" cy="85" r="10"></circle> | |
</g> | |
</svg> | |
<p> | |
SVG width: 100 height: 100 | |
</p> | |
</div> | |
<hr> | |
<div id="contains-outside-view-port"> | |
<svg width="100" height="100"> | |
<g id="outside-view-port"> | |
<circle cx="-5" cy="-5" r="25"></circle> | |
<circle cx="105" cy="105" r="25"></circle> | |
</g> | |
</svg> | |
<p> | |
SVG width: 100 height: 100 | |
</p> | |
</div> | |
<hr> | |
<script> | |
// Get Bounding Boxes | |
var insideViewPortBBox = d3.select("#inside-view-port").node().getBBox(); | |
var outsideViewPortBBox = d3.select("#outside-view-port").node().getBBox(); | |
// Print out BBox sizes | |
d3.select("#contains-inside-view-port") | |
.append("p") | |
.html("SVG Bounding Box => SVGRect {" + | |
"x: " + insideViewPortBBox.x + | |
", y: " + insideViewPortBBox.y + | |
", width: " + insideViewPortBBox.width + | |
", height: " + insideViewPortBBox.height + | |
"}"); | |
d3.select("#contains-outside-view-port") | |
.append("p") | |
.html("SVG Bounding Box => SVGRect {" + | |
"x: " + outsideViewPortBBox.x + | |
", y: " + outsideViewPortBBox.y + | |
", width: " + outsideViewPortBBox.width + | |
", height: " + outsideViewPortBBox.height + | |
"}"); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment