Created
July 11, 2012 18:12
-
-
Save roundrobin/3092106 to your computer and use it in GitHub Desktop.
just another inlet to tributary
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
width = 478 | |
height = 400 | |
line_height = 22 | |
padding_left = 10 | |
padding_right = 10 | |
padding = padding_left + padding_right | |
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?" | |
g.append("text").text(text).attr("y",200).attr("x",30) | |
g.append("rect").attr("width",width).attr("height",200).attr("y",200).attr("x",30).attr("fill","red") | |
var text_container = g.append("text").attr("height",200).attr("y",354).attr("x",30).attr("fill","green") | |
var test_text_container = g.append("text").attr("height",200).attr("y",354).attr("x",30).attr("fill","none") | |
var textArray = text.split(" ") | |
var tspan = text_container.append("tspan") | |
var tspan_copy = test_text_container.append("tspan") | |
var textSoFar; | |
for(i in textArray){ | |
//Get the current length of the text tspan | |
var textLength = tspan.node().getComputedTextLength() | |
//If the line is to long do a line break, otherwise adding a new word | |
if(textLength >= (width-padding)){ | |
tspan = text_container.append("tspan").attr("dy",line_height).attr("dx",-textLength) | |
} else { | |
textSoFar = tspan.text() | |
tspan_copy.text(textSoFar+" "+textArray[i]) | |
var textLength_of_check_container = tspan_copy.node().getComputedTextLength() | |
if(textLength_of_check_container > width) { | |
//This branch is doing a line break | |
tspan = text_container.append("tspan") | |
.attr("dy",line_height) | |
.attr("dx",-textLength) | |
//Adding a new word to the paragraph | |
textSoFar = tspan.text() //Getting old text | |
tspan.text(textSoFar+" "+textArray[i]) //Add new text | |
tspan_copy.text("") | |
}else{ | |
textSoFar = tspan.text() | |
//Adding a new word to the paragraph | |
tspan.text(textSoFar+" "+textArray[i]) | |
} | |
} | |
} //End For Loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment