Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 10, 2012 05:57
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 roundrobin/3081443 to your computer and use it in GitHub Desktop.
Save roundrobin/3081443 to your computer and use it in GitHub Desktop.
just another inlet to tributary
var width = 381
var height = 205
var font_size = 88
var precise = 4
var x = 230
var y = 119
var rect = g.append("rect")
.attr("width",width)
.attr("height",height)
.attr("fill","black")
.attr("x",x)
.attr("y",y)
var text1 = g.append("text")
.attr("fill","red")
.attr("x",x)
.attr("y",y)
.attr("font-size",font_size)
.text("Helllll yeeeahhhhhh")
.style('dominant-baseline','text-before-edge')
.style('font-family','Times')
text = fitTextToBox(rect,text1)
var textHeight = text.node().getBBox().height
var middle = (height/2)
text.attr("dy", middle - (textHeight / 2) )
function fitTextToBox(box,text){
var boxHeight = parseInt(box.attr("height"))
var boxWidth = parseInt(box.attr("width"))
var textWidth = text.node().getComputedTextLength();
var textHeight = text.node().getBBox().height;
var currentFZ = text.attr("font-size")
var aspectRatioBox = boxWidth / boxHeight;
var aspectRatioText = textWidth / textHeight;
var maxWidth = (boxWidth /textWidth )
var maxHeight = (boxHeight /textHeight)
var max = Math.min(maxWidth, maxHeight)
var fontsize = (currentFZ * max)
text.attr("font-size", fontsize)
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment