Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 29, 2012 10:16
Show Gist options
  • Save roundrobin/3197265 to your computer and use it in GitHub Desktop.
Save roundrobin/3197265 to your computer and use it in GitHub Desktop.
just another inlet to tributary
var width = 403
var height = 162
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("Hellllloooo")
.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 between(x, min, max) {
return x >= min && x <= max;
}
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