-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.min.js
5 lines (5 loc) · 9.06 KB
/
path.min.js
1
2
3
4
5
/*! path - v0.1 - 2012-10-24
* http://github.com/bseth99/pathjs
* Copyright (c) 2012 Ben Olson (http://bseth99.github.com/)
* Licensed under the MIT License. */
(function(e){function n(e){var t=[],n=0,r=e.length;for(;n<r;n++)t.push({x:e[n].x,y:e[n].y});return t}function r(e,t){var n,r,i=e.length;return t?typeof t=="string"?t.indexOf("%")>0?n=e[Math.floor(parseInt(t,10)/100*i)]:t=="start"?n=e[0]:t=="middle"?n=e[Math.floor(i/2)]:t=="end"&&(n=e[i-1]):n=e[t]:n={x:0,y:0},n||(n=e[0]),n}function s(e,t,n,r,i){var i=i||5,s=0,o=Math.abs(n-e),u=e<n?1:-1,a=-Math.abs(r-t),f=t<r?1:-1,l=o+a,c,h=[];for(;;){s++%i==0&&h.push({x:e,y:t});if(e==n&&t==r)break;c=2*l,c>=a&&(l+=a,e+=u),c<=o&&(l+=o,t+=f)}return h}function o(e,t,n,r){var r=r||5,i=0,s=-n,o=0,u=2-2*n,a={q1:[],q2:[],q3:[],q4:[]};do{i++%r==0&&(a.q1.push({x:e-s,y:t+o}),a.q2.push({x:e-o,y:t-s}),a.q3.push({x:e+s,y:t-o}),a.q4.push({x:e+o,y:t+s})),n=u,n<=o&&(u+=++o*2+1);if(n>s||u>o)u+=++s*2+1}while(s<0);return[].concat(a.q1,a.q2,a.q3,a.q4)}function u(e,t,n,r,i){var i=i||5,s=0,o=Math.abs(n-e),u=Math.abs(r-t),a=u&1,f=4*(1-o)*u*u,l=4*(a+1)*o*o,c=f+l+a*o*o,h,p={q1:[],q2:[],q3:[],q4:[]};e>n&&(e=n,n+=o),t>r&&(t=r),t+=(u+1)/2,r=t-a,o*=8*o,a=8*u*u;do{s++%i==0&&(p.q1.push({x:n,y:t}),p.q2.push({x:e,y:t}),p.q3.push({x:e,y:r}),p.q4.push({x:n,y:r})),h=2*c,h<=l&&(t++,r--,c+=l+=o);if(h>=f||2*c>l)e++,n--,c+=f+=a}while(e<=n);while(t-r<u)s++%i==0&&(p.q1.push({x:n+1,y:t++}),p.q2.push({x:e-1,y:t}),p.q3.push({x:e-1,y:r}),p.q4.push({x:n+1,y:r--}));return[].concat(p.q1,p.q2.reverse(),p.q3,p.q4.reverse())}function a(e,t,n){var r=e.x-t.x,i=e.y-t.y,s=t.x-n.x,o=t.y-n.y,u=Math.sqrt(r*r+i*i),a=Math.sqrt(s*s+o*o),f={x:(e.x+t.x)/2,y:(e.y+t.y)/2},l={x:(t.x+n.x)/2,y:(t.y+n.y)/2},c=f.x-l.x,h=f.y-l.y,p=a/(u+a),d={x:l.x+c*p,y:l.y+h*p},v=t.x-d.x,m=t.y-d.y,g={x:f.x+v,y:f.y+m},y={x:l.x+v,y:l.y+m};return{c1:g,c2:y,l1:Math.floor(u),l2:Math.floor(a)}}function f(e,t,n,r,i){var i=i||5,s=a(e,t,n),o=a(t,n,r),u=Math.floor(((s.l1||s.l2)+(o.l2||o.l1))/i),f=t,l=s.c2,c=o.c1,h=n,p=l.x-f.x,d=l.y-f.y,v=c.x-l.x,m=c.y-l.y,g=h.x-c.x,y=h.y-c.y,b=1/(u+1),w=b*b,E=w*b,S=3*b,x=3*w,T=6*w,N=6*E,C=f.x-l.x*2+c.x,k=f.y-l.y*2+c.y,L=(l.x-c.x)*3-f.x+h.x,A=(l.y-c.y)*3-f.y+h.y,O=f,M=(l.x-f.x)*S+C*x+L*E,_=(l.y-f.y)*S+k*x+A*E,D=C*T+L*N,P=k*T+A*N,H=L*N,B=A*N,j=[];while(u--)O={x:O.x+M,y:O.y+_},M+=D,_+=P,D+=H,P+=B,j.push(O);return j.push({x:h.x,y:h.y}),j}var t=Math.PI/180,i=function(e){arguments.length&&(this.points=[].concat(e))};i.prototype={points:[],current:0,box:null,append:function(e,t){var t=t||!1,e=e&&e.points?e:new i(e),r,s;return t&&(r=this.last(),s=e.first(),e.move(r.x-s.x,r.y-s.y)),this.points=this.points.concat(n(e.points)),this.box=null,this},appendTo:function(e,t){return e.append.call(e,this,t),this},prepend:function(e,t){var t=t||!1,e=e&&e.points?e:new i(e),r,s;return t&&(r=this.first(),s=e.last(),e.move(r.x-s.x,r.y-s.y)),this.points=e.points.concat(n(this.points)),this.box=null,this},prependTo:function(e,t){return e.prepend.call(e,this,t),this},reverse:function(){return this.points.reverse(),this},move:function(e,t,n){var i=this.points.length,s=0,o,n=(n&&n.x?n:r(this.points,n))||{x:0,y:0};for(;s<i;s++)o={x:this.points[s].x-n.x,y:this.points[s].y-n.y},this.points[s].x=n.x+o.x+e,this.points[s].y=n.y+o.y+t;return this.box=null,this},scale:function(e,t,n){var i=this.points.length,s=0,o,n=(n&&n.x?n:r(this.points,n))||{x:0,y:0};for(;s<i;s++)o={x:this.points[s].x-n.x,y:this.points[s].y-n.y},this.points[s].x=n.x+o.x*e,this.points[s].y=n.y+o.y*t;return this.box=null,this},skew:function(e,n,i){var s=this.points.length,o=0,u,i=(i&&i.x?i:r(this.points,i))||{x:0,y:0},a=[[1,Math.tan(e*t)],[Math.tan(n*t),1]];for(;o<s;o++)u={x:this.points[o].x-i.x,y:this.points[o].y-i.y},this.points[o].x=i.x+u.x*a[0][0]+u.y*a[0][1],this.points[o].y=i.y+u.x*a[1][0]+u.y*a[1][1];return this.box=null,this},rotate:function(e,n){var i=this.points.length,s=0,o,n=(n&&n.x?n:r(this.points,n))||{x:0,y:0},u=[[Math.cos(e*t),-Math.sin(e*t)],[Math.sin(e*t),Math.cos(e*t)]];for(;s<i;s++)o={x:this.points[s].x-n.x,y:this.points[s].y-n.y},this.points[s].x=n.x+o.x*u[0][0]+o.y*u[0][1],this.points[s].y=n.y+o.x*u[1][0]+o.y*u[1][1];return this.box=null,this},wrap:function(e){var t=this.points.length,n=this.width(),r=e.center(),i=e.first(),s=this.first(),s={x:s.x,y:s.y},o,u,a,f,l,c,h,p,d=0;for(;d<t;d++)a=this.points[d].x-s.x,f=this.points[d].y-s.y,h=e.step(a/n),c=h.x-r.x,l=h.y-r.y,p=Math.sqrt(c*c+l*l),o=c*f/p,u=l*f/p,this.points[d].x=o+h.x,this.points[d].y=u+h.y;return this.box=null,this},getBoundingBox:function(){var e=this.points.length,t=0;if(!this.box){this.box={x1:999999,y1:999999,x2:-999999,y2:-999999};for(;t<e;t++)this.box.x1=Math.min(this.box.x1,this.points[t].x),this.box.y1=Math.min(this.box.y1,this.points[t].y),this.box.x2=Math.max(this.box.x2,this.points[t].x),this.box.y2=Math.max(this.box.y2,this.points[t].y)}return this.box},height:function(){return this.getBoundingBox(),this.box.y2-this.box.y1},width:function(){return this.getBoundingBox(),this.box.x2-this.box.x1},top:function(){return this.getBoundingBox(),this.box.y1},left:function(){return this.getBoundingBox(),this.box.x1},center:function(){return this.getBoundingBox(),{x:(this.box.x1+this.box.x2)/2,y:(this.box.y1+this.box.y2)/2}},bottom:function(){return this.getBoundingBox(),this.box.y2},right:function(){return this.getBoundingBox(),this.box.x2},duplicate:function(){return new i(n(this.points))},first:function(){return this.current=0,this.points[0]},last:function(){return this.current=this.points.length-1,this.points[this.current]},next:function(){return this.current<this.points.length-1?this.points[++this.current]:null},prev:function(){return this.current>0?this.points[--this.current]:null},each:function(e){var t=0,n=this.points.length;for(;t<n;t++)e(t,this.points[t]);return this},closest:function(){},step:function(e){var t=e>1?e-Math.floor(e):e,n=t<0?1-t:t,r=Math.floor((this.points.length-1)*n);return this.points[r]}},PATH=function(e){var t=new i,n,r=0,s=e.length,o;for(;r<s;r++){o=e[r];if(!PATH.generator[o.fn])throw"Path generator "+func+" does not exist";o.start=n||{},n=t.append(PATH.generator[o.fn].call(this,o)).last()}return t},PATH.generator={start:function(e){return{x:e.x,y:e.y}},line:function(e){var t=e.start.x||0,n=e.start.y||0,r=e.x,i=e.y,o=s(t,n,r,i,e.density||3);return o},rectangle:function(e){var t=e.density||5,n=e.start.x||0,r=e.start.y||0,i=e.w,s=e.h,o=e.cornerRadius||0,u=PATH([{fn:"start",x:n,y:r}]),a,f,l,c,h;o.length||(o=[o,o,o,o]),a=o.map(function(e,t){return PATH([{fn:"circle",radius:e,arc:{start:0,end:90},density:1}]).rotate((t-2)*90)}),f=o.map(function(e,t){return t%2==0?PATH([{fn:"line",x:i-o[t]-o[(t+1)%4],y:0,density:1}]):PATH([{fn:"line",x:0,y:s-o[t]-o[(t+1)%4],density:1}])}),u.append(a[0],!0).append(f[0],!0).append(a[1],!0).append(f[1],!0).append(a[2],!0).append(f[2].reverse(),!0).append(a[3],!0).append(f[3].reverse(),!0),c=[],h=u.points.length;for(l=0;l<h;l++)(l%t==0||l==h-1)&&c.push(u.points[l]);return c},circle:function(e){var t=e.start.x||0,n=e.start.y||0,r=e.radius,i=u(t-r*2,n-r,t,n+r,e.density||3),s=0,o=i.length,a;return e.arc&&(s=Math.round(e.arc.start/360*o),o=Math.round(e.arc.end/360*o),i=i.slice(s,o),a={dx:t-i[0].x,dy:n-i[0].y},i=i.map(function(e){return{x:e.x+a.dx,y:e.y+a.dy}})),i},ellipse:function(e){var t=e.start.x||0,n=e.start.y||0,r=e.a,i=e.b,s=u(t-r*2,n-i,t,n+i,e.density||3),o=0,a=s.length,f;return e.arc&&(o=Math.floor(e.arc.start/360*a),a=Math.ceil(e.arc.end/360*a),s=s.slice(o,a),f={dx:t-s[0].x,dy:n-s[0].y},s=s.map(function(e){return{x:e.x+f.dx,y:e.y+f.dy}})),s},wave:function(e){var t=e.start.x||0,n=e.start.y||0,r=e.density||5,i=e.interpolate||!0,o=e.smooth||!1,u=1,a=e.length,f=e.frequency/2,l=e.amplitude,c=t+f/2,h={x:t,y:n},p=[],d;(!i||!!o)&&p.push({x:t,y:n});while(c<t+a)d={x:Math.round(c),y:Math.round(n+l*u)},i&&!o?p=p.concat(s(h.x,h.y,d.x,d.y,r)):p.push(d),u*=-1,c+=f,h=d;return i&&!o?p=p.concat(s(h.x,h.y,Math.round(c-f/2),n,r)):p.push({x:c-f/2,y:n}),o&&(p=PATH.generator.bezier({start:p[0],points:p.slice(1,p.length),density:r})),p},bezier:function(e){var t=[{x:e.start.x||0,y:e.start.y||0}].concat(e.points),n=e.density||5,r=1,i=t.length,s=[];t.unshift(t[1]),t.push(t[i-1]);while(r<i)s=s.concat(f.apply(this,t.slice(r-1,r+3).concat([n]))),r++;return s}},PATH.adapter={animateNode:function(e){return function(t,n){var r=e.step(n.pos);n.elem.style.top=r.y+"px",n.elem.style.left=r.x+"px"}},animatePlot:function(e){return function(t,n){var r=e.step(n.pos),i=r.x,s=r.y,o;o=n.elem.getContext("2d"),o.fillStyle="black",o.strokeStyle="black",o.lineWidth=1,o.beginPath(),o.moveTo(i,s),o.arc(i,s,1,0,2*Math.PI,!0),o.fill(),o.stroke()}},drawCanvas:function(e,t,n){var r=e.getContext("2d"),i=t.first(),n=n||{},s=n.fillStyle||"none";r.fillStyle=s,r.strokeStyle=n.strokeStyle||"black",r.lineWidth=n.lineWidth||1,r.beginPath(),r.moveTo(i.x,i.y);while(i=t.next())r.lineTo(i.x,i.y);s!="none"&&(r.closePath(),r.fill()),r.stroke()},drawSVG:function(e,t,n){var r=t.first(),n=n||{},i="",s,o;n.fill=n.fillStyle||"none",n.stroke=n.strokeStyle||"black",n.lineWidth=n.lineWidth||"1";do i+=Math.round(r.x)+","+Math.round(r.y)+" ";while(r=t.next());o=document.createElementNS("http://www.w3.org/2000/svg","polygon"),o.setAttribute("points",i);for(s in n)o.style[s]=n[s];return e.appendChild(o),o}},PATH.version="0.1"})();