-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs-compress-test.min.js
1 lines (1 loc) · 6.91 KB
/
js-compress-test.min.js
1
function svgBranchStraight(t,e,n,r,a){var s=svg.group().addClass("branch-g").attr("id",a.id).data(a);s.line(t,e,n,r).stroke({color:"#f06",opacity:.6,width:1}).addClass("branch-path"),s.line(t,e,n,r).stroke({color:"#f06",opacity:0,width:8})}function svgBranchCurved(t,e,n,r,a,s,i,o,h){var d=svg.group().addClass("branch-g").attr("id",h.id).data(h);"story"==h.dimension&&d.addClass("story");var c={"stroke-width":h.depth<3?2:3==h.depth?.7:.5,stroke:h.depth<=3?"#000":"#B92E6C","stroke-dasharray":"level"!=h.dimension?"":"local"==h.value?"7 5":"individual"==h.value?"0 5":""},l="M"+t+" "+e+" C"+n+" "+r+" "+a+" "+s+" "+i+" "+o;d.path(l).attr(svgStyles.normal).attr(c).addClass("branch-path"),"story"==h.dimension&&(d.path(l).attr(svgStyles["transp-sensi"]),d.circle(csize).attr(svgStyles.friut).attr({cx:i,cy:o}),d.circle(csize2).attr(svgStyles["friut-selected"]).addClass("selected").attr({cx:i,cy:o}))}function setup(){createCanvas(100,100).parent("container"),resizeCanvas(cnt.offsetWidth,cnt.offsetHeight)}function setup2(){function t(t){return map(random(1),0,1,-t,t)}e=_.find(state.data.children,function(t){return"north-america"==t.props.id});b1=new Branch(e.children,null,len0,t(PI/15),e.props),trees.push(new Tree(createVector(.2*width,.45*height),b1));n=_.find(state.data.children,function(t){return"south-america"==t.props.id});b2=new Branch(n.children,null,len0,t(PI/15),n.props),trees.push(new Tree(createVector(.2*width,.9*height),b2));e=_.find(state.data.children,function(t){return"europe"==t.props.id});b1=new Branch(e.children,null,len0,t(PI/15),e.props),trees.push(new Tree(createVector(.5*width,.45*height),b1));n=_.find(state.data.children,function(t){return"africa"==t.props.id});b2=new Branch(n.children,null,len0,t(PI/15),n.props),trees.push(new Tree(createVector(.5*width,.9*height),b2));var e=_.find(state.data.children,function(t){return"asia"==t.props.id});b1=new Branch(e.children,null,len0,t(PI/15),e.props),trees.push(new Tree(createVector(.8*width,.45*height),b1));var n=_.find(state.data.children,function(t){return"oceania"==t.props.id});b2=new Branch(n.children,null,len0,t(PI/15),n.props),trees.push(new Tree(createVector(.8*width,.9*height),b2)),state.p5setupDone=!0}function draw(){console.log("loop n "+frameCount),state.loadingData||(state.p5setupDone||setup2(),trees.forEach(function(t){t.display()}))}function Tree(t,e){this.root=t,this.branches=[],this.storyCount=countChildren(e,0),this.addBranch=function(t){t.attachTo(this),this.branches.push(t)},this.addBranch(e),this.display=function(){for(var t=0,e=0;e<this.branches.length;e++)this.branches[e].branch(),tf.push(),tf.translate(this.root.x,this.root.y),this.branches[e].display(),tf.pop(),this.branches[e].drawn||t++;0==t&&(addListeners(),noLoop())}}function Branch(t,e,n,r,a){this.tree=null,this.children=t,this.start=null!=e?e.copy():createVector(0,0),this.len=n,this.angle=r,this.props=a,this.dir=createVector(0,1),this.dir.mult(this.len),this.dir.rotate(this.angle),this.end=p5.Vector.sub(this.start,this.dir),this.branched=!1,this.drawn=!1,this.attachTo=function(t){this.tree=t},this.branch=function(){if(!this.branched&&this.children.length>0){var t=[],e=countChildren(this),n=0;this.children.forEach(function(r){var a=countChildren(r)/e,s=n+a/2;t.push(s),n+=a});for(var r=0;r<this.children.length;r++){var a=this.children[r].props.dimension,s=(s="area"==a?.2*len0:"level"==a?1.6*len0:"sector"==a?.5*len0:.4*len0)*map(random(),0,1,.9,1.1),i=.5*PI,o=-i/2+i*t[r],h=this.angle+o,d=new Branch(this.children[r].children,this.end.copy(),s,h,this.children[r].props);this.tree.addBranch(d)}this.branched=!0}},this.display=function(){if(!this.drawn){var t=PI/15,e=p5.Vector.sub(this.end,this.start),n=p5.Vector.sub(this.start,this.end),r=random()>.5?t:-t,a=r!=t?t:-t,s=e.rotate(r).mult(.3).add(this.start),i=n.rotate(a).mult(.3).add(this.end);svgBranchCurved(tf.x+this.start.x,tf.y+this.start.y,tf.x+s.x,tf.y+s.y,tf.x+i.x,tf.y+i.y,tf.x+this.end.x,tf.y+this.end.y,this.props),this.drawn=!0}}}function countChildren(t,e){if(currentSum=e||0,0==t.children.length)return 1;var n=0;return t.children.forEach(function(t){n+=countChildren(t,currentSum)}),n+currentSum}function addListeners(){$("svg .branch-g").mouseover(function(t){this.instance.find(".selected").attr({opacity:1}),state.logp.innerText=this.dataset.value.toUpperCase()}),$("svg .branch-g").mouseout(function(t){this.instance.find(".selected").attr({opacity:0}),state.logp.innerText=""}),$("svg .branch-g").click(function(t){t.stopPropagation(),console.log(this.dataset.id)})}function setupLogP(){state.logp=document.createElement("p"),state.logp.classList.add("log"),document.body.appendChild(state.logp),state.logp.innerText="loading data"}function loadDataNew(t,e,n){var r={};$.getJSON(t,function(t){for(var n=0;n<t.length;n++){var a=t[n],s=[];e.forEach(function(t,e){s.push(a[t].slug())}),e.forEach(function(t,e){s.push(a[t].slug())});var i=a.area.slug(),o=a["level-simplified"].slug(),h=a.sector.slug();r.hasOwnProperty(i)||(r[i]={}),r[i].hasOwnProperty(o)||(r[i][o]={}),r[i][o].hasOwnProperty(h)||(r[i][o][h]=[]),r[i][o][h].push(a)}console.log("middata",r),fullTreeData={children:[],props:{id:"root",depth:0,class:"branch-middle"}}})}function loadData(t,e){var n={};$.getJSON(t,function(t){for(h=0;h<t.length;h++){var r=t[h],a=r.area.slug(),s=r["level-simplified"].slug(),i=r.sector.slug();n.hasOwnProperty(a)||(n[a]={}),n[a].hasOwnProperty(s)||(n[a][s]={}),n[a][s].hasOwnProperty(i)||(n[a][s][i]=[]),n[a][s][i].push(r)}console.log("middata",n),fullTreeData={children:[],props:{id:"root",depth:0,class:"branch-middle"}};for(var o=Object.keys(n),h=0;h<o.length;h++){var a=o[h],d=[];fullTreeData.children.push({children:d,props:{id:a,depth:1,dimension:"area",value:a,class:"branch-middle"}});for(var c=Object.keys(n[a]),l=0;l<c.length;l++){var s=c[l],p=[];d.push({children:p,props:{id:a+"---"+s,depth:2,dimension:"level",value:s,class:"branch-middle"}});for(var u=Object.keys(n[a][s]),f=0;f<u.length;f++){var i=u[f],g=n[a][s][i].map(function(t,e){return{children:[],props:$.extend({},t,{id:a+"---"+s+"---"+i+"---"+e,depth:4,dimension:"story",value:s+" story about "+i+" in "+a+" - "+e,class:"branch-story"})}});p.push({children:g,props:{id:a+"---"+s+"---"+i,depth:3,dimension:"sector",value:i,class:"branch-middle"}})}}}e(fullTreeData)})}var state={loadingData:!0,data:{},logp:null,p5setupDone:!1,p5Finished:!1};setupLogP(),loadData("data/all.json",function(t){state.data=t,console.log("state.data",state.data),state.loadingData=!1,state.logp.innerText="data loaded."});var cnt=document.getElementById("container"),svg=SVG().addTo("#container").size(cnt.offsetWidth,cnt.offsetHeight).addClass("artboard"),svgStyles={normal:{stroke:"#f06","stroke-width":1,fill:"none","stroke-linecap":"round"},friut:{fill:"#B92E6C",stroke:"#E7E5F1","stroke-width":1},"friut-selected":{fill:"none",stroke:"#B92E6C","stroke-width":.5,opacity:0},hover:{stroke:"#000","stroke-width":2,"fill-opacity":0},"transp-sensi":{stroke:"#000","stroke-opacity":0,"stroke-width":10,fill:"none"}},csize=5,csize2=12,trees=[],tf=new Transformer,len0=70;