Created
September 19, 2013 02:38
-
-
Save yanofsky/6618496 to your computer and use it in GitHub Desktop.
BBox @font-face issue
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> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<title>Text bbox issue</title> | |
</head> | |
<style type="text/css" media="screen"> | |
@import url(http://fonts.googleapis.com/css?family=Cutive); | |
svg text { | |
font-family: 'Cutive',Helvetica, sans-serif; | |
font-size: 30px; | |
} | |
</style> | |
<body id="" onload=""> | |
<div id="content"> | |
</div> | |
<button id="updateButton">Update!</button> | |
</body> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script type="text/javascript" charset="utf-8"> | |
function transformCoordOf(elem) { | |
var separator = elem.attr("transform").indexOf(",") > -1 ? "," : " "; | |
var trans = elem.attr("transform").split(separator); | |
return { x: (trans[0] ? parseFloat(trans[0].split("(")[1]) : 0), y: (trans[1] ? parseFloat(trans[1].split(")")[0] ): 0) }; | |
} | |
function positionElements() { | |
txts.filter(function(d,i){return i != 0}) | |
.attr("transform",function(d,i){ | |
var prev = d3.select(txts[0][i]), | |
prevWidth = parseFloat(prev.node().getBoundingClientRect().width) | |
prevCoords = transformCoordOf(prev); | |
var cur = d3.select(this), | |
curWidth = parseFloat(cur.node().getBoundingClientRect().width) | |
curCoords = transformCoordOf(cur); | |
var y = prevCoords.y, | |
x = prevCoords.x + prevWidth + 10; | |
return "translate("+x+","+y+")"; | |
}) | |
} | |
var names = ["apples","oranges","bananas"] | |
var canvas = d3.select("#content") | |
.append("svg") | |
.attr("width","600px") | |
.attr("height","100px"); | |
var txts = canvas.selectAll("text").data(names) | |
.enter() | |
.append("text") | |
.attr("transform","translate(10,50)") | |
.text(function(d){return d}); | |
positionElements() | |
d3.select("button").on("click",function(){positionElements()}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment