Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 11, 2012 22:20
Show Gist options
  • Save roundrobin/3094099 to your computer and use it in GitHub Desktop.
Save roundrobin/3094099 to your computer and use it in GitHub Desktop.
just another inlet to tributary
width = 527
height = 145
rect = g.append("rect").attr("width",width).attr("height",height).attr("fill","none")
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")
wrapGroupInMaskWidthFadeOut = (box,selection) =>
height = parseInt(box.attr("height"))
defs = d3.select("svg").append("defs")
gradient = defs.append('linearGradient')
.attr('id','fadeout')
.attr('gradientUnits','userSpaceOnUse')
.attr('x1','0%')
.attr('x2','0%')
.attr('y1','0%')
.attr('y2',height)
gradient.append('stop')
.attr('stop-color',"#FFFFFF")
.attr('offset',80+'%')
gradient.append('stop')
.attr('stop-color',"#FFFFFF")
.attr('offset',85+'%')
gradient.append('stop')
.attr('stop-color',"#000000")
.attr('offset',100+'%')
mask = defs.append("mask").attr("id","fadeOutMask")
mask.append("rect").attr("width",width).attr("height",height).attr("fill","url(#fadeout)")
new_group = g.append("g").attr("mask","url(#fadeOutMask)")
elem = selection.node().cloneNode(true)
selection.remove()
new_group.node().appendChild(elem)
return new_group
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
text = wrapTextToBoxWidth(rect, text)
wrapGroupInMaskWidthFadeOut(rect,text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment