Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 12, 2012 21:16
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/3101048 to your computer and use it in GitHub Desktop.
Save roundrobin/3101048 to your computer and use it in GitHub Desktop.
just another inlet to tributary
rect = g.append('rect').attr('width',200).attr('height',200)
.attr('fill','red')
text = g.append('text').attr('width',200).attr('height',200)
.text('Hallo das ist ein test du bloeder nuggle verstehtst du das nicht?')
.attr('y',20)
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,box)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment