1 line
14 KiB
JavaScript
1 line
14 KiB
JavaScript
|
function flatten(t,e){if("string"==typeof t)return t;var n=[];for(e in t){var a=flatten(t[e],e);a&&n.push(a)}return n.join(" ")}function parseText(t){tags={};var e={},n=d3.select("#per-line").property("checked");t.split(n?/\n/g:wordSeparators).forEach(function(t){discard.test(t)||(n||(t=t.replace(punctuation,"")),stopWords.test(t.toLowerCase())||(t=t.substr(0,maxLength),e[t.toLowerCase()]=t,tags[t=t.toLowerCase()]=(tags[t]||0)+1))}),tags=d3.entries(tags).sort(function(t,e){return e.value-t.value}),tags.forEach(function(t){t.key=e[t.key]}),generate()}function generate(){layout.font(d3.select("#font").property("value")).spiral(d3.select("input[name=spiral]:checked").property("value")),fontSize=d3.scale[d3.select("input[name=scale]:checked").property("value")]().range([10,100]),tags.length&&fontSize.domain([+tags[tags.length-1].value||1,+tags[0].value]),complete=0,statusText.style("display",null),words=[],layout.stop().words(tags.slice(0,max=Math.min(tags.length,+d3.select("#max").property("value")))).start()}function progress(t){statusText.text(++complete+"/"+max)}function draw(t,e){statusText.style("display","none"),scale=e?Math.min(w/Math.abs(e[1].x-w/2),w/Math.abs(e[0].x-w/2),h/Math.abs(e[1].y-h/2),h/Math.abs(e[0].y-h/2))/2:1,words=t;var n=vis.selectAll("text").data(words,function(t){return t.text.toLowerCase()});n.transition().duration(1e3).attr("transform",function(t){return"translate("+[t.x,t.y]+")rotate("+t.rotate+")"}).style("font-size",function(t){return t.size+"px"}),n.enter().append("text").attr("text-anchor","middle").attr("transform",function(t){return"translate("+[t.x,t.y]+")rotate("+t.rotate+")"}).style("font-size","1px").transition().duration(1e3).style("font-size",function(t){return t.size+"px"}),n.style("font-family",function(t){return t.font}).style("fill",function(t){return fill(t.text.toLowerCase())}).text(function(t){return t.text});var a=background.append("g").attr("transform",vis.attr("transform")),r=a.node();n.exit().each(function(){r.appendChild(this)}),a.transition().duration(1e3).style("opacity",1e-6).remove(),vis.transition().delay(1e3).duration(750).attr("transform","translate("+[w>>1,h>>1]+")scale("+scale+")")}function downloadPNG(){d3.event.preventDefault();var t=document.createElement("canvas"),e=t.getContext("2d");t.width=w,t.height=h,e.translate(w>>1,h>>1),e.scale(scale,scale),words.forEach(function(t,n){e.save(),e.translate(t.x,t.y),e.rotate(t.rotate*Math.PI/180),e.textAlign="center",e.fillStyle=fill(t.text.toLowerCase()),e.font=t.size+"px "+t.font,e.fillText(t.text,0,0),e.restore()}),echoContentType.attr("value","image/png"),echoInput.attr("value",t.toDataURL("image/png")),echoForm.node().submit()}function downloadSVG(){d3.event.preventDefault(),echoContentType.attr("value","image/svg+xml;charset=utf-8"),echoInput.attr("value",svg.attr("version","1.1").attr("xmlns","http://www.w3.org/2000/svg").node().parentNode.innerHTML),echoForm.node().submit()}!function(t){function e(){function t(t,n,a){for(var r,o,s,l=([{x:0,y:0},{x:e[0],y:e[1]}],n.x),i=n.y,d=Math.sqrt(e[0]*e[0]+e[1]*e[1]),h=m(e),f=Math.random()<.5?1:-1,p=-f;(r=h(p+=f))&&(o=~~r[0],s=~~r[1],!(Math.min(o,s)>d));)if(n.x=l+o,n.y=i+s,!(n.x+n.x0<0||n.y+n.y0<0||n.x+n.x1>e[0]||n.y+n.y1>e[1])&&(!a||!u(n,t,e[0]))&&(!a||c(n,a))){for(var y,g=n.sprite,v=n.width>>5,x=e[0]>>5,w=n.x-(v<<4),M=127&w,b=32-M,z=n.y1-n.y0,k=(n.y+n.y0)*x+(w>>5),T=0;z>T;T++){y=0;for(var A=0;v>=A;A++)t[k+A]|=y<<b|(v>A?(y=g[T*v+A])>>>M:0);k+=x}return delete n.sprite,!0}return!1}var e=[256,256],h=n,p=a,y=r,g=o,v=s,m=d,x=[],w=1/0,b=d3.dispatch("word","end"),z=null,k={};return k.start=function(){function n(){for(var n,s=+new Date;+new Date-s<w&&++u<o&&z;)n=d[u],n.x=e[0]*(Math.random()+.5)>>1,n.y=e[1]*(Math.random()+.5)>>1,l(n,d,u),t(a,n,r)&&(c.push(n),b.word(n),r?i(r,n):r=[{x:n.x+n.x0,y:n.y+n.y0},{x:n.x+n.x1,y:n.y+n.y1}],n.x-=e[0]>>1,n.y-=e[1]>>1);u>=o&&(k.stop(),b.end(c,r))}var a=f((e[0]>>5)*e[1]),r=null,o=x.length,u=-1,c=[],d=x.map(function(t,e){return{text:h.call(this,t,e),font:p.call(this,t,e),rotate:g.call(this,t,e),size:~~y.call(this,t,e),padding:s.call(this,t,e)}}).sor
|