Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 11, 2012 19: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/3092872 to your computer and use it in GitHub Desktop.
Save roundrobin/3092872 to your computer and use it in GitHub Desktop.
just another inlet to tributary
width = 527
height = 405
rect = g.append("rect").attr("width",width).attr("height",height).attr("fill","grey")
text = g.append("text").text("Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?")
.attr("dominant-baseline","text-before-edge")
wrapTextToBoxWidth = (box,text,line_height = 22,padding=90) =>
width = parseInt(box.attr("width"))
height = parseInt(box.attr("height"))
text_string = text.text()
text.text("") #Empty the old text
test_text_container = box.append("text").attr("fill", "none")
textArray = text_string.split(" ")
tspan = text.append("tspan")
tspan_copy = test_text_container.append("tspan")
textSoFar = undefined
for i of textArray
textLength = tspan.node().getComputedTextLength()
if textLength >= (width - padding)
tspan = text.append("tspan").attr("dy", line_height).attr("x", 0)
else
textSoFar = tspan.text()
tspan_copy.text textSoFar + " " + textArray[i]
textLength_of_check_container = tspan_copy.node().getComputedTextLength()
if textLength_of_check_container > width
tspan = text.append("tspan").attr("dy", line_height).attr("x", 0)
textSoFar = tspan.text()
tspan.text textSoFar + " " + textArray[i]
tspan_copy.text ""
else
textSoFar = tspan.text()
tspan.text textSoFar + " " + textArray[i]
test_text_container.remove()
return text
wrapTextToBoxWidth(rect, text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment