-
Notifications
You must be signed in to change notification settings - Fork 11
/
d3.sketchy.min.js
4 lines (4 loc) · 4.27 KB
/
d3.sketchy.min.js
1
2
3
4
/*global d3:false *//*jshint unused:false*//**
* Initiate the sketchy library
* @constructor
*/var d3sketchy=function(){function t(t){e=n(e,t);return e}function n(e,t){var n={},r;for(r in e)n[r]=e[r];for(r in t)n[r]=t[r];return n}function r(e,t){return Math.random()*(t-e)+e}function i(){}function s(e,t,n,i,s,o,u){var a=.551915024494,f=Math.atan(a),l=Math.sqrt(a*a+1),c=e,h=r(i,s)*Math.PI/180,p="M";p+=[c*Math.sin(h),c*Math.cos(h)];p+=" C"+[l*c*Math.sin(h+f),l*c*Math.cos(h+f)];for(var d=0;d<4;d++){h+=Math.PI/2*(1+r(o,u));c*=1+r(t,n);p+=" "+(d?"S":"")+[l*c*Math.sin(h-f),l*c*Math.cos(h-f)];p+=" "+[c*Math.sin(h),c*Math.cos(h)]}return p}function o(e,t,n,i){var s=r(n,i);return"rotate("+s+")"+"scale(1,"+r(e,t)+")"}var e={x:0,y:0,width:20,height:20,sketch:1,density:1,radius:10,angle:45,count:2,shape:"circle",clip:"",margin:2};i.drawLine=function(e){for(var t=1;t<3;t++){var n=r(.2,.8),i=e.x1+(e.x2-e.x1)*n+r(-1,1),s=e.y1+(e.y2-e.y1)*n+r(-1,1),o=n+r(-0.3,-0.2),u=e.x1+(e.x2-e.x1)*o+r(-1,1),a=e.y1+(e.y2-e.y1)*o+r(-1,1);e.svg.append("path").attr("d","M"+(e.x1+r(-1,0)*e.sketch.x/t)+" "+(e.y1+r(-1,1)*e.sketch.y/t)+" Q"+u+" "+a+" "+i+" "+s+" T"+(e.x2+r(0,1)*e.sketch.x/t)+" "+(e.y2+r(-1,1)*e.sketch.y/t))}};i.circleFill=function(t){var r=n(e,t),s=r.svg.append("g").attr("transform","translate("+r.x+" "+r.y+") rotate("+r.angle+")"),o=0;while(o>-2*t.r){var u;r.shape==="cut"?u=Math.sqrt(Math.pow(r.r,2)-Math.pow(r.r-Math.abs(o),2)):r.shape==="star"?u=r.r-Math.sqrt(Math.pow(r.r,2)-Math.pow(r.r-Math.abs(o),2)):u=Math.sqrt(Math.pow(r.r,2)-Math.pow(r.r-Math.abs(o),2));i.drawLine({svg:s,x1:-u,y1:o+r.r,x2:u,y2:o+r.r,sketch:{x:r.density*r.sketch,y:r.density*r.sketch}});o-=r.density}return s};i.rectFill=function(e){var t=e.svg.append("g").attr("transform","translate("+e.x+" "+e.y+")");e.svg=t;return i.drawPattern(e)};i.drawPattern=function(e){var t=e.svg,n=e.angle;while(n>360)n-=360;n>180&&(n-=180);var r=Math.PI/180*(90-n),s={y:1,x:-1/Math.tan(r)},o=e.density,u,a,f,l,c,h,p,d,v=e.x,m=e.y;if(Math.abs(n)===90)while(m<e.y+e.height){i.drawLine({svg:t,x1:v,y1:m,x2:v+e.width,y2:m,sketch:{x:o*e.sketch,y:o*e.sketch}});m+=o}else if(Math.abs(n)===180||n===0)while(v<e.x+e.width){i.drawLine({svg:t,x1:v,y1:m,x2:v,y2:m+e.height,sketch:{x:o*e.sketch,y:o*e.sketch}});v+=o}else if(n<90){p=Math.abs(o/Math.sin(Math.PI/180*n));d=Math.abs(o/Math.sin(Math.PI/180*(90-n)));m+=p;c=e.y;h=e.x;while(c<e.y+e.height){l=e.width/s.x;h=e.width+v;c=m+s.y*l;f=m;a=v;if(c<e.y){u=(m-e.y)/s.y;h=v+Math.abs(s.x)*u;c=e.y}else if(m>e.y+e.height){f=e.y+e.height;u=(f-c)/s.y;a=v+e.width-u*Math.abs(s.x)}i.drawLine({svg:t,x1:a,y1:f,x2:h,y2:c,sketch:{x:d*e.sketch,y:p*e.sketch}});m+=p}}else{p=Math.abs(o/Math.sin(Math.PI/180*n));d=Math.abs(o/Math.sin(Math.PI/180*(180-n)));m=e.y+e.height;m-=p;c=e.y+e.height;h=e.x;while(c>e.y){l=e.width/s.x;h=e.width+v;c=m+s.y*l;f=m;a=v;if(c>e.y+e.height){u=(m-(e.y+e.height))/s.y;h=v+Math.abs(s.x*u);c=e.y+e.height}else if(m<e.y){f=e.y;u=(f-c)/s.y;a=v+e.width-Math.abs(u*s.x)}i.drawLine({svg:t,x1:a,y1:f,x2:h,y2:c,sketch:{x:d*e.sketch,y:p*e.sketch}});m-=p}}return t};i.fill=function(t){var r=n(e,t),s=r.svg.append("g").attr("clip-path","url(#"+r.clip+")"),o=d3.select("#"+r.clip).node().getBBox();r.x=o.x-r.margin;r.y=o.y-r.margin;r.width=o.width+2*r.margin;r.height=o.height+2*r.margin;r.svg=s;return i.drawPattern(r)};i.alterPath=function(t){var i=n(e,t),s=[];for(var o=0;o<i.count;o++){var u=[];for(var a=0;a<i.path.length;a++)u.push({x:i.path[a].x+r(-1,1)*i.sketch/(o+1),y:i.path[a].y+r(-1,1)*i.sketch/(o+1)});s.push(u)}return s};i.pathStroke=function(e){var t=i.alterPath(e),n=e.svg.append("g");for(var r=0;r<t.length;r++)for(var s=0;s<t[r].length;s++){var o=t[r][s].x,u=t[r][s].y,a,f;if(s<t[r].length-1){a=t[r][s+1].x;f=t[r][s+1].y}else{a=t[r][0].x;f=t[r][0].y}i.drawLine({svg:n,x1:o,y1:u,x2:a,y2:f,sketch:{x:e.sketch,y:e.sketch}})}return n};i.circleStroke=function(t){var r=n(e,t),i=r.svg.append("g").attr("transform",function(){return"translate("+r.x+" "+r.y+") "+o(1,1,0,360)});for(var u=0;u<r.count;u++)i.append("path").attr("d",function(){return s(r.r,r.sketch/-50/(u+1),r.sketch/10/(u+1),200,240,0,r.sketch/5/(u+1))});return i};i.rectStroke=function(t){var r=n(e,t),s=r.svg.append("g"),o=[{x:r.x,y:r.y},{x:r.x+r.width,y:r.y},{x:r.x+r.width,y:r.y+r.height},{x:r.x,y:r.y+r.height}];return i.pathStroke({svg:s,path:o,count:r.count,sketch:r.sketch})};return i};