@antv/util
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).util={})}(this,function(t){"use strict";function n(t){return[parseInt(t.substr(1,2),16),parseInt(t.substr(3,2),16),parseInt(t.substr(5,2),16)]}function r(t){t=Math.round(t).toString(16);return 1===t.length?"0"+t:t}function u(t){return"#"+r(t[0])+r(t[1])+r(t[2])}var a,z=/rgba?\(([\s.,0-9]+)\)/;function e(t){if("#"===t[0]&&7===t.length)return t;a||((n=document.createElement("i")).title="Web Colour Picker",n.style.display="none",document.body.appendChild(n),a=n),a.style.color=t;var n=document.defaultView.getComputedStyle(a,"").getPropertyValue("color");return u(z.exec(n)[1].split(/\s*,\s*/).map(function(t){return Number(t)}))}function i(t,n,r,a){return t[a]+(n[a]-t[a])*r}var N=/^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i,V=/^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i,Z=/[\d.]+:(#[^\s]+|[^)]+\))/gi;var o="undefined"!=typeof Float32Array?Float32Array:Array;function l(t,n,r){var a=n[0],e=n[1],o=n[2],u=n[3],i=n[4],c=n[5],h=n[6],s=n[7],n=n[8],f=r[0],l=r[1],p=r[2],x=r[3],y=r[4],v=r[5],g=r[6],M=r[7],r=r[8];return t[0]=f*a+l*u+p*h,t[1]=f*e+l*i+p*s,t[2]=f*o+l*c+p*n,t[3]=x*a+y*u+v*h,t[4]=x*e+y*i+v*s,t[5]=x*o+y*c+v*n,t[6]=g*a+M*u+r*h,t[7]=g*e+M*i+r*s,t[8]=g*o+M*c+r*n,t}function c(){var t=new o(3);return o!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t}function h(t,n,r){var a=new o(3);return a[0]=t,a[1]=n,a[2]=r,a}Math.hypot||(Math.hypot=function(){for(var t=0,n=arguments.length;n--;)t+=arguments[n]*arguments[n];return Math.sqrt(t)});function s(){var t=new o(4);return o!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t[3]=1,t}c(),f=new o(4),o!=Float32Array&&(f[0]=0,f[1]=0,f[2]=0,f[3]=0);function b(t,n,r){return t[0]=n[0]+r[0],t[1]=n[1]+r[1],t}function A(t,n,r){return t[0]=Math.min(n[0],r[0]),t[1]=Math.min(n[1],r[1]),t}function w(t,n,r){return t[0]=Math.max(n[0],r[0]),t[1]=Math.max(n[1],r[1]),t}function P(t,n,r){return t[0]=n[0]*r,t[1]=n[1]*r,t}function T(t,n){var r=n[0]-t[0],n=n[1]-t[1];return Math.hypot(r,n)}c(),h(1,0,0),h(0,1,0),s(),s(),f=new o(9),o!=Float32Array&&(f[1]=0,f[2]=0,f[3]=0,f[5]=0,f[6]=0,f[7]=0),f[0]=1,f[4]=1,f[8]=1;var f,k=function(t,n,r){return t[0]=n[0]-r[0],t[1]=n[1]-r[1],t};function R(t,n,r){var a,e,o=[0,0,0,0,0,0,0,0,0];a=o,r=r,e=Math.sin(r),r=Math.cos(r),a[0]=r,a[1]=e,a[2]=0,a[3]=-e,a[4]=r,a[5]=0,a[6]=0,a[7]=0,a[8]=1,l(t,o,n)}function p(t,n){return t[0]*n[1]-n[0]*t[1]}f=new o(2),o!=Float32Array&&(f[0]=0,f[1]=0);var H=/[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/gi,E=/[^\s,]+/gi;function C(t){var a=t||[];return Array.isArray(a)?a:"string"==typeof a?((a=a.match(H)).forEach(function(r,t){var n;1<(r=r.match(E))[0].length&&(n=r[0].charAt(0),r.splice(1,0,r[0].substr(1)),r[0]=n),r.forEach(function(t,n){isNaN(t)||(r[n]=+t)}),a[t]=r}),a):void 0}function X(t,n){var o=[],u=[];return t.length&&function t(n,r){if(1===n.length)o.push(n[0]),u.push(n[0]);else{for(var a=[],e=0;e<n.length-1;e++)0===e&&o.push(n[0]),e===n.length-2&&u.push(n[e+1]),a[e]=[(1-r)*n[e][0]+r*n[e+1][0],(1-r)*n[e][1]+r*n[e+1][1]];t(a,r)}}(t,n),{left:o,right:u.reverse()}}function Y(t,n,r){if(1===r)return[[].concat(t)];var a=[];if("L"===n[0]||"C"===n[0]||"Q"===n[0])a=a.concat(function(t,n,r){for(var t=[[t[1],t[2]]],a=(r=r||2,[]),e=("A"===n[0]?(t.push(n[6]),t.push(n[7])):"C"===n[0]?(t.push([n[1],n[2]]),t.push([n[3],n[4]]),t.push([n[5],n[6]])):"S"===n[0]||"Q"===n[0]?(t.push([n[1],n[2]]),t.push([n[3],n[4]])):t.push([n[1],n[2]]),t),o=1/r,u=0;u<r-1;u++){var i=X(e,o/(1-o*u));a.push(i.left),e=i.right}return a.push(e),a.map(function(t){var n=[];return 4===t.length&&(n.push("C"),n=n.concat(t[2])),3<=t.length&&(3===t.length&&n.push("Q"),n=n.concat(t[1])),2===t.length&&n.push("L"),n=n.concat(t[t.length-1])})}(t,n,r));else{var e=[].concat(t);"M"===e[0]&&(e[0]="L");for(var o=0;o<=r-1;o++)a.push(e)}return a}var B=function t(n,r){if(n===r)return!0;if(n&&r&&"object"==typeof n&&"object"==typeof r){if(n.constructor!==r.constructor)return!1;var a,e,o;if(Array.isArray(n)){if((a=n.length)!=r.length)return!1;for(e=a;0!=e--;)if(!t(n[e],r[e]))return!1;return!0}if(n.constructor===RegExp)return n.source===r.source&&n.flags===r.flags;if(n.valueOf!==Object.prototype.valueOf)return n.valueOf()===r.valueOf();if(n.toString!==Object.prototype.toString)return n.toString()===r.toString();if((a=(o=Object.keys(n)).length)!==Object.keys(r).length)return!1;for(e=a;0!=e--;)if(!Object.prototype.hasOwnProperty.call(r,o[e]))return!1;for(e=a;0!=e--;){var u=o[e];if(!t(n[u],r[u]))return!1}return!0}return n!=n&&r!=r};function x(t){var n=[];switch(t[0]){case"M":case"L":n.push([t[1],t[2]]);break;case"A":n.push([t[6],t[7]]);break;case"Q":n.push([t[3],t[4]]),n.push([t[1],t[2]]);break;case"T":n.push([t[1],t[2]]);break;case"C":n.push([t[5],t[6]]),n.push([t[1],t[2]]),n.push([t[3],t[4]]);break;case"S":n.push([t[3],t[4]]),n.push([t[1],t[2]]);break;case"H":case"V":n.push([t[1],t[1]])}return n}function y(t,n,r){for(var a,e=[].concat(t),o=1/(r+1),u=x(n)[0],i=1;i<=r;i++)o*=i,0===(a=Math.floor(t.length*o))?e.unshift([u[0]*o+t[a][0]*(1-o),u[1]*o+t[a][1]*(1-o)]):e.splice(a,0,[u[0]*o+t[a][0]*(1-o),u[1]*o+t[a][1]*(1-o)]);return e}function v(t,n,r,a,e){return e?[["M",+t+ +e,n],["l",r-2*e,0],["a",e,e,0,0,1,e,e],["l",0,a-2*e],["a",e,e,0,0,1,-e,e],["l",2*e-r,0],["a",e,e,0,0,1,-e,-e],["l",0,2*e-a],["a",e,e,0,0,1,e,-e],["z"]]:[["M",t,n],["l",r,0],["l",0,a],["l",-r,0],["z"]]}var G=new RegExp("([a-z])[\\s,]*((-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?[\\s]*,?[\\s]*)+)","ig"),_=new RegExp("(-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?)[\\s]*,?[\\s]*","ig");function d(t){var o,u;return"string"==typeof t?t?(o={a:7,c:6,o:2,h:1,l:2,m:2,r:4,q:4,s:4,t:2,v:1,u:3,z:0},u=[],t.replace(G,function(t,n,r){var a=[],e=n.toLowerCase();if(r.replace(_,function(t,n){n&&a.push(+n)}),"m"===e&&2<a.length&&(u.push([n].concat(a.splice(0,2))),e="l",n="m"===n?"l":"L"),"o"===e&&1===a.length&&u.push([n,a[0]]),"r"===e)u.push([n].concat(a));else for(;a.length>=o[e]&&(u.push([n].concat(a.splice(0,o[e]))),o[e]););return""}),u):null:Array.isArray(t)?t:void 0}var D=/[a-z]/;function U(t,n){return[n[0]+(n[0]-t[0]),n[1]+(n[1]-t[1])]}function W(t){var n=d(t);if(!n||!n.length)return[["M",0,0]];for(var r=!1,a=0;a<n.length;a++){var e=n[a][0];if(D.test(e)||0<=["V","H","T","S"].indexOf(e)){r=!0;break}}if(!r)return n;var o=[],u=0,i=0,c=0,h=0,t=0,s=n[0];"M"!==s[0]&&"m"!==s[0]||(c=u=+s[1],h=i=+s[2],t++,o[0]=["M",u,i]);for(var a=t,f=n.length;a<f;a++){var l=n[a],p=o[a-1],x=[],y=(e=l[0]).toUpperCase();if(e!==y)switch(x[0]=y){case"A":x[1]=l[1],x[2]=l[2],x[3]=l[3],x[4]=l[4],x[5]=l[5],x[6]=+l[6]+u,x[7]=+l[7]+i;break;case"V":x[1]=+l[1]+i;break;case"H":x[1]=+l[1]+u;break;case"M":c=+l[1]+u,h=+l[2]+i,x[1]=c,x[2]=h;break;default:for(var v=1,g=l.length;v<g;v++)x[v]=+l[v]+(v%2?u:i)}else x=n[a];switch(y){case"Z":u=+c,i=+h;break;case"H":x=["L",u=x[1],i];break;case"V":x=["L",u,i=x[1]];break;case"T":var u=x[1],i=x[2],M=U([p[1],p[2]],[p[3],p[4]]),x=["Q",M[0],M[1],u,i];break;case"S":u=x[x.length-2],i=x[x.length-1];var M=p.length,m=U([p[M-4],p[M-3]],[p[M-2],p[M-1]]);x=["C",m[0],m[1],x[1],x[2],u,i];break;case"M":c=x[x.length-2],h=x[x.length-1];break;default:u=x[x.length-2],i=x[x.length-1]}o.push(x)}return o}function $(t,n,r,a){var e=t*r+n*a;return(e=1<e?1:e)<-1&&(e=-1),(t*a-n*r<0?-1:1)*Math.acos(e)}function J(t){var n=t.px,r=t.py,a=t.cx,e=t.cy,o=t.rx,u=t.ry,i=void 0===(i=t.xAxisRotation)?0:i,c=void 0===(c=t.largeArcFlag)?0:c,t=void 0===(t=t.sweepFlag)?0:t,h=[];if(0===o||0===u)return[{x1:0,y1:0,x2:0,y2:0,x:a,y:e}];var s=Math.sin(i*F/360),f=Math.cos(i*F/360),l=-s*(n-a)/2+f*(r-e)/2;if(0==(i=f*(n-a)/2+s*(r-e)/2)&&0==l)return[{x1:0,y1:0,x2:0,y2:0,x:a,y:e}];var o=Math.abs(o),u=Math.abs(u),p=Math.pow(i,2)/Math.pow(o,2)+Math.pow(l,2)/Math.pow(u,2);1<p&&(o*=Math.sqrt(p),u*=Math.sqrt(p));var x=(p=tt(n,r,a,e,o,u,c,t,s,f,i,l))[0],y=p[1],v=p[2],g=p[3],n=Math.abs(g)/(F/4),M=(Math.abs(1-n)<1e-7&&(n=1),Math.max(Math.ceil(n),1));g/=M;for(var m=0;m<M;m++)h.push(K(v,g)),v+=g;return h.map(function(t){var n=q(t[0],o,u,f,s,x,y),r=n.x,n=n.y,a=q(t[1],o,u,f,s,x,y),e=a.x,a=a.y,t=q(t[2],o,u,f,s,x,y);return{x1:r,y1:n,x2:e,y2:a,x:t.x,y:t.y}})}var F=2*Math.PI,q=function(t,n,r,a,e,o,u){var i=t.x,t=t.y;return{x:a*(i*=n)-e*(t*=r)+o,y:e*i+a*t+u}},K=function(t,n){var r=1.5707963267948966===n?.551915024494:-1.5707963267948966===n?-.551915024494:4/3*Math.tan(n/4),a=Math.cos(t),e=Math.sin(t),o=Math.cos(t+n),t=Math.sin(t+n);return[{x:a-e*r,y:e+a*r},{x:o+t*r,y:t-o*r},{x:o,y:t}]},tt=function(t,n,r,a,e,o,u,i,c,h,s,f){var l=Math.pow(e,2),p=Math.pow(o,2),x=Math.pow(s,2),y=Math.pow(f,2),v=l*p-l*y-p*x,l=(v<0&&(v=0),v/=l*y+p*x,(v=Math.sqrt(v)*(u===i?-1:1))*e/o*f),y=v*-o/e*s,p=h*l-c*y+(t+r)/2,x=c*l+h*y+(n+a)/2,u=(s-l)/e,v=(f-y)/o,t=(-s-l)/e,r=(-f-y)/o,c=$(1,0,u,v),h=$(u,v,t,r);return 0===i&&0<h&&(h-=F),1===i&&h<0&&(h+=F),[p,x,c,h]};function g(t,n,r,a){return[t,n,r,a,r,a]}function nt(t,n){"TQ".indexOf(t[0])<0&&(n.qx=null,n.qy=null);var r=t.slice(1),a=r[0],e=r[1];switch(t[0]){case"M":return n.x=a,n.y=e,t;case"A":return["C"].concat(function(t,n,r,a,e,o,u,i,c){return J({px:t,py:n,cx:i,cy:c,rx:r,ry:a,xAxisRotation:e,largeArcFlag:o,sweepFlag:u}).reduce(function(t,n){var r=n.x1,a=n.y1,e=n.x2,o=n.y2,u=n.x,n=n.y;return t.push(r,a,e,o,u,n),t},[])}.apply(0,[n.x1,n.y1].concat(t.slice(1))));case"Q":return n.qx=a,n.qy=e,["C"].concat(function(t,n,r,a,e,o){return[1/3*t+2/3*r,1/3*n+2/3*a,1/3*e+2/3*r,1/3*o+2/3*a,e,o]}.apply(0,[n.x1,n.y1].concat(t.slice(1))));case"L":return["C"].concat(g(n.x1,n.y1,t[1],t[2]));case"H":return["C"].concat(g(n.x1,n.y1,t[1],n.y1));case"V":return["C"].concat(g(n.x1,n.y1,n.x1,t[1]));case"Z":return["C"].concat(g(n.x1,n.y1,n.x,n.y))}return t}function S(t,n){void 0===n&&(n=!1);for(var r,a,e=W(t),o={x1:0,y1:0,x2:0,y2:0,x:0,y:0,qx:null,qy:null},u=[],i="",c=e.length,h=[],s=0;s<c;s+=1){e[s]&&(i=e[s][0]),u[s]=i,e[s]=nt(e[s],o),y=x=p=l=f=void 0;var f=e,l=u,p=s;if(7<f[p].length){f[p].shift();for(var x=f[p],y=p;x.length;)l[p]="A",f.splice(y+=1,0,["C"].concat(x.splice(0,6)));f.splice(p,1)}c=e.length,"Z"===i&&h.push(s),a=(r=e[s]).length,o.x1=+r[a-2],o.y1=+r[a-1],o.x2=+r[a-4]||o.x1,o.y2=+r[a-3]||o.y1}return n?[e,h]:e}function rt(t,n,r,a,e){return t*(t*(-3*n+9*r-9*a+3*e)+6*n-12*r+6*a)-3*n+3*r}function at(t,n,r,a,e,o,u,i,c){for(var h=(c=1<(c=null===c?1:c)?1:c<0?0:c)/2,s=[-.1252,.1252,-.3678,.3678,-.5873,.5873,-.7699,.7699,-.9041,.9041,-.9816,.9816],f=[.2491,.2491,.2335,.2335,.2032,.2032,.1601,.1601,.1069,.1069,.0472,.0472],l=0,p=0;p<12;p++){var x=h*s[p]+h,y=rt(x,t,r,e,u),x=rt(x,n,a,o,i);l+=f[p]*Math.sqrt(y*y+x*x)}return h*l}function L(t,n,r){return n>=t.x&&n<=t.x+t.width&&r>=t.y&&r<=t.y+t.height}function I(t,n,r,a){return null===t&&(t=n=r=a=0),null===n&&(n=t.y,r=t.width,a=t.height,t=t.x),{x:t,y:n,width:r,w:r,height:a,h:a,x2:t+r,y2:n+a,cx:t+r/2,cy:n+a/2,r1:Math.min(r,a)/2,r2:Math.max(r,a)/2,r0:Math.sqrt(r*r+a*a)/2,path:v(t,n,r,a),vb:[t,n,r,a].join(" ")}}function et(t,n,r,a,e,o,u,i){return Array.isArray(t)||(t=[t,n,r,a,e,o,u,i]),n=function(t,n,r,a,e,o,u,i){for(var c,h,s,f=[],l=[[],[]],p=0;p<2;++p){var x,y=0===p?(h=6*t-12*r+6*e,c=-3*t+9*r-9*e+3*u,3*r-3*t):(h=6*n-12*a+6*o,c=-3*n+9*a-9*o+3*i,3*a-3*n);Math.abs(c)<1e-12?Math.abs(h)<1e-12||0<(s=-y/h)&&s<1&&f.push(s):(y=h*h-4*y*c,x=Math.sqrt(y),y<0||(0<(y=(-h+x)/(2*c))&&y<1&&f.push(y),0<(y=(-h-x)/(2*c))&&y<1&&f.push(y)))}for(var v,g=f.length,M=g;g--;)s=f[g],l[0][g]=(v=1-s)*v*v*t+3*v*v*s*r+3*v*s*s*e+s*s*s*u,l[1][g]=v*v*v*n+3*v*v*s*a+3*v*s*s*o+s*s*s*i;return l[0][M]=t,l[1][M]=n,l[0][M+1]=u,l[1][M+1]=i,l[0].length=l[1].length=M+2,{min:{x:Math.min.apply(0,l[0]),y:Math.min.apply(0,l[1])},max:{x:Math.max.apply(0,l[0]),y:Math.max.apply(0,l[1])}}}.apply(null,t),I(n.min.x,n.min.y,n.max.x-n.min.x,n.max.y-n.min.y)}function ot(t,n,r,a,e,o,u,i,c){var h=1-c,s=Math.pow(h,3),f=Math.pow(h,2),l=c*c,p=l*c,x=t+2*c*(r-t)+l*(e-2*r+t),y=n+2*c*(a-n)+l*(o-2*a+n),v=r+2*c*(e-r)+l*(u-2*e+r);return{x:s*t+3*f*c*r+3*h*c*c*e+p*u,y:s*n+3*f*c*a+3*h*c*c*o+p*i,m:{x:x,y:y},n:{x:v,y:l=a+2*c*(o-a)+l*(i-2*o+a)},start:{x:h*t+c*r,y:h*n+c*a},end:{x:h*e+c*u,y:h*o+c*i},alpha:90-180*Math.atan2(x-v,y-l)/Math.PI}}var ut=function(t,n,r){var a=et(t),e=et(n);if(e=e,a=I(a=a),e=I(e),!(L(e,a.x,a.y)||L(e,a.x2,a.y)||L(e,a.x,a.y2)||L(e,a.x2,a.y2)||L(a,e.x,e.y)||L(a,e.x2,e.y)||L(a,e.x,e.y2)||L(a,e.x2,e.y2)||(a.x<e.x2&&a.x>e.x||e.x<a.x2&&e.x>a.x)&&(a.y<e.y2&&a.y>e.y||e.y<a.y2&&e.y>a.y)))return r?0:[];for(var o=~~(at.apply(0,t)/8),u=~~(at.apply(0,n)/8),i=[],c=[],h={},s=r?0:[],f=0;f<1+o;f++){var l=ot.apply(0,t.concat(f/o));i.push({x:l.x,y:l.y,t:f/o})}for(f=0;f<1+u;f++){l=ot.apply(0,n.concat(f/u));c.push({x:l.x,y:l.y,t:f/u})}for(f=0;f<o;f++)for(var p=0;p<u;p++){var x=i[f],y=i[f+1],v=c[p],g=c[p+1],M=Math.abs(y.x-x.x)<.001?"y":"x",m=Math.abs(g.x-v.x)<.001?"y":"x",d=function(t,n,r,a,e,o,u,i){if(!(Math.max(t,r)<Math.min(e,u)||Math.min(t,r)>Math.max(e,u)||Math.max(n,a)<Math.min(o,i)||Math.min(n,a)>Math.max(o,i))){var c=(t*a-n*r)*(e-u)-(t-r)*(e*i-o*u),h=(t*a-n*r)*(o-i)-(n-a)*(e*i-o*u),s=(t-r)*(o-i)-(n-a)*(e-u);if(s){var c=c/s,h=h/s,s=+c.toFixed(2),f=+h.toFixed(2);if(!(s<+Math.min(t,r).toFixed(2)||s>+Math.max(t,r).toFixed(2)||s<+Math.min(e,u).toFixed(2)||s>+Math.max(e,u).toFixed(2)||f<+Math.min(n,a).toFixed(2)||f>+Math.max(n,a).toFixed(2)||f<+Math.min(o,i).toFixed(2)||f>+Math.max(o,i).toFixed(2)))return{x:c,y:h}}}}(x.x,x.y,y.x,y.y,v.x,v.y,g.x,g.y);d&&h[d.x.toFixed(4)]!==d.y.toFixed(4)&&(h[d.x.toFixed(4)]=d.y.toFixed(4),M=x.t+Math.abs((d[M]-x[M])/(y[M]-x[M]))*(y.t-x.t),y=v.t+Math.abs((d[m]-v[m])/(g[m]-v[m]))*(g.t-v.t),0<=M&&M<=1&&0<=y&&y<=1&&(r?s++:s.push({x:d.x,y:d.y,t1:M,t2:y})))}return s};var it=/,?([a-z]),?/gi;var ct=Math.PI/180;function M(t){return Math.sqrt(t[0]*t[0]+t[1]*t[1])}function m(t,n){return M(t)*M(n)?(t[0]*n[0]+t[1]*n[1])/(M(t)*M(n)):1}function ht(t,n){return(t[0]*n[1]<t[1]*n[0]?-1:1)*Math.acos(m(t,n))}function Q(t,n){return t[0]===n[0]&&t[1]===n[1]}function st(t,n){var r=n[1],a=n[2],e=(e=n[3],e=ct*e,o=2*Math.PI,(e%o+o)%o),o=n[4],u=n[5],i=t[0],c=t[1],h=n[6],n=n[7],s=Math.cos(e)*(i-h)/2+Math.sin(e)*(c-n)/2,f=-1*Math.sin(e)*(i-h)/2+Math.cos(e)*(c-n)/2,l=s*s/(r*r)+f*f/(a*a),l=(1<l&&(r*=Math.sqrt(l),a*=Math.sqrt(l)),r*r*(f*f)+a*a*(s*s)),l=l?Math.sqrt((r*r*(a*a)-l)/l):1,p=(o===u&&(l*=-1),isNaN(l)&&(l=0),a?l*r*f/a:0),l=r?l*-a*s/r:0,i=(i+h)/2+Math.cos(e)*p-Math.sin(e)*l,c=(c+n)/2+Math.sin(e)*p+Math.cos(e)*l,x=[(s-p)/r,(f-l)/a],s=[(-1*s-p)/r,(-1*f-l)/a],p=ht([1,0],x),f=ht(x,s);return m(x,s)<=-1&&(f=Math.PI),1<=m(x,s)&&(f=0),0===u&&0<f&&(f-=2*Math.PI),1===u&&f<0&&(f+=2*Math.PI),{cx:i,cy:c,rx:Q(t,[h,n])?0:r,ry:Q(t,[h,n])?0:a,startAngle:p,endAngle:p+f,xRotation:e,arcFlag:o,sweepFlag:u}}function ft(t,n){return[n[0]+(n[0]-t[0]),n[1]+(n[1]-t[1])]}function lt(t,n,r){return n<=t&&t<=r}function pt(t,n,r,a){var e=r.x-t.x,o=r.y-t.y,u=n.x-t.x,n=n.y-t.y,i=a.x-r.x,a=a.y-r.y,r=u*a-n*i,c=null;return.001*(u*u+n*n)*(i*i+a*a)<r*r&&(lt(a=(e*a-o*i)/r,0,1)&<((e*n-o*u)/r,0,1)&&(c={x:t.x+a*u,y:t.y+a*n})),c}var xt=1e-6;function O(t){return Math.abs(t)<xt?0:t<0?-1:1}function j(t,n,r){var a=!1,e=t.length;if(e<=2)return!1;for(var o,u,i,c=0;c<e;c++){var h=t[c],s=t[(c+1)%e];if(u=s,((i=[n,r])[0]-(o=h)[0])*(u[1]-o[1])==(u[0]-o[0])*(i[1]-o[1])&&Math.min(o[0],u[0])<=i[0]&&i[0]<=Math.max(o[0],u[0])&&Math.min(o[1],u[1])<=i[1]&&i[1]<=Math.max(o[1],u[1]))return!0;0<O(h[1]-r)!=0<O(s[1]-r)&&O(n-(r-h[1])*(h[0]-s[0])/(h[1]-s[1])-h[0])<0&&(a=!a)}return a}function yt(t){for(var n,r,a=[],e=t.length,o=0;o<e-1;o++){var u=t[o],i=t[o+1];a.push({from:{x:u[0],y:u[1]},to:{x:i[0],y:i[1]}})}return 1<a.length&&(n=t[0],r=t[e-1],a.push({from:{x:r[0],y:r[1]},to:{x:n[0],y:n[1]}})),a}function vt(t){var n=t.map(function(t){return t[0]}),t=t.map(function(t){return t[1]});return{minX:Math.min.apply(null,n),maxX:Math.max.apply(null,n),minY:Math.min.apply(null,t),maxY:Math.max.apply(null,t)}}t.angleTo=function(t,n,r){e=n,u=(a=t)[0],a=a[1],i=e[0],e=e[1],o=Math.sqrt(u*u+a*a)*Math.sqrt(i*i+e*e);var a,e,o,u=Math.acos(Math.min(Math.max(o&&(u*i+a*e)/o,-1),1)),i=0<=p(t,n);return r?i?2*Math.PI-u:u:i?u:2*Math.PI-u},t.catmullRom2Bezier=function(t,n,r){void 0===r&&(r=[[0,0],[1,1]]);for(var n=!!(n=void 0===n?!1:n),a=[],e=0,o=t.length;e<o;e+=2)a.push([t[e],t[e+1]]);for(var u,i,c,h=function(t,n,r,a){var e,o=[],u=!!a;if(u){h=a[0],s=a[1];for(var i=0,c=t.length;i<c;i+=1)var h=A([0,0],h,d=t[i]),s=w([0,0],s,d)}for(var i=0,f=t.length;i<f;i+=1){var l,p,x,y,v,g,M,m,d=t[i];0!==i||r?i!==f-1||r?(l=t[[i?i-1:f-1,i-1][r?0:1]],p=t[r?(i+1)%f:i+1],g=P(g=k([0,0],p,l),g,n),0!==(v=(x=T(d,l))+(y=T(d,p)))&&(x/=v,y/=v),v=P([0,0],g,-x),g=P([0,0],g,y),M=b([0,0],d,v),m=A([0,0],m=b([0,0],d,g),w([0,0],p,d)),m=w([0,0],m,A([0,0],p,d)),M=A([0,0],M=b([0,0],d,v=P([0,0],v=k([0,0],m,d),-x/y)),w([0,0],l,d)),M=w([0,0],M,A([0,0],l,d)),m=b([0,0],d,g=P([0,0],g=k([0,0],d,M),y/x)),u&&(M=A([0,0],M=w([0,0],M,h),s),m=A([0,0],m=w([0,0],m,h),s)),o.push(e),o.push(M),e=m):(M=d,o.push(e),o.push(M)):e=d}return r&&o.push(o.shift()),o}(a,.4,n,r),s=a.length,f=[],e=0;e<s-1;e+=1)u=h[2*e],i=h[2*e+1],c=a[e+1],f.push(["C",u[0],u[1],i[0],i[1],c[0],c[1]]);return n&&(u=h[s],i=h[s+1],c=a[0],f.push(["C",u[0],u[1],i[0],i[1],c[0],c[1]])),f},t.direction=p,t.fillPath=function(a,t){if(1===a.length)return a;var e=a.length-1,n=t.length-1,r=e/n,o=[];if(1===a.length&&"M"===a[0][0]){for(var u=0;u<n-e;u++)a.push(a[0]);return a}for(u=0;u<n;u++){var i=Math.floor(r*u);o[i]=(o[i]||0)+1}var c=o.reduce(function(t,n,r){return r===e?t.concat(a[e]):t.concat(Y(a[r],a[r+1],n))},[]);return c.unshift(a[0]),"Z"!==t[n]&&"z"!==t[n]||c.push("Z"),c},t.fillPathByDiff=function(t,n){var r=function(t,n){var r=t.length,a=n.length,e=0;if(0===r||0===a)return null;for(var o=[],u=0;u<=r;u++)o[u]=[],o[u][0]={min:u};for(var i=0;i<=a;i++)o[0][i]={min:i};for(var c,u=1;u<=r;u++)for(var h=t[u-1],i=1;i<=a;i++){var s=n[i-1],e=B(h,s)?0:1,s=o[u-1][i].min+1,f=o[u][i-1].min+1,l=o[u-1][i-1].min+e;o[u][i]=(void 0,c=null,(f=f)<(l=l)&&(l=f,c="add"),(f=s)<l&&(l=f,c="del"),{type:c,min:l})}return o}(t,n),a=t.length,e=n.length,o=[],u=1,i=1;if(r[a][e]!==a){for(var c=1;c<=a;c++){for(var h=r[c][c].min,i=c,s=u;s<=e;s++)r[c][s].min<h&&(h=r[c][s].min,i=s);u=i,r[c][u].type&&o.push({index:c-1,type:r[c][u].type})}for(c=o.length-1;0<=c;c--)u=o[c].index,"add"===o[c].type?t.splice(u,0,[].concat(t[u])):t.splice(u,1)}if((a=t.length)<e)for(c=0;c<e-a;c++)"z"===t[a-1][0]||"Z"===t[a-1][0]?t.splice(a-2,0,t[a-2]):t.push(t[a-1]);return t},t.formatPath=function(t,n){if(t.length<=1)return t;for(var r,a=0;a<n.length;a++)if(t[a][0]!==n[a][0])switch(r=x(t[a]),n[a][0]){case"M":t[a]=["M"].concat(r[0]);break;case"L":t[a]=["L"].concat(r[0]);break;case"A":t[a]=[].concat(n[a]),t[a][6]=r[0][0],t[a][7]=r[0][1];break;case"Q":if(r.length<2){if(!(0<a)){t[a]=n[a];break}r=y(r,t[a-1],1)}t[a]=["Q"].concat(r.reduce(function(t,n){return t.concat(n)},[]));break;case"T":t[a]=["T"].concat(r[0]);break;case"C":if(r.length<3){if(!(0<a)){t[a]=n[a];break}r=y(r,t[a-1],2)}t[a]=["C"].concat(r.reduce(function(t,n){return t.concat(n)},[]));break;case"S":if(r.length<2){if(!(0<a)){t[a]=n[a];break}r=y(r,t[a-1],1)}t[a]=["S"].concat(r.reduce(function(t,n){return t.concat(n)},[]));break;default:t[a]=n[a]}return t},t.getArcParams=st,t.getLineIntersect=pt,t.gradient=function(t){var o=("string"==typeof t?t.split("-"):t).map(function(t){return n(-1===t.indexOf("#")?e(t):t)});return function(t){return n=o,t=t,t=isNaN(Number(t))||t<0?0:1<t?1:Number(t),r=n.length-1,a=Math.floor(r*t),t=r*t-a,e=n[a],r=a===r?e:n[a+1],u([i(e,r,t,0),i(e,r,t,1),i(e,r,t,2)]);var n,r,a,e}},t.isPointInPolygon=j,t.isPolygonsIntersect=function(n,r){if(n.length<2||r.length<2)return!1;var t=vt(n),a=vt(r);if(t=t,(a=a).minX>t.maxX||a.maxX<t.minX||a.minY>t.maxY||a.maxY<t.minY)return!1;if(r.some(function(t){return j(n,t[0],t[1])}))return!0;if(n.some(function(t){return j(r,t[0],t[1])}))return!0;var o=yt(n);return yt(r).some(function(t){for(var n=o,r=t,a=0;a<n.length;a++){var e=n[a];if(pt(e.from,e.to,r.from,r.to))return!0}return!1})},t.parsePath=C,t.parsePathArray=function(t){return t.join(",").replace(it,"$1")},t.parsePathString=d,t.path2Absolute=W,t.path2Curve=S,t.path2Segments=function(t){for(var n=[],r=null,a=null,e=null,o=0,u=(t=C(t)).length,i=0;i<u;i++){var c=t[i],a=t[i+1],h=c[0],s={command:h,prePoint:r,params:c,startTangent:null,endTangent:null};switch(h){case"M":e=[c[1],c[2]],o=i;break;case"A":var f=st(r,c);s.arcParams=f}"Z"===h?(r=e,a=t[o+1]):r=[c[(P=c.length)-2],c[P-1]],a&&"Z"===a[0]&&(a=t[o],n[o]&&(n[o].prePoint=r)),s.currentPoint=r,n[o]&&Q(r,n[o].currentPoint)&&(n[o].prePoint=s.prePoint);var l,p,x,y,v,g,M,m,d,b,A,w,P=a?[a[a.length-2],a[a.length-1]]:null,T=(s.nextPoint=P,s.prePoint);["L","H","V"].includes(h)?(s.startTangent=[T[0]-r[0],T[1]-r[1]],s.endTangent=[r[0]-T[0],r[1]-T[1]]):"Q"===h?(l=[c[1],c[2]],s.startTangent=[T[0]-l[0],T[1]-l[1]],s.endTangent=[r[0]-l[0],r[1]-l[1]]):"T"===h?(l=ft((p=n[i-1]).currentPoint,T),"Q"===p.command?(s.command="Q",s.startTangent=[T[0]-l[0],T[1]-l[1]],s.endTangent=[r[0]-l[0],r[1]-l[1]]):(s.command="TL",s.startTangent=[T[0]-r[0],T[1]-r[1]],s.endTangent=[r[0]-T[0],r[1]-T[1]])):"C"===h?(x=[c[1],c[2]],y=[c[3],c[4]],s.startTangent=[T[0]-x[0],T[1]-x[1]],s.endTangent=[r[0]-y[0],r[1]-y[1]],0===s.startTangent[0]&&0===s.startTangent[1]&&(s.startTangent=[x[0]-y[0],x[1]-y[1]]),0===s.endTangent[0]&&0===s.endTangent[1]&&(s.endTangent=[y[0]-x[0],y[1]-x[1]])):"S"===h?(x=ft((p=n[i-1]).currentPoint,T),y=[c[1],c[2]],"C"===p.command?(s.command="C",s.startTangent=[T[0]-x[0],T[1]-x[1]]):(s.command="SQ",s.startTangent=[T[0]-y[0],T[1]-y[1]]),s.endTangent=[r[0]-y[0],r[1]-y[1]]):"A"===h&&(h=.001,v=void 0===(v=(b=s.arcParams||{}).cx)?0:v,g=void 0===(g=b.cy)?0:g,A=b.rx,M=void 0===(M=b.ry)?0:M,m=b.sweepFlag,d=void 0===(d=b.startAngle)?0:d,b=void 0===(b=b.endAngle)?0:b,0===(void 0===m?0:m)&&(h*=-1),A=(m=void 0===A?0:A)*Math.cos(d-h)+v,w=M*Math.sin(d-h)+g,s.startTangent=[A-e[0],w-e[1]],A=m*Math.cos(d+b+h)+v,w=M*Math.sin(d+b-h)+g,s.endTangent=[T[0]-A,T[1]-w]),n.push(s)}return n},t.pathIntersection=function(t,n){var r=t,a=n,e=void 0;r=S(r),a=S(a);for(var o,u,i,c,h,s,f,l,p=e?0:[],x=0,y=r.length;x<y;x++){var v=r[x];if("M"===v[0])o=i=v[1],g=c=v[2];else for(var g="C"===v[0]?(o=(f=[o,g].concat(v.slice(1)))[6],f[7]):(f=[o,g,o,g,i,c,i,c],o=i,c),M=0,m=a.length;M<m;M++){var d=a[M];if("M"===d[0])u=h=d[1],b=s=d[2];else{var b="C"===d[0]?(u=(l=[u,b].concat(d.slice(1)))[6],l[7]):(l=[u,b,u,b,h,s,h,s],u=h,s),A=ut(f,l,e);if(e)p+=A;else{for(var w=0,P=A.length;w<P;w++)A[w].segment1=x,A[w].segment2=M,A[w].bez1=f,A[w].bez2=l;p=p.concat(A)}}}}return p},t.rectPath=v,t.rgb2arr=n,t.toCSSGradient=function(t){var n,r,a,e,o;return/^[r,R,L,l]{1}[\s]*\(/.test(t)?(r=void 0,"l"===t[0]?(n=+(e=N.exec(t))[1]+90,r=e[2],a="linear-gradient("+n+"deg, "):"r"===t[0]&&(a="radial-gradient(",r=(e=V.exec(t))[4]),(o=r.match(Z)).forEach(function(t,n){t=t.split(":");a+=t[1]+" "+100*Number(t[0])+"%",n!==o.length-1&&(a+=", ")}),a+=")"):t},t.toRGB=e,t.transform=function(t,n){for(var r,a,e,o,u,i,c=t?[].concat(t):[1,0,0,0,1,0,0,0,1],h=0,s=n.length;h<s;h++){var f=n[h];switch(f[0]){case"t":e=a=c,o=[f[1],f[2]],u=void 0,o=o,(u=i=[0,0,0,0,0,0,0,0,0])[0]=1,u[1]=0,u[2]=0,u[3]=0,u[4]=1,u[5]=0,u[6]=o[0],u[7]=o[1],u[8]=1,l(a,i,e);break;case"s":u=o=c,a=[f[1],f[2]],i=void 0,(i=e=[0,0,0,0,0,0,0,0,0])[0]=(a=a)[0],i[1]=0,i[2]=0,i[3]=0,i[4]=a[1],i[5]=0,i[6]=0,i[7]=0,i[8]=1,l(o,e,u);break;case"r":R(c,c,f[1]);break;case"m":r=f[1],l(c,r,c)}}return c},t.vertical=function(t,n,r){return r?(t[0]=n[1],t[1]=-1*n[0]):(t[0]=-1*n[1],t[1]=n[0]),t},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).util={})}(this,function(t){"use strict";function n(t){return[parseInt(t.substr(1,2),16),parseInt(t.substr(3,2),16),parseInt(t.substr(5,2),16)]}function e(t){t=Math.round(t).toString(16);return 1===t.length?"0"+t:t}function o(t){return"#"+e(t[0])+e(t[1])+e(t[2])}const F=/rgba?\(([\s.,0-9]+)\)/;let a;function r(t){if("#"===t[0]&&7===t.length)return t;(a=a||function(){const t=document.createElement("i");return t.title="Web Colour Picker",t.style.display="none",document.body.appendChild(t),t}()).style.color=t;t=document.defaultView.getComputedStyle(a,"").getPropertyValue("color");const e=F.exec(t);return o(e[1].split(/\s*,\s*/).map(t=>Number(t)))}function c(t,e,n,a){return t[a]+(e[a]-t[a])*n}const Q=/^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i,j=/^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i,Z=/[\d.]+:(#[^\s]+|[^)]+\))/gi;var i="undefined"!=typeof Float32Array?Float32Array:Array;function u(t,e,n){var a=e[0],r=e[1],i=e[2],o=e[3],c=e[4],l=e[5],s=e[6],h=e[7],e=e[8],u=n[0],x=n[1],p=n[2],f=n[3],m=n[4],y=n[5],d=n[6],M=n[7],n=n[8];return t[0]=u*a+x*o+p*s,t[1]=u*r+x*c+p*h,t[2]=u*i+x*l+p*e,t[3]=f*a+m*o+y*s,t[4]=f*r+m*c+y*h,t[5]=f*i+m*l+y*e,t[6]=d*a+M*o+n*s,t[7]=d*r+M*c+n*h,t[8]=d*i+M*l+n*e,t}function l(){var t=new i(3);return i!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t}function s(t,e,n){var a=new i(3);return a[0]=t,a[1]=e,a[2]=n,a}Math.hypot||(Math.hypot=function(){for(var t=0,e=arguments.length;e--;)t+=arguments[e]*arguments[e];return Math.sqrt(t)});function h(){var t=new i(4);return i!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t[3]=1,t}l(),x=new i(4),i!=Float32Array&&(x[0]=0,x[1]=0,x[2]=0,x[3]=0);var x;function B(t,e,n){var a,r,i=[0,0,0,0,0,0,0,0,0];a=i,n=n,r=Math.sin(n),n=Math.cos(n),a[0]=n,a[1]=r,a[2]=0,a[3]=-r,a[4]=n,a[5]=0,a[6]=0,a[7]=0,a[8]=1,u(t,i,e)}function p(t,e){return t[0]*e[1]-e[0]*t[1]}function m(t){return t.map(t=>Array.isArray(t)?[...t]:t)}l(),s(1,0,0),s(0,1,0),h(),h(),x=new i(9),i!=Float32Array&&(x[1]=0,x[2]=0,x[3]=0,x[5]=0,x[6]=0,x[7]=0),x[0]=1,x[4]=1,x[8]=1,x=new i(2),i!=Float32Array&&(x[0]=0,x[1]=0);const y={x1:0,y1:0,x2:0,y2:0,x:0,y:0,qx:null,qy:null};const f={a:7,c:6,h:1,l:2,m:2,r:4,q:4,s:4,t:2,v:1,z:0};function d(t){return Array.isArray(t)&&t.every(t=>{var e=t[0].toLowerCase();return f[e]===t.length-1&&"achlmqstvz".includes(e)})}function M(t){return d(t)&&t.every(([t])=>t===t.toUpperCase())}function g(t){return M(t)&&t.every(([t])=>"ACLMQZ".includes(t))}function v(t){let e=t.pathValue[t.segmentStart],n=e.toLowerCase();const a=t["data"];for(;a.length>=f[n]&&("m"===n&&2<a.length?(t.segments.push([e,...a.splice(0,2)]),n="l",e="m"===e?"l":"L"):t.segments.push([e,...a.splice(0,f[n])]),f[n]););}function w(t){return 48<=t&&t<=57}function b(t){const{pathValue:e,max:n}=t;for(;t.index<n&&(10===(a=e.charCodeAt(t.index))||13===a||8232===a||8233===a||32===a||9===a||11===a||12===a||160===a||5760<=a&&[5760,6158,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8239,8287,12288,65279].includes(a));)t.index+=1;var a}function E(e){const{max:n,pathValue:a,index:t}=e;var r,i=a.charCodeAt(t),o=f[a[t].toLowerCase()];if(e.segmentStart=t,function(t){switch(32|t){case 109:case 122:case 108:case 104:case 118:case 99:case 115:case 113:case 116:case 97:return 1;default:return}}(i))if(e.index+=1,b(e),e.data=[],o){for(;;){for(let t=o;0<t;--t){if(97!=(32|i)||3!==t&&4!==t)(function(t){const{max:e,pathValue:n,index:a}=t;let r=a;var i;let o=!1,c=!1,l=!1,s;if(r>=e)t.err=`[path-util]: Invalid path value at index ${r}, "pathValue" is missing param`;else if(43!==(s=n.charCodeAt(r))&&45!==s||(r+=1,s=n.charCodeAt(r)),w(s)||46===s){if(46!==s){if(i=48===s,r+=1,s=n.charCodeAt(r),i&&r<e&&s&&w(s))return t.err=`[path-util]: Invalid path value at index ${a}, "${n[a]}" illegal number`;for(;r<e&&w(n.charCodeAt(r));)r+=1,o=!0;s=n.charCodeAt(r)}if(46===s){for(l=!0,r+=1;w(n.charCodeAt(r));)r+=1,c=!0;s=n.charCodeAt(r)}if(101===s||69===s){if(l&&!o&&!c)return t.err=`[path-util]: Invalid path value at index ${r}, "${n[r]}" invalid float exponent`;if(r+=1,43!==(s=n.charCodeAt(r))&&45!==s||(r+=1),!(r<e&&w(n.charCodeAt(r))))return t.err=`[path-util]: Invalid path value at index ${r}, "${n[r]}" invalid integer exponent`;for(;r<e&&w(n.charCodeAt(r));)r+=1}t.index=r,t.param=+t.pathValue.slice(a,r)}else t.err=`[path-util]: Invalid path value at index ${r}, "${n[r]}" is not a number`})(e);else{l=c=void 0;var c=e;const{index:s,pathValue:h}=c;var l=h.charCodeAt(s);48===l?(c.param=0,c.index+=1):49===l?(c.param=1,c.index+=1):c.err=`[path-util]: invalid Arc flag "${h[s]}", expecting 0 or 1 at index `+s}if(e.err.length)return;e.data.push(e.param),b(e),e.index<n&&44===a.charCodeAt(e.index)&&(e.index+=1,b(e))}if(e.index>=e.max)break;if(!(48<=(r=a.charCodeAt(e.index))&&r<=57||43===r||45===r||46===r))break}v(e)}else v(e);else e.err=`[path-util]: Invalid path value "${a[t]}" is not a path command`}class O{constructor(t){this.pathValue=t,this.segments=[],this.max=t.length,this.index=0,this.param=0,this.segmentStart=0,this.data=[],this.err=""}}function A(t){if(d(t))return m(t);var e=new O(t);for(b(e);e.index<e.max&&!e.err.length;)E(e);return e.err||e.segments}function C(t){if(M(t))return m(t);const e=A(t);let c=0,l=0,s=0,h=0;return e.map(t=>{const e=t.slice(1).map(Number),[n]=t;var a=n.toUpperCase();if("M"===n)return[c,l]=e,s=c,h=l,["M",c,l];let r;if(n!==a)switch(a){case"A":r=[a,e[0],e[1],e[2],e[3],e[4],e[5]+c,e[6]+l];break;case"V":r=[a,e[0]+l];break;case"H":r=[a,e[0]+c];break;default:var i=e.map((t,e)=>t+(e%2?l:c));r=[a,...i]}else r=[a,...e];var o=r.length;switch(a){case"Z":c=s,l=h;break;case"H":[,c]=r;break;case"V":[,l]=r;break;default:c=r[o-2],l=r[o-1],"M"===a&&(s=c,h=l)}return r})}function q(t){if(g(t))return m(t);const e=C(t),n=Object.assign({},y);var a=e.length;for(let t=0;t<a;t+=1){e[t],e[t]=function(t,e){var[n]=t,{x1:a,y1:r,x2:i,y2:o}=e,c=t.slice(1).map(Number);let l=t;return"TQ".includes(n)||(e.qx=null,e.qy=null),"H"===n?l=["L",t[1],r]:"V"===n?l=["L",a,t[1]]:"S"===n?(t=2*r-o,e.x1=o=2*a-i,e.y1=t,l=["C",o,t,...c]):"T"===n?(i=2*a-e.qx,o=2*r-e.qy,e.qx=i,e.qy=o,l=["Q",i,o,...c]):"Q"===n&&([t,a]=c,e.qx=t,e.qy=a),l}(e[t],n);var r=e[t],i=r.length;n.x1=+r[i-2],n.y1=+r[i-1],n.x2=+r[i-4]||n.x1,n.y2=+r[i-3]||n.y1}return e}function I(t,e,n){return{x:t*Math.cos(n)-e*Math.sin(n),y:t*Math.sin(n)+e*Math.cos(n)}}function P(t,e,n){var a=t[0],t=t[1];return[a+(e[0]-a)*n,t+(e[1]-t)*n]}function S(t,e){return Math.sqrt((t[0]-e[0])*(t[0]-e[0])+(t[1]-e[1])*(t[1]-e[1]))}function $(t,e,n,a,r){var i=S([t,e],[n,a]);let o={x:0,y:0};return{length:i,point:o="number"==typeof r?0===r?{x:t,y:e}:i<=r?{x:n,y:a}:([r,i]=P([t,e],[n,a],r/i),{x:r,y:i}):o,min:{x:Math.min(t,n),y:Math.min(e,a)},max:{x:Math.max(t,n),y:Math.max(e,a)}}}function k(t,e,n,a){var t=[t,e],e=[n,a],r=P(t,e,.5),i=P(e,r,.5),o=P(r,i,.5),c=P(i,o,.5),l=P(o,c,.5),t=$(...[...t,...r,...o,...l,.5]).point,r=$(...[...l,...c,...i,...e,0]).point;return[t.x,t.y,r.x,r.y,n,a]}function R(t,e){var[n]=t,a=t.slice(1).map(Number),[r,i]=a;let o;var c,l,s,h,u,x,{x1:p,y1:f,x:m,y}=e;switch("TQ".includes(n)||(e.qx=null,e.qy=null),n){case"M":return e.x=r,e.y=i,t;case"A":return["C",...function t(e,n,a,r,i,o,c,l,s,h){let u=e,x=n,p=a,f=r,m=l,y=s;var e=120*Math.PI/180,d=Math.PI/180*(+i||0);let M=[],g,v,w,b;h?[g,v,w,b]=h:(n=I(u,x,-d),u=n.x,x=n.y,n=I(m,y,-d),m=n.x,y=n.y,a=(u-m)/2,r=(x-y)/2,1<(l=a*a/(p*p)+r*r/(f*f))&&(l=Math.sqrt(l),p*=l,f*=l),s=p*p,n=f*f,l=(o===c?-1:1)*Math.sqrt(Math.abs((s*n-s*r*r-n*a*a)/(s*r*r+n*a*a))),w=l*p*r/f+(u+m)/2,b=l*-f*a/p+(x+y)/2,g=(Math.asin((x-b)/f)*Math.pow(10,9)>>0)/Math.pow(10,9),v=(Math.asin((y-b)/f)*Math.pow(10,9)>>0)/Math.pow(10,9),g=u<w?Math.PI-g:g,v=m<w?Math.PI-v:v,g<0&&(g=2*Math.PI+g),v<0&&(v=2*Math.PI+v),c&&g>v&&(g-=2*Math.PI),!c&&v>g&&(v-=2*Math.PI));o=v-g,Math.abs(o)>e&&(s=v,n=m,r=y,v=g+e*(c&&v>g?1:-1),m=w+p*Math.cos(v),y=b+f*Math.sin(v),M=t(m,y,p,f,i,0,c,n,r,[v,s,w,b])),o=v-g,l=Math.cos(g),a=Math.sin(g),e=Math.cos(v),i=Math.sin(v),c=Math.tan(o/4),n=4/3*p*c,r=4/3*f*c,s=[u,x];const A=[u+n*a,x-r*l];o=[m+n*i,y-r*e],c=[m,y];if(A[0]=2*s[0]-A[0],A[1]=2*s[1]-A[1],h)return[...A,...o,...c,...M];const C=[];for(let t=0,e=(M=[...A,...o,...c,...M]).length;t<e;t+=1)C[t]=t%2?I(M[t-1],M[t],d).y:I(M[t],M[t+1],d).x;return C}(...o=[p,f,...a])];case"Q":return e.qx=r,e.qy=i,o=[p,f,...a],["C",...([c,l,s,h,u,x]=[...o],[1/3*c+2/3*s,1/3*l+2/3*h,1/3*u+2/3*s,1/3*x+2/3*h,u,x])];case"L":return["C",...k(p,f,r,i)];case"Z":return p===m&&f===y?["C",p,f,m,y,m,y]:["C",...k(p,f,m,y)]}return t}function L(t,e=!1){var n,a;if(g(n=t)&&n.every(([t])=>"MC".includes(t)))return n=m(t),e?[n,[]]:n;const r=q(t),i=Object.assign({},y),o=[];let c="",l=r.length;const s=[];for(let t=0;t<l;t+=1){r[t]&&([c]=r[t]),o[t]=c;var h=R(r[t],i),u=(r[t]=h,p=x=u=void 0,r),x=o,p=t;if(7<u[p].length){u[p].shift();const f=u[p];let t=p;for(;f.length;)x[p]="A",u.splice(t+=1,0,["C"].concat(f.splice(0,6)));u.splice(p,1)}l=r.length,"Z"===c&&s.push(t),a=(h=r[t]).length,i.x1=+h[a-2],i.y1=+h[a-1],i.x2=+h[a-4]||i.x1,i.y2=+h[a-3]||i.y1}return e?[r,s]:r}function V(t,e){var{x:t,y:n}=t,{x:e,y:a}=e,r=t*e+n*a,i=Math.sqrt((Math.pow(t,2)+Math.pow(n,2))*(Math.pow(e,2)+Math.pow(a,2)));return(t*a-n*e<0?-1:1)*Math.acos(r/i)}function H(e,n,a,r,i,o,c,l,s,h){var u="number"==typeof h;let x=e,p=n,f=0,m=[x,p,f],y=[x,p];let d={x:0,y:0},M=[{x:x,y:p}];u&&0===h&&(d={x:x,y:p});for(let t=0;t<=300;t+=1){var g=t/300;({x,y:p}=function(t,e,n,a,r,i,o,c,l,s){const{abs:h,sin:u,cos:x,sqrt:p,PI:f}=Math;let m=h(n),y=h(a);if(n=f/180*((r%360+360)%360),t===c&&e===l)return{x:t,y:e};if(0===m||0===y)return $(t,e,c,l,s).point;a=(t-c)/2,r=(e-l)/2,a={x:x(n)*a+u(n)*r,y:-u(n)*a+x(n)*r},1<(r=Math.pow(a.x,2)/Math.pow(m,2)+Math.pow(a.y,2)/Math.pow(y,2))&&(m*=p(r),y*=p(r));let d=(r=Math.pow(m,2)*Math.pow(y,2)-Math.pow(m,2)*Math.pow(a.y,2)-Math.pow(y,2)*Math.pow(a.x,2))/(Math.pow(m,2)*Math.pow(a.y,2)+Math.pow(y,2)*Math.pow(a.x,2));d=d<0?0:d;const M=(r=(i!==o?1:-1)*p(d))*(m*a.y/y),g=r*(-y*a.x/m),v=x(n)*M-u(n)*g+(t+c)/2,w=u(n)*M+x(n)*g+(e+l)/2;i={x:(a.x-M)/m,y:(a.y-g)/y},r=V({x:1,y:0},i);let b=V(i,{x:(-a.x-M)/m,y:(-a.y-g)/y});return!o&&0<b?b-=2*f:o&&b<0&&(b+=2*f),t=r+(b%=2*f)*s,c=m*x(t),e=y*u(t),{x:x(n)*c-u(n)*e+v,y:u(n)*c+x(n)*e+w}}(e,n,a,r,i,o,c,l,s,g)),M=[...M,{x:x,y:p}],f+=S(y,[x,p]),y=[x,p],u&&f>h&&h>m[2]&&(g=(f-h)/(f-m[2]),d={x:y[0]*(1-g)+m[0]*g,y:y[1]*(1-g)+m[1]*g}),m=[x,p,f]}return u&&h>=f&&(d={x:l,y:s}),{length:f,point:d,min:{x:Math.min(...M.map(t=>t.x)),y:Math.min(...M.map(t=>t.y))},max:{x:Math.max(...M.map(t=>t.x)),y:Math.max(...M.map(t=>t.y))}}}function D(e,n,a,r,i,o,c,l,s){var h="number"==typeof s;let u=e,x=n,p=0,f=[u,x,p],m=[u,x];let y={x:0,y:0},d=[{x:u,y:x}];h&&0===s&&(y={x:u,y:x});var M,g,v,w,b,A,C,q;for(let t=0;t<=20;t+=1){var I,P=t/20;({x:u,y:x}=(I=e,M=n,g=a,v=r,w=i,b=o,A=c,C=l,q=void 0,q=1-(P=P),{x:Math.pow(q,3)*I+3*Math.pow(q,2)*P*g+3*q*Math.pow(P,2)*w+Math.pow(P,3)*A,y:Math.pow(q,3)*M+3*Math.pow(q,2)*P*v+3*q*Math.pow(P,2)*b+Math.pow(P,3)*C})),d=[...d,{x:u,y:x}],p+=S(m,[u,x]),m=[u,x],h&&p>s&&s>f[2]&&(I=(p-s)/(p-f[2]),y={x:m[0]*(1-I)+f[0]*I,y:m[1]*(1-I)+f[1]*I}),f=[u,x,p]}return h&&s>=p&&(y={x:c,y:l}),{length:p,point:y,min:{x:Math.min(...d.map(t=>t.x)),y:Math.min(...d.map(t=>t.y))},max:{x:Math.max(...d.map(t=>t.x)),y:Math.max(...d.map(t=>t.y))}}}function G(e,n,a,r,i,o,c){var l="number"==typeof c;let s=e,h=n,u=0,x=[s,h,u],p=[s,h];let f={x:0,y:0},m=[{x:s,y:h}];l&&0===c&&(f={x:s,y:h});var y,d,M,g,v,w;for(let t=0;t<=300;t+=1){var b,A=t/300;({x:s,y:h}=(b=e,y=n,d=a,M=r,g=i,v=o,w=void 0,w=1-(A=A),{x:Math.pow(w,2)*b+2*w*A*d+Math.pow(A,2)*g,y:Math.pow(w,2)*y+2*w*A*M+Math.pow(A,2)*v})),m=[...m,{x:s,y:h}],u+=S(p,[s,h]),p=[s,h],l&&u>c&&c>x[2]&&(b=(u-c)/(u-x[2]),f={x:p[0]*(1-b)+x[0]*b,y:p[1]*(1-b)+x[1]*b}),x=[s,h,u]}return l&&c>=u&&(f={x:i,y:o}),{length:u,point:f,min:{x:Math.min(...m.map(t=>t.x)),y:Math.min(...m.map(t=>t.y))},max:{x:Math.max(...m.map(t=>t.x)),y:Math.max(...m.map(t=>t.y))}}}function N(t,n){var a,r,i=q(t),o="number"==typeof n;let c=[],l=0,s=0,h=0,u=0,x,p=[],f=[],m=0,y={x:0,y:0},d=y,M=y,g=y,v=0;for(let t=0,e=i.length;t<e;t+=1)[r]=x=i[t],a="M"===r,c=a?c:[l,s,...x.slice(1)],a?([,h,u]=x,y={x:h,y:u},d=y,m=0,o&&n<.001&&(g=y)):"L"===r?{length:m,min:y,max:d,point:M}=$(...c,(n||0)-v):"A"===r?{length:m,min:y,max:d,point:M}=H(...c,(n||0)-v):"C"===r?{length:m,min:y,max:d,point:M}=D(...c,(n||0)-v):"Q"===r?{length:m,min:y,max:d,point:M}=G(...c,(n||0)-v):"Z"===r&&(c=[l,s,h,u],{length:m,min:y,max:d,point:M}=$(...c,(n||0)-v)),o&&v<n&&v+m>=n&&(g=M),f=[...f,d],p=[...p,y],v+=m,[l,s]="Z"!==r?x.slice(-2):[h,u];return o&&n>=v&&(g={x:l,y:s}),{length:v,point:g,min:{x:Math.min(...p.map(t=>t.x)),y:Math.min(...p.map(t=>t.y))},max:{x:Math.max(...f.map(t=>t.x)),y:Math.max(...f.map(t=>t.y))}}}function T(t){return N(t).length}function U(t){let s=0,h=0,u;return L(t).map(t=>{return"M"!==t[0]?(u=([e,n,a,r,i,o,c,l]=[s,h,...t.slice(1)],3*((l-n)*(a+i)-(c-e)*(r+o)+r*(e-i)-a*(n-o)+l*(i+e/3)-c*(o+n/3))/20),[s,h]=t.slice(-2),u):([,s,h]=t,0);var e,n,a,r,i,o,c,l}).reduce((t,e)=>t+e,0)}function z(t,e){return N(t,e).point}function _(t,n){var e,t=A(t),a=q(t),r=T(t),i=t=>{var e=t.x-n.x,t=t.y-n.y;return e*e+t*t};let o=8;var c,l,s;let h,u=0,x=1/0;for(let t=0;t<=r;t+=o)(c=i(e=z(a,t)))<x&&(h=e,u=t,x=c);o/=2;for(var p,f,m,y;.5<o;)m=i(l=z(a,p=u-o)),y=i(s=z(a,f=u+o)),0<=p&&m<x?(h=l,u=p,x=m):f<=r&&y<x?(h=s,u=f,x=y):o/=2;var t=function(t,e){const n=A(t);if("string"==typeof n)throw TypeError(n);let a=[...n],r=T(a),i=a.length-1,o=0,c=0,l=n[0];var[t,s]=l.slice(-2),t={x:t,y:s};if(i<=0||!e||!Number.isFinite(e))return{segment:l,index:0,length:c,point:t,lengthAtSegment:o};if(e>=r)return a=n.slice(0,-1),o=T(a),c=r-o,{segment:n[i],index:i,length:c,lengthAtSegment:o};const h=[];for(;0<i;)l=a[i],a=a.slice(0,-1),o=T(a),c=r-o,r=o,h.push({segment:l,index:i,length:c,lengthAtSegment:o}),--i;return h.find(({lengthAtSegment:t})=>t<=e)}(t,u),d=Math.sqrt(x);return{closest:h,distance:d,segment:t}}function W(t){return t.map((t,e,n)=>{var a,r,i,o,n=e&&n[e-1].slice(-2).concat(t.slice(1)),c=e?D(...n).length:0;let l;return{s:t,ss:l=e?c?(e=.5,a=(n=n).slice(0,2),r=n.slice(2,4),i=n.slice(4,6),n=n.slice(6,8),a=P(a,r,e),r=P(r,i,e),i=P(i,n,e),o=P(a,r,e),r=P(r,i,e),e=P(o,r,e),[["C"].concat(a,o,e),["C"].concat(r,i,n)]):[t,t]:[t],l:c}})}t.angleTo=function(t,e,n){r=e,o=(a=t)[0],a=a[1],c=r[0],r=r[1],i=Math.sqrt(o*o+a*a)*Math.sqrt(c*c+r*r);var a,r,i,o=Math.acos(Math.min(Math.max(i&&(o*c+a*r)/i,-1),1)),c=0<=p(t,e);return n?c?2*Math.PI-o:o:c?o:2*Math.PI-o},t.clonePath=m,t.direction=p,t.distanceSquareRoot=S,t.equalizeSegments=function t(e,n,a){const r=W(e),i=W(n);var e=r.length,n=i.length,o=r.filter(t=>t.l).length,c=i.filter(t=>t.l).length,o=r.filter(t=>t.l).reduce((t,{l:e})=>t+e,0)/o||0,c=i.filter(t=>t.l).reduce((t,{l:e})=>t+e,0)/c||0;const l=a||Math.max(e,n),s=[o,c],h=[l-e,l-n];let u;a=[r,i].map((t,n)=>t.l===l?t.map(t=>t.s):t.map((t,e)=>(u=e&&h[n]&&t.l>=s[n],h[n]-=u?1:0,u?t.ss:[t.s])).flat());return a[0].length===a[1].length?a:t(a[0],a[1],l)},t.getDrawDirection=function(t){return 0<=U(t)},t.getPathArea=U,t.getPathBBox=function(t){if(!t)return{x:0,y:0,width:0,height:0,x2:0,y2:0,cx:0,cy:0,cz:0};var{min:{x:t,y:e},max:{x:n,y:a}}=N(t),r=n-t,i=a-e;return{width:r,height:i,x:t,y:e,x2:n,y2:a,cx:t+r/2,cy:e+i/2,cz:Math.max(r,i)+Math.min(r,i)/2}},t.getPathBBoxTotalLength=function(t){if(!t)return{length:0,x:0,y:0,width:0,height:0,x2:0,y2:0,cx:0,cy:0,cz:0};var{length:t,min:{x:e,y:n},max:{x:a,y:r}}=N(t),i=a-e,o=r-n;return{length:t,width:i,height:o,x:e,y:n,x2:a,y2:r,cx:e+i/2,cy:n+o/2,cz:Math.max(i,o)+Math.min(i,o)/2}},t.getPointAtLength=z,t.getRotatedCurve=function(a,r){const i=a.length-1,e=[];var t;let o=0;const n=function(i){const o=i.length,c=o-1;return i.map((t,r)=>i.map((t,e)=>{let n=r+e,a;return 0===e||i[n]&&"M"===i[n][0]?(a=i[n],["M"].concat(a.slice(-2))):(n>=o&&(n-=c),i[n])}))}(a);return n.forEach((t,n)=>{a.slice(1).forEach((t,e)=>{o+=S(a[(n+e)%i].slice(-2),r[e%i].slice(-2))}),e[n]=o,o=0}),t=e.indexOf(Math.min.apply(null,e)),n[t]},t.getTotalLength=T,t.gradient=function(t){const e="string"==typeof t?t.split("-"):t,i=e.map(t=>n(-1===t.indexOf("#")?r(t):t));return t=>{return e=i,t=t,t=isNaN(Number(t))||t<0?0:1<t?1:Number(t),n=e.length-1,a=Math.floor(n*t),t=n*t-a,r=e[a],n=a===n?r:e[a+1],o([c(r,n,t,0),c(r,n,t,1),c(r,n,t,2)]);var e,n,a,r}},t.isPointInStroke=function(t,e){return t=_(t,e).distance,Math.abs(t)<.001},t.normalizePath=q,t.path2Absolute=C,t.path2Curve=L,t.path2String=function(t,e="off"){return function(t,n){if("off"===n)return m(t);const a="number"==typeof n&&1<=n?Math.pow(10,n):1;return t.map(t=>{var e=t.slice(1).map(Number).map(t=>n?Math.round(t*a)/a:Math.round(t));return[t[0],...e]})}(t,e).map(t=>t[0]+t.slice(1).join(" ")).join("")},t.reverseCurve=function(a){const t=a.slice(1).map((t,e,n)=>(e?n[e-1].slice(-2):a[0].slice(1)).concat(t.slice(1))).map(n=>n.map((t,e)=>n[n.length-e-2*(1-e%2)])).reverse();return[["M"].concat(t[0].slice(0,2))].concat(t.map(t=>["C"].concat(t.slice(2))))},t.rgb2arr=n,t.toCSSGradient=function(e){if(/^[r,R,L,l]{1}[\s]*\(/.test(e)){let n,t;var a,r;"l"===e[0]?(a=+(r=Q.exec(e))[1]+90,t=r[2],n=`linear-gradient(${a}deg, `):"r"===e[0]&&(n="radial-gradient(",r=j.exec(e),t=r[4]);const i=t.match(Z);return i.forEach((t,e)=>{t=t.split(":");n+=`${t[1]} ${100*Number(t[0])}%`,e!==i.length-1&&(n+=", ")}),n+=")"}return e},t.toRGB=r,t.transform=function(t,n){var a,r,i,o,c,l,s=t?[].concat(t):[1,0,0,0,1,0,0,0,1];for(let t=0,e=n.length;t<e;t++){var h=n[t];switch(h[0]){case"t":i=r=s,o=[h[1],h[2]],c=void 0,o=o,(c=l=[0,0,0,0,0,0,0,0,0])[0]=1,c[1]=0,c[2]=0,c[3]=0,c[4]=1,c[5]=0,c[6]=o[0],c[7]=o[1],c[8]=1,u(r,l,i);break;case"s":c=o=s,r=[h[1],h[2]],l=void 0,(l=i=[0,0,0,0,0,0,0,0,0])[0]=(r=r)[0],l[1]=0,l[2]=0,l[3]=0,l[4]=r[1],l[5]=0,l[6]=0,l[7]=0,l[8]=1,u(o,i,c);break;case"r":B(s,s,h[1]);break;case"m":a=h[1],u(s,a,s)}}return s},t.vertical=function(t,e,n){return n?(t[0]=e[1],t[1]=-1*e[0]):(t[0]=-1*e[1],t[1]=e[0]),t},Object.defineProperty(t,"__esModule",{value:!0})}); |
@@ -7,4 +7,4 @@ /** | ||
export function toHex(value) { | ||
var x16Value = Math.round(value).toString(16); | ||
return x16Value.length === 1 ? "0" + x16Value : x16Value; | ||
const x16Value = Math.round(value).toString(16); | ||
return x16Value.length === 1 ? `0${x16Value}` : x16Value; | ||
} | ||
@@ -17,4 +17,4 @@ /** | ||
export function arr2rgb(arr) { | ||
return "#" + toHex(arr[0]) + toHex(arr[1]) + toHex(arr[2]); | ||
return `#${toHex(arr[0])}${toHex(arr[1])}${toHex(arr[2])}`; | ||
} | ||
//# sourceMappingURL=arr2rgb.js.map |
@@ -22,8 +22,8 @@ import { rgb2arr } from './rgb2arr'; | ||
function calColor(points, percent) { | ||
var fixedPercent = isNaN(Number(percent)) || percent < 0 ? 0 : percent > 1 ? 1 : Number(percent); | ||
var steps = points.length - 1; | ||
var step = Math.floor(steps * fixedPercent); | ||
var left = steps * fixedPercent - step; | ||
var start = points[step]; | ||
var end = step === steps ? start : points[step + 1]; | ||
const fixedPercent = isNaN(Number(percent)) || percent < 0 ? 0 : percent > 1 ? 1 : Number(percent); | ||
const steps = points.length - 1; | ||
const step = Math.floor(steps * fixedPercent); | ||
const left = steps * fixedPercent - step; | ||
const start = points[step]; | ||
const end = step === steps ? start : points[step + 1]; | ||
return arr2rgb([getValue(start, end, left, 0), getValue(start, end, left, 1), getValue(start, end, left, 2)]); | ||
@@ -37,8 +37,8 @@ } | ||
export function gradient(colors) { | ||
var colorArray = typeof colors === 'string' ? colors.split('-') : colors; | ||
var points = colorArray.map(function (color) { | ||
const colorArray = typeof colors === 'string' ? colors.split('-') : colors; | ||
const points = colorArray.map((color) => { | ||
return rgb2arr(color.indexOf('#') === -1 ? toRGB(color) : color); | ||
}); | ||
// 返回一个函数 | ||
return function (percent) { | ||
return (percent) => { | ||
return calColor(points, percent); | ||
@@ -45,0 +45,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
var regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i; | ||
var regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i; | ||
var regexColorStop = /[\d.]+:(#[^\s]+|[^)]+\))/gi; | ||
const regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i; | ||
const regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i; | ||
const regexColorStop = /[\d.]+:(#[^\s]+|[^)]+\))/gi; | ||
function isGradientColor(val) { | ||
@@ -9,27 +9,27 @@ return /^[r,R,L,l]{1}[\s]*\(/.test(val); | ||
if (isGradientColor(gradientColor)) { | ||
var cssColor_1; | ||
var steps = void 0; | ||
let cssColor; | ||
let steps; | ||
if (gradientColor[0] === 'l') { | ||
// 线性渐变 | ||
var arr = regexLG.exec(gradientColor); | ||
var angle = +arr[1] + 90; // css 和 g 的渐变起始角度不同 | ||
const arr = regexLG.exec(gradientColor); | ||
const angle = +arr[1] + 90; // css 和 g 的渐变起始角度不同 | ||
steps = arr[2]; | ||
cssColor_1 = "linear-gradient(" + angle + "deg, "; | ||
cssColor = `linear-gradient(${angle}deg, `; | ||
} | ||
else if (gradientColor[0] === 'r') { | ||
// 径向渐变 | ||
cssColor_1 = 'radial-gradient('; | ||
var arr = regexRG.exec(gradientColor); | ||
cssColor = 'radial-gradient('; | ||
const arr = regexRG.exec(gradientColor); | ||
steps = arr[4]; | ||
} | ||
var colorStops_1 = steps.match(regexColorStop); | ||
colorStops_1.forEach(function (item, index) { | ||
var itemArr = item.split(':'); | ||
cssColor_1 += itemArr[1] + " " + Number(itemArr[0]) * 100 + "%"; | ||
if (index !== colorStops_1.length - 1) { | ||
cssColor_1 += ', '; | ||
const colorStops = steps.match(regexColorStop); | ||
colorStops.forEach((item, index) => { | ||
const itemArr = item.split(':'); | ||
cssColor += `${itemArr[1]} ${Number(itemArr[0]) * 100}%`; | ||
if (index !== colorStops.length - 1) { | ||
cssColor += ', '; | ||
} | ||
}); | ||
cssColor_1 += ')'; | ||
return cssColor_1; | ||
cssColor += ')'; | ||
return cssColor; | ||
} | ||
@@ -36,0 +36,0 @@ return gradientColor; |
import { arr2rgb } from './arr2rgb'; | ||
var RGB_REG = /rgba?\(([\s.,0-9]+)\)/; | ||
const RGB_REG = /rgba?\(([\s.,0-9]+)\)/; | ||
/** | ||
@@ -8,3 +8,3 @@ * 创建辅助 tag 取颜色 | ||
function createTmp() { | ||
var i = document.createElement('i'); | ||
const i = document.createElement('i'); | ||
i.title = 'Web Colour Picker'; | ||
@@ -15,3 +15,3 @@ i.style.display = 'none'; | ||
} | ||
var iEl; | ||
let iEl; | ||
/** | ||
@@ -32,5 +32,5 @@ * 将颜色转换到 rgb 的格式 | ||
iEl.style.color = color; | ||
var rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color'); | ||
var matches = RGB_REG.exec(rst); | ||
var cArray = matches[1].split(/\s*,\s*/).map(function (s) { return Number(s); }); | ||
let rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color'); | ||
const matches = RGB_REG.exec(rst); | ||
const cArray = matches[1].split(/\s*,\s*/).map((s) => Number(s)); | ||
rst = arr2rgb(cArray); | ||
@@ -37,0 +37,0 @@ return rst; |
@@ -1,2 +0,2 @@ | ||
var RADIAN = Math.PI / 180; | ||
const RADIAN = Math.PI / 180; | ||
export function toRadian(degree) { | ||
@@ -3,0 +3,0 @@ return RADIAN * degree; |
@@ -6,3 +6,3 @@ /** | ||
function leftTranslate(out, a, v) { | ||
var transMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const transMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromTranslation(transMat, v); | ||
@@ -12,3 +12,3 @@ return mat3.multiply(out, transMat, a); | ||
function leftRotate(out, a, rad) { | ||
var rotateMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const rotateMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromRotation(rotateMat, rad); | ||
@@ -18,3 +18,3 @@ return mat3.multiply(out, rotateMat, a); | ||
function leftScale(out, a, v) { | ||
var scaleMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const scaleMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromScaling(scaleMat, v); | ||
@@ -32,5 +32,5 @@ return mat3.multiply(out, scaleMat, a); | ||
export function transform(m, actions) { | ||
var matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
for (var i = 0, len = actions.length; i < len; i++) { | ||
var action = actions[i]; | ||
const matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
for (let i = 0, len = actions.length; i < len; i++) { | ||
const action = actions[i]; | ||
switch (action[0]) { | ||
@@ -71,4 +71,4 @@ case 't': | ||
export function angleTo(v1, v2, direct) { | ||
var ang = vec2.angle(v1, v2); | ||
var angleLargeThanPI = direction(v1, v2) >= 0; | ||
const ang = vec2.angle(v1, v2); | ||
const angleLargeThanPI = direction(v1, v2) >= 0; | ||
if (direct) { | ||
@@ -75,0 +75,0 @@ if (angleLargeThanPI) { |
@@ -1,16 +0,17 @@ | ||
export { parsePath } from './parse-path'; | ||
export { catmullRom2Bezier } from './catmull-rom-2-bezier'; | ||
export { fillPath } from './fill-path'; | ||
export { fillPathByDiff } from './fill-path-by-diff'; | ||
export { formatPath } from './format-path'; | ||
export { pathIntersection } from './path-intersection'; | ||
export { parsePathArray } from './parse-path-array'; | ||
export { parsePathString } from './parse-path-string'; | ||
export { path2Curve } from './path-2-curve'; | ||
export { path2Absolute } from './path-2-absolute'; | ||
export { rectPath } from './rect-path'; | ||
export { getArcParams } from './get-arc-params'; | ||
export { path2Segments } from './path-2-segments'; | ||
export { getLineIntersect } from './get-line-intersect'; | ||
export { isPolygonsIntersect } from './is-polygons-intersect'; | ||
export { isPointInPolygon } from './point-in-polygon'; | ||
export { path2String } from './convert/path-2-string'; | ||
export { path2Curve } from './convert/path-2-curve'; | ||
export { path2Absolute } from './convert/path-2-absolute'; | ||
export { clonePath } from './process/clone-path'; | ||
export { normalizePath } from './process/normalize-path'; | ||
export { reverseCurve } from './process/reverse-curve'; | ||
export { getPathBBox } from './util/get-path-bbox'; | ||
export { getTotalLength } from './util/get-total-length'; | ||
export { getPathBBoxTotalLength } from './util/get-path-bbox-total-length'; | ||
export { getRotatedCurve } from './util/get-rotated-curve'; | ||
export { getPathArea } from './util/get-path-area'; | ||
export { getDrawDirection } from './util/get-draw-direction'; | ||
export { getPointAtLength } from './util/get-point-at-length'; | ||
export { isPointInStroke } from './util/is-point-in-stroke'; | ||
export { distanceSquareRoot } from './util/distance-square-root'; | ||
export { equalizeSegments } from './util/equalize-segments'; | ||
export * from './types'; |
@@ -1,17 +0,18 @@ | ||
export { parsePath } from './parse-path'; | ||
export { catmullRom2Bezier } from './catmull-rom-2-bezier'; | ||
export { fillPath } from './fill-path'; | ||
export { fillPathByDiff } from './fill-path-by-diff'; | ||
export { formatPath } from './format-path'; | ||
export { pathIntersection } from './path-intersection'; | ||
export { parsePathArray } from './parse-path-array'; | ||
export { parsePathString } from './parse-path-string'; | ||
export { path2Curve } from './path-2-curve'; | ||
export { path2Absolute } from './path-2-absolute'; | ||
export { rectPath } from './rect-path'; | ||
export { getArcParams } from './get-arc-params'; | ||
export { path2Segments } from './path-2-segments'; | ||
export { getLineIntersect } from './get-line-intersect'; | ||
export { isPolygonsIntersect } from './is-polygons-intersect'; | ||
export { isPointInPolygon } from './point-in-polygon'; | ||
export { path2String } from './convert/path-2-string'; | ||
export { path2Curve } from './convert/path-2-curve'; | ||
export { path2Absolute } from './convert/path-2-absolute'; | ||
export { clonePath } from './process/clone-path'; | ||
export { normalizePath } from './process/normalize-path'; | ||
export { reverseCurve } from './process/reverse-curve'; | ||
export { getPathBBox } from './util/get-path-bbox'; | ||
export { getTotalLength } from './util/get-total-length'; | ||
export { getPathBBoxTotalLength } from './util/get-path-bbox-total-length'; | ||
export { getRotatedCurve } from './util/get-rotated-curve'; | ||
export { getPathArea } from './util/get-path-area'; | ||
export { getDrawDirection } from './util/get-draw-direction'; | ||
export { getPointAtLength } from './util/get-point-at-length'; | ||
export { isPointInStroke } from './util/is-point-in-stroke'; | ||
export { distanceSquareRoot } from './util/distance-square-root'; | ||
export { equalizeSegments } from './util/equalize-segments'; | ||
export * from './types'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,7 @@ | ||
export declare function arcToCubic(x1: number, y1: number, rx: number, ry: number, angle: number, LAF: number, SF: number, x2: number, y2: number): number[]; | ||
/** | ||
* Converts A (arc-to) segments to C (cubic-bezier-to). | ||
* | ||
* For more information of where this math came from visit: | ||
* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
*/ | ||
export declare function arcToCubic(X1: number, Y1: number, RX: number, RY: number, angle: number, LAF: number, SF: number, X2: number, Y2: number, recursive: number[]): any[]; |
@@ -1,140 +0,283 @@ | ||
var TAU = Math.PI * 2; | ||
var mapToEllipse = function (_a, rx, ry, cosphi, sinphi, centerx, centery) { | ||
var x = _a.x, y = _a.y; | ||
x *= rx; | ||
y *= ry; | ||
var xp = cosphi * x - sinphi * y; | ||
var yp = sinphi * x + cosphi * y; | ||
return { | ||
x: xp + centerx, | ||
y: yp + centery, | ||
}; | ||
}; | ||
var approxUnitArc = function (ang1, ang2) { | ||
// If 90 degree circular arc, use a constant | ||
// as derived from http://spencermortensen.com/articles/bezier-circle | ||
var a = ang2 === 1.5707963267948966 | ||
? 0.551915024494 | ||
: ang2 === -1.5707963267948966 | ||
? -0.551915024494 | ||
: (4 / 3) * Math.tan(ang2 / 4); | ||
var x1 = Math.cos(ang1); | ||
var y1 = Math.sin(ang1); | ||
var x2 = Math.cos(ang1 + ang2); | ||
var y2 = Math.sin(ang1 + ang2); | ||
return [ | ||
{ | ||
x: x1 - y1 * a, | ||
y: y1 + x1 * a, | ||
}, | ||
{ | ||
x: x2 + y2 * a, | ||
y: y2 - x2 * a, | ||
}, | ||
{ | ||
x: x2, | ||
y: y2, | ||
}, | ||
]; | ||
}; | ||
var vectorAngle = function (ux, uy, vx, vy) { | ||
var sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
var dot = ux * vx + uy * vy; | ||
if (dot > 1) { | ||
dot = 1; | ||
import { rotateVector } from '../util/rotate-vector'; | ||
/** | ||
* Converts A (arc-to) segments to C (cubic-bezier-to). | ||
* | ||
* For more information of where this math came from visit: | ||
* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
*/ | ||
export function arcToCubic(X1, Y1, RX, RY, angle, LAF, SF, X2, Y2, recursive) { | ||
let x1 = X1; | ||
let y1 = Y1; | ||
let rx = RX; | ||
let ry = RY; | ||
let x2 = X2; | ||
let y2 = Y2; | ||
// for more information of where this Math came from visit: | ||
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
const d120 = (Math.PI * 120) / 180; | ||
const rad = (Math.PI / 180) * (+angle || 0); | ||
/** @type {number[]} */ | ||
let res = []; | ||
let xy; | ||
let f1; | ||
let f2; | ||
let cx; | ||
let cy; | ||
if (!recursive) { | ||
xy = rotateVector(x1, y1, -rad); | ||
x1 = xy.x; | ||
y1 = xy.y; | ||
xy = rotateVector(x2, y2, -rad); | ||
x2 = xy.x; | ||
y2 = xy.y; | ||
const x = (x1 - x2) / 2; | ||
const y = (y1 - y2) / 2; | ||
let h = (x * x) / (rx * rx) + (y * y) / (ry * ry); | ||
if (h > 1) { | ||
h = Math.sqrt(h); | ||
rx *= h; | ||
ry *= h; | ||
} | ||
const rx2 = rx * rx; | ||
const ry2 = ry * ry; | ||
const k = (LAF === SF ? -1 : 1) * | ||
Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))); | ||
cx = (k * rx * y) / ry + (x1 + x2) / 2; | ||
cy = (k * -ry * x) / rx + (y1 + y2) / 2; | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f1 = ((Math.asin((y1 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9); | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f2 = ((Math.asin((y2 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9); | ||
f1 = x1 < cx ? Math.PI - f1 : f1; | ||
f2 = x2 < cx ? Math.PI - f2 : f2; | ||
if (f1 < 0) | ||
f1 = Math.PI * 2 + f1; | ||
if (f2 < 0) | ||
f2 = Math.PI * 2 + f2; | ||
if (SF && f1 > f2) { | ||
f1 -= Math.PI * 2; | ||
} | ||
if (!SF && f2 > f1) { | ||
f2 -= Math.PI * 2; | ||
} | ||
} | ||
if (dot < -1) { | ||
dot = -1; | ||
else { | ||
[f1, f2, cx, cy] = recursive; | ||
} | ||
return sign * Math.acos(dot); | ||
}; | ||
var getArcCenter = function (px, py, cx, cy, rx, ry, largeArcFlag, sweepFlag, sinphi, cosphi, pxp, pyp) { | ||
var rxsq = Math.pow(rx, 2); | ||
var rysq = Math.pow(ry, 2); | ||
var pxpsq = Math.pow(pxp, 2); | ||
var pypsq = Math.pow(pyp, 2); | ||
var radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
if (radicant < 0) { | ||
radicant = 0; | ||
let df = f2 - f1; | ||
if (Math.abs(df) > d120) { | ||
const f2old = f2; | ||
const x2old = x2; | ||
const y2old = y2; | ||
f2 = f1 + d120 * (SF && f2 > f1 ? 1 : -1); | ||
x2 = cx + rx * Math.cos(f2); | ||
y2 = cy + ry * Math.sin(f2); | ||
res = arcToCubic(x2, y2, rx, ry, angle, 0, SF, x2old, y2old, [f2, f2old, cx, cy]); | ||
} | ||
radicant /= rxsq * pypsq + rysq * pxpsq; | ||
radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
var centerxp = ((radicant * rx) / ry) * pyp; | ||
var centeryp = ((radicant * -ry) / rx) * pxp; | ||
var centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
var centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
var vx1 = (pxp - centerxp) / rx; | ||
var vy1 = (pyp - centeryp) / ry; | ||
var vx2 = (-pxp - centerxp) / rx; | ||
var vy2 = (-pyp - centeryp) / ry; | ||
var ang1 = vectorAngle(1, 0, vx1, vy1); | ||
var ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
if (sweepFlag === 0 && ang2 > 0) { | ||
ang2 -= TAU; | ||
df = f2 - f1; | ||
const c1 = Math.cos(f1); | ||
const s1 = Math.sin(f1); | ||
const c2 = Math.cos(f2); | ||
const s2 = Math.sin(f2); | ||
const t = Math.tan(df / 4); | ||
const hx = (4 / 3) * rx * t; | ||
const hy = (4 / 3) * ry * t; | ||
const m1 = [x1, y1]; | ||
const m2 = [x1 + hx * s1, y1 - hy * c1]; | ||
const m3 = [x2 + hx * s2, y2 - hy * c2]; | ||
const m4 = [x2, y2]; | ||
m2[0] = 2 * m1[0] - m2[0]; | ||
m2[1] = 2 * m1[1] - m2[1]; | ||
if (recursive) { | ||
return [...m2, ...m3, ...m4, ...res]; | ||
} | ||
if (sweepFlag === 1 && ang2 < 0) { | ||
ang2 += TAU; | ||
res = [...m2, ...m3, ...m4, ...res]; | ||
const newres = []; | ||
for (let i = 0, ii = res.length; i < ii; i += 1) { | ||
newres[i] = i % 2 ? rotateVector(res[i - 1], res[i], rad).y : rotateVector(res[i], res[i + 1], rad).x; | ||
} | ||
return [centerx, centery, ang1, ang2]; | ||
}; | ||
var arcToBezier = function (_a) { | ||
var px = _a.px, py = _a.py, cx = _a.cx, cy = _a.cy, rx = _a.rx, ry = _a.ry, _b = _a.xAxisRotation, xAxisRotation = _b === void 0 ? 0 : _b, _c = _a.largeArcFlag, largeArcFlag = _c === void 0 ? 0 : _c, _d = _a.sweepFlag, sweepFlag = _d === void 0 ? 0 : _d; | ||
var curves = []; | ||
if (rx === 0 || ry === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
var sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
var cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
var pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
var pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
if (pxp === 0 && pyp === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
rx = Math.abs(rx); | ||
ry = Math.abs(ry); | ||
var lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
if (lambda > 1) { | ||
rx *= Math.sqrt(lambda); | ||
ry *= Math.sqrt(lambda); | ||
} | ||
var _e = getArcCenter(px, py, cx, cy, rx, ry, largeArcFlag, sweepFlag, sinphi, cosphi, pxp, pyp), centerx = _e[0], centery = _e[1], ang1 = _e[2], ang2 = _e[3]; | ||
// If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// this issue, we round to 1.0 when the ratio is close to 1.0. | ||
var ratio = Math.abs(ang2) / (TAU / 4); | ||
if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
ratio = 1.0; | ||
} | ||
var segments = Math.max(Math.ceil(ratio), 1); | ||
ang2 /= segments; | ||
for (var i = 0; i < segments; i++) { | ||
curves.push(approxUnitArc(ang1, ang2)); | ||
ang1 += ang2; | ||
} | ||
return curves.map(function (curve) { | ||
var _a = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery), x1 = _a.x, y1 = _a.y; | ||
var _b = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery), x2 = _b.x, y2 = _b.y; | ||
var _c = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery), x = _c.x, y = _c.y; | ||
return { x1: x1, y1: y1, x2: x2, y2: y2, x: x, y: y }; | ||
}); | ||
}; | ||
export function arcToCubic(x1, y1, rx, ry, angle, LAF, SF, x2, y2) { | ||
var curves = arcToBezier({ | ||
px: x1, | ||
py: y1, | ||
cx: x2, | ||
cy: y2, | ||
rx: rx, | ||
ry: ry, | ||
xAxisRotation: angle, | ||
largeArcFlag: LAF, | ||
sweepFlag: SF, | ||
}); | ||
return curves.reduce(function (prev, cur) { | ||
var x1 = cur.x1, y1 = cur.y1, x2 = cur.x2, y2 = cur.y2, x = cur.x, y = cur.y; | ||
prev.push(x1, y1, x2, y2, x, y); | ||
return prev; | ||
}, []); | ||
return newres; | ||
} | ||
// const TAU = Math.PI * 2; | ||
// const mapToEllipse = ( | ||
// { x, y }: { x: number; y: number }, | ||
// rx: number, | ||
// ry: number, | ||
// cosphi: number, | ||
// sinphi: number, | ||
// centerx: number, | ||
// centery: number, | ||
// ) => { | ||
// x *= rx; | ||
// y *= ry; | ||
// const xp = cosphi * x - sinphi * y; | ||
// const yp = sinphi * x + cosphi * y; | ||
// return { | ||
// x: xp + centerx, | ||
// y: yp + centery, | ||
// }; | ||
// }; | ||
// const approxUnitArc = (ang1: number, ang2: number) => { | ||
// // If 90 degree circular arc, use a constant | ||
// // as derived from http://spencermortensen.com/articles/bezier-circle | ||
// const a = | ||
// ang2 === 1.5707963267948966 | ||
// ? 0.551915024494 | ||
// : ang2 === -1.5707963267948966 | ||
// ? -0.551915024494 | ||
// : (4 / 3) * Math.tan(ang2 / 4); | ||
// const x1 = Math.cos(ang1); | ||
// const y1 = Math.sin(ang1); | ||
// const x2 = Math.cos(ang1 + ang2); | ||
// const y2 = Math.sin(ang1 + ang2); | ||
// return [ | ||
// { | ||
// x: x1 - y1 * a, | ||
// y: y1 + x1 * a, | ||
// }, | ||
// { | ||
// x: x2 + y2 * a, | ||
// y: y2 - x2 * a, | ||
// }, | ||
// { | ||
// x: x2, | ||
// y: y2, | ||
// }, | ||
// ]; | ||
// }; | ||
// const vectorAngle = (ux: number, uy: number, vx: number, vy: number) => { | ||
// const sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
// let dot = ux * vx + uy * vy; | ||
// if (dot > 1) { | ||
// dot = 1; | ||
// } | ||
// if (dot < -1) { | ||
// dot = -1; | ||
// } | ||
// return sign * Math.acos(dot); | ||
// }; | ||
// const getArcCenter = ( | ||
// px: any, | ||
// py: any, | ||
// cx: any, | ||
// cy: any, | ||
// rx: number, | ||
// ry: number, | ||
// largeArcFlag: number, | ||
// sweepFlag: number, | ||
// sinphi: number, | ||
// cosphi: number, | ||
// pxp: number, | ||
// pyp: number, | ||
// ) => { | ||
// const rxsq = Math.pow(rx, 2); | ||
// const rysq = Math.pow(ry, 2); | ||
// const pxpsq = Math.pow(pxp, 2); | ||
// const pypsq = Math.pow(pyp, 2); | ||
// let radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
// if (radicant < 0) { | ||
// radicant = 0; | ||
// } | ||
// radicant /= rxsq * pypsq + rysq * pxpsq; | ||
// radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
// const centerxp = ((radicant * rx) / ry) * pyp; | ||
// const centeryp = ((radicant * -ry) / rx) * pxp; | ||
// const centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
// const centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
// const vx1 = (pxp - centerxp) / rx; | ||
// const vy1 = (pyp - centeryp) / ry; | ||
// const vx2 = (-pxp - centerxp) / rx; | ||
// const vy2 = (-pyp - centeryp) / ry; | ||
// const ang1 = vectorAngle(1, 0, vx1, vy1); | ||
// let ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
// if (sweepFlag === 0 && ang2 > 0) { | ||
// ang2 -= TAU; | ||
// } | ||
// if (sweepFlag === 1 && ang2 < 0) { | ||
// ang2 += TAU; | ||
// } | ||
// return [centerx, centery, ang1, ang2]; | ||
// }; | ||
// const arcToBezier = ({ px, py, cx, cy, rx, ry, xAxisRotation = 0, largeArcFlag = 0, sweepFlag = 0 }) => { | ||
// const curves = []; | ||
// if (rx === 0 || ry === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
// const sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
// const cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
// const pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
// const pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
// if (pxp === 0 && pyp === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
// rx = Math.abs(rx); | ||
// ry = Math.abs(ry); | ||
// const lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
// if (lambda > 1) { | ||
// rx *= Math.sqrt(lambda); | ||
// ry *= Math.sqrt(lambda); | ||
// } | ||
// let [centerx, centery, ang1, ang2] = getArcCenter( | ||
// px, | ||
// py, | ||
// cx, | ||
// cy, | ||
// rx, | ||
// ry, | ||
// largeArcFlag, | ||
// sweepFlag, | ||
// sinphi, | ||
// cosphi, | ||
// pxp, | ||
// pyp, | ||
// ); | ||
// // If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// // 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// // unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// // this issue, we round to 1.0 when the ratio is close to 1.0. | ||
// let ratio = Math.abs(ang2) / (TAU / 4); | ||
// if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
// ratio = 1.0; | ||
// } | ||
// const segments = Math.max(Math.ceil(ratio), 1); | ||
// ang2 /= segments; | ||
// for (let i = 0; i < segments; i++) { | ||
// curves.push(approxUnitArc(ang1, ang2)); | ||
// ang1 += ang2; | ||
// } | ||
// return curves.map((curve) => { | ||
// const { x: x1, y: y1 } = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x: x2, y: y2 } = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x, y } = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery); | ||
// return { x1, y1, x2, y2, x, y }; | ||
// }); | ||
// }; | ||
// export function arcToCubic( | ||
// x1: number, | ||
// y1: number, | ||
// rx: number, | ||
// ry: number, | ||
// angle: number, | ||
// LAF: number, | ||
// SF: number, | ||
// x2: number, | ||
// y2: number, | ||
// ) { | ||
// const curves = arcToBezier({ | ||
// px: x1, | ||
// py: y1, | ||
// cx: x2, | ||
// cy: y2, | ||
// rx, | ||
// ry, | ||
// xAxisRotation: angle, | ||
// largeArcFlag: LAF, | ||
// sweepFlag: SF, | ||
// }); | ||
// return curves.reduce((prev, cur) => { | ||
// const { x1, y1, x2, y2, x, y } = cur; | ||
// prev.push(x1, y1, x2, y2, x, y); | ||
// return prev; | ||
// }, [] as number[]); | ||
// } | ||
//# sourceMappingURL=arc-2-cubic.js.map |
@@ -1,35 +0,20 @@ | ||
// export function getPointAtSegLength(p1x: number, p1y: number, c1x: number, c1y: number, c2x: number, c2y: number, p2x: number, p2y: number, t: number) { | ||
// const t1 = 1 - t; | ||
// return { | ||
// x: (t1 ** 3) * p1x | ||
// + t1 * t1 * 3 * t * c1x | ||
// + t1 * 3 * t * t * c2x | ||
// + (t ** 3) * p2x, | ||
// y: (t1 ** 3) * p1y | ||
// + t1 * t1 * 3 * t * c1y | ||
// + t1 * 3 * t * t * c2y | ||
// + (t ** 3) * p2y, | ||
// }; | ||
// } | ||
// export function midPoint(a: number[], b: number[], t: number) { | ||
// const ax = a[0]; | ||
// const ay = a[1]; | ||
// const bx = b[0]; | ||
// const by = b[1]; | ||
// return [ax + (bx - ax) * t, ay + (by - ay) * t]; | ||
// } | ||
import { segmentLineFactory } from '../util/segment-line-factory'; | ||
import { midPoint } from '../util/mid-point'; | ||
export function lineToCubic(x1, y1, x2, y2) { | ||
return [x1, y1, x2, y2, x2, y2]; | ||
// const t = 0.5; | ||
// const p0 = [x1, y1]; | ||
// const p1 = [x2, y2]; | ||
// const p2 = midPoint(p0, p1, t); | ||
// const p3 = midPoint(p1, p2, t); | ||
// const p4 = midPoint(p2, p3, t); | ||
// const p5 = midPoint(p3, p4, t); | ||
// const p6 = midPoint(p4, p5, t); | ||
// const cp1 = getPointAtSegLength.apply(0, p0.concat(p2, p4, p6, t)); | ||
// const cp2 = getPointAtSegLength.apply(0, p6.concat(p5, p3, p1, 0)); | ||
// return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
const t = 0.5; | ||
const p0 = [x1, y1]; | ||
const p1 = [x2, y2]; | ||
const p2 = midPoint(p0, p1, t); | ||
const p3 = midPoint(p1, p2, t); | ||
const p4 = midPoint(p2, p3, t); | ||
const p5 = midPoint(p3, p4, t); | ||
const p6 = midPoint(p4, p5, t); | ||
const seg1 = [...p0, ...p2, ...p4, ...p6, t]; | ||
// @ts-ignore | ||
const cp1 = segmentLineFactory(...seg1).point; | ||
const seg2 = [...p6, ...p5, ...p3, ...p1, 0]; | ||
// @ts-ignore | ||
const cp2 = segmentLineFactory(...seg2).point; | ||
return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
} | ||
//# sourceMappingURL=line-2-cubic.js.map |
export function quadToCubic(x1, y1, qx, qy, x2, y2) { | ||
var r13 = 1 / 3; | ||
var r23 = 2 / 3; | ||
const r13 = 1 / 3; | ||
const r23 = 2 / 3; | ||
return [ | ||
@@ -5,0 +5,0 @@ r13 * x1 + r23 * qx, |
@@ -1,2 +0,2 @@ | ||
import type { PathCommand, ProcessParams } from '../types'; | ||
export declare function segmentToCubic(segment: PathCommand, params: ProcessParams): PathCommand; | ||
import type { PathSegment, ParserParams, CSegment, MSegment } from '../types'; | ||
export declare function segmentToCubic(segment: PathSegment, params: ParserParams): CSegment | MSegment; |
@@ -5,30 +5,34 @@ import { arcToCubic } from './arc-2-cubic'; | ||
export function segmentToCubic(segment, params) { | ||
if ('TQ'.indexOf(segment[0]) < 0) { | ||
const [pathCommand] = segment; | ||
const values = segment.slice(1).map(Number); | ||
const [x, y] = values; | ||
let args; | ||
const { x1: px1, y1: py1, x: px, y: py } = params; | ||
if (!'TQ'.includes(pathCommand)) { | ||
params.qx = null; | ||
params.qy = null; | ||
} | ||
var _a = segment.slice(1), s1 = _a[0], s2 = _a[1]; | ||
switch (segment[0]) { | ||
switch (pathCommand) { | ||
case 'M': | ||
params.x = s1; | ||
params.y = s2; | ||
params.x = x; | ||
params.y = y; | ||
return segment; | ||
case 'A': | ||
return ['C'].concat(arcToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1)))); | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...arcToCubic(...args)]; | ||
case 'Q': | ||
params.qx = s1; | ||
params.qy = s2; | ||
return ['C'].concat(quadToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1)))); | ||
params.qx = x; | ||
params.qy = y; | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...quadToCubic(...args)]; | ||
case 'L': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, segment[1], segment[2])); | ||
case 'H': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, segment[1], params.y1)); | ||
case 'V': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, params.x1, segment[1])); | ||
return ['C', ...lineToCubic(px1, py1, x, y)]; | ||
case 'Z': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, params.x, params.y)); | ||
// prevent NaN from divide 0 | ||
if (px1 === px && py1 === py) { | ||
return ['C', px1, py1, px, py, px, py]; | ||
} | ||
return ['C', ...lineToCubic(px1, py1, px, py)]; | ||
default: | ||
@@ -35,0 +39,0 @@ } |
@@ -1,16 +0,82 @@ | ||
declare type A = ['a' | 'A', number, number, number, number, number, number, number]; | ||
declare type C = ['c' | 'C', number, number, number, number, number, number]; | ||
declare type O = ['o' | 'O', number, number]; | ||
declare type H = ['h' | 'H', number]; | ||
declare type L = ['l' | 'L', number, number]; | ||
declare type M = ['m' | 'M', number, number]; | ||
declare type R = ['r' | 'R', number, number, number, number]; | ||
declare type Q = ['q' | 'Q', number, number, number, number]; | ||
declare type S = ['s' | 'S', number, number, number, number, number, number, number]; | ||
declare type T = ['t' | 'T', number, number]; | ||
declare type V = ['v' | 'V', number]; | ||
declare type U = ['u' | 'U', number, number, number]; | ||
declare type Z = ['z' | 'Z']; | ||
export declare type PathCommand = A | C | O | H | L | M | R | Q | S | T | V | U | Z; | ||
export declare type ProcessParams = { | ||
export declare type Point = { | ||
x: number; | ||
y: number; | ||
}; | ||
export declare type MCommand = 'M'; | ||
export declare type mCommand = 'm'; | ||
export declare type LCommand = 'L'; | ||
export declare type lCommand = 'l'; | ||
export declare type VCommand = 'V'; | ||
export declare type vCommand = 'v'; | ||
export declare type HCommand = 'H'; | ||
export declare type hCommand = 'h'; | ||
export declare type ZCommand = 'Z'; | ||
export declare type zCommand = 'z'; | ||
export declare type CCommand = 'C'; | ||
export declare type cCommand = 'c'; | ||
export declare type SCommand = 'S'; | ||
export declare type sCommand = 's'; | ||
export declare type QCommand = 'Q'; | ||
export declare type qCommand = 'q'; | ||
export declare type TCommand = 'T'; | ||
export declare type tCommand = 't'; | ||
export declare type ACommand = 'A'; | ||
export declare type aCommand = 'a'; | ||
export declare type AbsoluteCommand = MCommand | LCommand | VCommand | HCommand | ZCommand | CCommand | SCommand | QCommand | TCommand | ACommand; | ||
export declare type RelativeCommand = mCommand | lCommand | vCommand | hCommand | zCommand | cCommand | sCommand | qCommand | tCommand | aCommand; | ||
export declare type PathCommand = AbsoluteCommand | RelativeCommand; | ||
export declare type MSegment = [MCommand, number, number]; | ||
export declare type mSegment = [mCommand, number, number]; | ||
export declare type MoveSegment = MSegment | mSegment; | ||
export declare type LSegment = [LCommand, number, number]; | ||
export declare type lSegment = [lCommand, number, number]; | ||
export declare type LineSegment = LSegment | lSegment; | ||
export declare type VSegment = [VCommand, number]; | ||
export declare type vSegment = [vCommand, number]; | ||
export declare type VertLineSegment = vSegment | VSegment; | ||
export declare type HSegment = [HCommand, number]; | ||
export declare type hSegment = [hCommand, number]; | ||
export declare type HorLineSegment = HSegment | hSegment; | ||
export declare type ZSegment = [ZCommand]; | ||
export declare type zSegment = [zCommand]; | ||
export declare type CloseSegment = ZSegment | zSegment; | ||
export declare type CSegment = [CCommand, number, number, number, number, number, number]; | ||
export declare type cSegment = [cCommand, number, number, number, number, number, number]; | ||
export declare type CubicSegment = CSegment | cSegment; | ||
export declare type SSegment = [SCommand, number, number, number, number]; | ||
export declare type sSegment = [sCommand, number, number, number, number]; | ||
export declare type ShortCubicSegment = SSegment | sSegment; | ||
export declare type QSegment = [QCommand, number, number, number, number]; | ||
export declare type qSegment = [qCommand, number, number, number, number]; | ||
export declare type QuadSegment = QSegment | qSegment; | ||
export declare type TSegment = [TCommand, number, number]; | ||
export declare type tSegment = [tCommand, number, number]; | ||
export declare type ShortQuadSegment = TSegment | tSegment; | ||
export declare type ASegment = [ACommand, number, number, number, number, number, number, number]; | ||
export declare type aSegment = [aCommand, number, number, number, number, number, number, number]; | ||
export declare type ArcSegment = ASegment | aSegment; | ||
export declare type PathSegment = MoveSegment | LineSegment | VertLineSegment | HorLineSegment | CloseSegment | CubicSegment | ShortCubicSegment | QuadSegment | ShortQuadSegment | ArcSegment; | ||
export interface SegmentProperties { | ||
/** the segment */ | ||
segment: PathSegment; | ||
/** the segment index */ | ||
index: number; | ||
/** the segment length */ | ||
length: number; | ||
/** the length including the segment length */ | ||
lengthAtSegment: number; | ||
[key: string]: any; | ||
} | ||
export declare type ShortSegment = VertLineSegment | HorLineSegment | ShortCubicSegment | ShortQuadSegment | CloseSegment; | ||
export declare type AbsoluteSegment = MSegment | LSegment | VSegment | HSegment | CSegment | SSegment | QSegment | TSegment | ASegment | ZSegment; | ||
export declare type RelativeSegment = mSegment | lSegment | vSegment | hSegment | cSegment | sSegment | qSegment | tSegment | aSegment | zSegment; | ||
export declare type NormalSegment = MSegment | LSegment | CSegment | QSegment | ASegment | ZSegment; | ||
export declare type PathArray = [MSegment | mSegment, ...PathSegment[]]; | ||
export declare type AbsoluteArray = [MSegment, ...AbsoluteSegment[]]; | ||
export declare type RelativeArray = [MSegment, ...RelativeSegment[]]; | ||
export declare type NormalArray = [MSegment, ...NormalSegment[]]; | ||
export declare type CurveArray = [MSegment, ...CSegment[]]; | ||
export declare type PolygonArray = [MSegment, ...LSegment[], ZSegment]; | ||
export declare type PolylineArray = [MSegment, ...LSegment[]]; | ||
export interface ParserParams { | ||
x1: number; | ||
@@ -24,3 +90,34 @@ y1: number; | ||
qy: number | null; | ||
}; | ||
export {}; | ||
} | ||
export interface PathBBox { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
x2: number; | ||
y2: number; | ||
cx: number; | ||
cy: number; | ||
cz: number; | ||
} | ||
export interface PathBBoxTotalLength extends PathBBox { | ||
length: number; | ||
} | ||
export interface SegmentLimits { | ||
min: Point; | ||
max: Point; | ||
} | ||
export interface PointProperties { | ||
closest: { | ||
x: number; | ||
y: number; | ||
}; | ||
distance: number; | ||
segment?: SegmentProperties; | ||
} | ||
export interface LengthFactory { | ||
length: number; | ||
point: Point; | ||
min: Point; | ||
max: Point; | ||
} |
@@ -10,4 +10,4 @@ "use strict"; | ||
function toHex(value) { | ||
var x16Value = Math.round(value).toString(16); | ||
return x16Value.length === 1 ? "0" + x16Value : x16Value; | ||
const x16Value = Math.round(value).toString(16); | ||
return x16Value.length === 1 ? `0${x16Value}` : x16Value; | ||
} | ||
@@ -21,5 +21,5 @@ exports.toHex = toHex; | ||
function arr2rgb(arr) { | ||
return "#" + toHex(arr[0]) + toHex(arr[1]) + toHex(arr[2]); | ||
return `#${toHex(arr[0])}${toHex(arr[1])}${toHex(arr[2])}`; | ||
} | ||
exports.arr2rgb = arr2rgb; | ||
//# sourceMappingURL=arr2rgb.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.gradient = void 0; | ||
var rgb2arr_1 = require("./rgb2arr"); | ||
var arr2rgb_1 = require("./arr2rgb"); | ||
var torgb_1 = require("./torgb"); | ||
const rgb2arr_1 = require("./rgb2arr"); | ||
const arr2rgb_1 = require("./arr2rgb"); | ||
const torgb_1 = require("./torgb"); | ||
/** | ||
@@ -25,8 +25,8 @@ * 获取颜色之间的插值 | ||
function calColor(points, percent) { | ||
var fixedPercent = isNaN(Number(percent)) || percent < 0 ? 0 : percent > 1 ? 1 : Number(percent); | ||
var steps = points.length - 1; | ||
var step = Math.floor(steps * fixedPercent); | ||
var left = steps * fixedPercent - step; | ||
var start = points[step]; | ||
var end = step === steps ? start : points[step + 1]; | ||
const fixedPercent = isNaN(Number(percent)) || percent < 0 ? 0 : percent > 1 ? 1 : Number(percent); | ||
const steps = points.length - 1; | ||
const step = Math.floor(steps * fixedPercent); | ||
const left = steps * fixedPercent - step; | ||
const start = points[step]; | ||
const end = step === steps ? start : points[step + 1]; | ||
return (0, arr2rgb_1.arr2rgb)([getValue(start, end, left, 0), getValue(start, end, left, 1), getValue(start, end, left, 2)]); | ||
@@ -40,8 +40,8 @@ } | ||
function gradient(colors) { | ||
var colorArray = typeof colors === 'string' ? colors.split('-') : colors; | ||
var points = colorArray.map(function (color) { | ||
const colorArray = typeof colors === 'string' ? colors.split('-') : colors; | ||
const points = colorArray.map((color) => { | ||
return (0, rgb2arr_1.rgb2arr)(color.indexOf('#') === -1 ? (0, torgb_1.toRGB)(color) : color); | ||
}); | ||
// 返回一个函数 | ||
return function (percent) { | ||
return (percent) => { | ||
return calColor(points, percent); | ||
@@ -48,0 +48,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toCSSGradient = void 0; | ||
var regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i; | ||
var regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i; | ||
var regexColorStop = /[\d.]+:(#[^\s]+|[^)]+\))/gi; | ||
const regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i; | ||
const regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i; | ||
const regexColorStop = /[\d.]+:(#[^\s]+|[^)]+\))/gi; | ||
function isGradientColor(val) { | ||
@@ -12,27 +12,27 @@ return /^[r,R,L,l]{1}[\s]*\(/.test(val); | ||
if (isGradientColor(gradientColor)) { | ||
var cssColor_1; | ||
var steps = void 0; | ||
let cssColor; | ||
let steps; | ||
if (gradientColor[0] === 'l') { | ||
// 线性渐变 | ||
var arr = regexLG.exec(gradientColor); | ||
var angle = +arr[1] + 90; // css 和 g 的渐变起始角度不同 | ||
const arr = regexLG.exec(gradientColor); | ||
const angle = +arr[1] + 90; // css 和 g 的渐变起始角度不同 | ||
steps = arr[2]; | ||
cssColor_1 = "linear-gradient(" + angle + "deg, "; | ||
cssColor = `linear-gradient(${angle}deg, `; | ||
} | ||
else if (gradientColor[0] === 'r') { | ||
// 径向渐变 | ||
cssColor_1 = 'radial-gradient('; | ||
var arr = regexRG.exec(gradientColor); | ||
cssColor = 'radial-gradient('; | ||
const arr = regexRG.exec(gradientColor); | ||
steps = arr[4]; | ||
} | ||
var colorStops_1 = steps.match(regexColorStop); | ||
colorStops_1.forEach(function (item, index) { | ||
var itemArr = item.split(':'); | ||
cssColor_1 += itemArr[1] + " " + Number(itemArr[0]) * 100 + "%"; | ||
if (index !== colorStops_1.length - 1) { | ||
cssColor_1 += ', '; | ||
const colorStops = steps.match(regexColorStop); | ||
colorStops.forEach((item, index) => { | ||
const itemArr = item.split(':'); | ||
cssColor += `${itemArr[1]} ${Number(itemArr[0]) * 100}%`; | ||
if (index !== colorStops.length - 1) { | ||
cssColor += ', '; | ||
} | ||
}); | ||
cssColor_1 += ')'; | ||
return cssColor_1; | ||
cssColor += ')'; | ||
return cssColor; | ||
} | ||
@@ -39,0 +39,0 @@ return gradientColor; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toRGB = void 0; | ||
var arr2rgb_1 = require("./arr2rgb"); | ||
var RGB_REG = /rgba?\(([\s.,0-9]+)\)/; | ||
const arr2rgb_1 = require("./arr2rgb"); | ||
const RGB_REG = /rgba?\(([\s.,0-9]+)\)/; | ||
/** | ||
@@ -11,3 +11,3 @@ * 创建辅助 tag 取颜色 | ||
function createTmp() { | ||
var i = document.createElement('i'); | ||
const i = document.createElement('i'); | ||
i.title = 'Web Colour Picker'; | ||
@@ -18,3 +18,3 @@ i.style.display = 'none'; | ||
} | ||
var iEl; | ||
let iEl; | ||
/** | ||
@@ -35,5 +35,5 @@ * 将颜色转换到 rgb 的格式 | ||
iEl.style.color = color; | ||
var rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color'); | ||
var matches = RGB_REG.exec(rst); | ||
var cArray = matches[1].split(/\s*,\s*/).map(function (s) { return Number(s); }); | ||
let rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color'); | ||
const matches = RGB_REG.exec(rst); | ||
const cArray = matches[1].split(/\s*,\s*/).map((s) => Number(s)); | ||
rst = (0, arr2rgb_1.arr2rgb)(cArray); | ||
@@ -40,0 +40,0 @@ return rst; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toRadian = void 0; | ||
var RADIAN = Math.PI / 180; | ||
const RADIAN = Math.PI / 180; | ||
function toRadian(degree) { | ||
@@ -6,0 +6,0 @@ return RADIAN * degree; |
@@ -7,5 +7,5 @@ "use strict"; | ||
* */ | ||
var gl_matrix_1 = require("gl-matrix"); | ||
const gl_matrix_1 = require("gl-matrix"); | ||
function leftTranslate(out, a, v) { | ||
var transMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const transMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
gl_matrix_1.mat3.fromTranslation(transMat, v); | ||
@@ -15,3 +15,3 @@ return gl_matrix_1.mat3.multiply(out, transMat, a); | ||
function leftRotate(out, a, rad) { | ||
var rotateMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const rotateMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
gl_matrix_1.mat3.fromRotation(rotateMat, rad); | ||
@@ -21,3 +21,3 @@ return gl_matrix_1.mat3.multiply(out, rotateMat, a); | ||
function leftScale(out, a, v) { | ||
var scaleMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const scaleMat = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
gl_matrix_1.mat3.fromScaling(scaleMat, v); | ||
@@ -35,5 +35,5 @@ return gl_matrix_1.mat3.multiply(out, scaleMat, a); | ||
function transform(m, actions) { | ||
var matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
for (var i = 0, len = actions.length; i < len; i++) { | ||
var action = actions[i]; | ||
const matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
for (let i = 0, len = actions.length; i < len; i++) { | ||
const action = actions[i]; | ||
switch (action[0]) { | ||
@@ -76,4 +76,4 @@ case 't': | ||
function angleTo(v1, v2, direct) { | ||
var ang = gl_matrix_1.vec2.angle(v1, v2); | ||
var angleLargeThanPI = direction(v1, v2) >= 0; | ||
const ang = gl_matrix_1.vec2.angle(v1, v2); | ||
const angleLargeThanPI = direction(v1, v2) >= 0; | ||
if (direct) { | ||
@@ -80,0 +80,0 @@ if (angleLargeThanPI) { |
@@ -1,16 +0,17 @@ | ||
export { parsePath } from './parse-path'; | ||
export { catmullRom2Bezier } from './catmull-rom-2-bezier'; | ||
export { fillPath } from './fill-path'; | ||
export { fillPathByDiff } from './fill-path-by-diff'; | ||
export { formatPath } from './format-path'; | ||
export { pathIntersection } from './path-intersection'; | ||
export { parsePathArray } from './parse-path-array'; | ||
export { parsePathString } from './parse-path-string'; | ||
export { path2Curve } from './path-2-curve'; | ||
export { path2Absolute } from './path-2-absolute'; | ||
export { rectPath } from './rect-path'; | ||
export { getArcParams } from './get-arc-params'; | ||
export { path2Segments } from './path-2-segments'; | ||
export { getLineIntersect } from './get-line-intersect'; | ||
export { isPolygonsIntersect } from './is-polygons-intersect'; | ||
export { isPointInPolygon } from './point-in-polygon'; | ||
export { path2String } from './convert/path-2-string'; | ||
export { path2Curve } from './convert/path-2-curve'; | ||
export { path2Absolute } from './convert/path-2-absolute'; | ||
export { clonePath } from './process/clone-path'; | ||
export { normalizePath } from './process/normalize-path'; | ||
export { reverseCurve } from './process/reverse-curve'; | ||
export { getPathBBox } from './util/get-path-bbox'; | ||
export { getTotalLength } from './util/get-total-length'; | ||
export { getPathBBoxTotalLength } from './util/get-path-bbox-total-length'; | ||
export { getRotatedCurve } from './util/get-rotated-curve'; | ||
export { getPathArea } from './util/get-path-area'; | ||
export { getDrawDirection } from './util/get-draw-direction'; | ||
export { getPointAtLength } from './util/get-point-at-length'; | ||
export { isPointInStroke } from './util/is-point-in-stroke'; | ||
export { distanceSquareRoot } from './util/distance-square-root'; | ||
export { equalizeSegments } from './util/equalize-segments'; | ||
export * from './types'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPointInPolygon = exports.isPolygonsIntersect = exports.getLineIntersect = exports.path2Segments = exports.getArcParams = exports.rectPath = exports.path2Absolute = exports.path2Curve = exports.parsePathString = exports.parsePathArray = exports.pathIntersection = exports.formatPath = exports.fillPathByDiff = exports.fillPath = exports.catmullRom2Bezier = exports.parsePath = void 0; | ||
var parse_path_1 = require("./parse-path"); | ||
Object.defineProperty(exports, "parsePath", { enumerable: true, get: function () { return parse_path_1.parsePath; } }); | ||
var catmull_rom_2_bezier_1 = require("./catmull-rom-2-bezier"); | ||
Object.defineProperty(exports, "catmullRom2Bezier", { enumerable: true, get: function () { return catmull_rom_2_bezier_1.catmullRom2Bezier; } }); | ||
var fill_path_1 = require("./fill-path"); | ||
Object.defineProperty(exports, "fillPath", { enumerable: true, get: function () { return fill_path_1.fillPath; } }); | ||
var fill_path_by_diff_1 = require("./fill-path-by-diff"); | ||
Object.defineProperty(exports, "fillPathByDiff", { enumerable: true, get: function () { return fill_path_by_diff_1.fillPathByDiff; } }); | ||
var format_path_1 = require("./format-path"); | ||
Object.defineProperty(exports, "formatPath", { enumerable: true, get: function () { return format_path_1.formatPath; } }); | ||
var path_intersection_1 = require("./path-intersection"); | ||
Object.defineProperty(exports, "pathIntersection", { enumerable: true, get: function () { return path_intersection_1.pathIntersection; } }); | ||
var parse_path_array_1 = require("./parse-path-array"); | ||
Object.defineProperty(exports, "parsePathArray", { enumerable: true, get: function () { return parse_path_array_1.parsePathArray; } }); | ||
var parse_path_string_1 = require("./parse-path-string"); | ||
Object.defineProperty(exports, "parsePathString", { enumerable: true, get: function () { return parse_path_string_1.parsePathString; } }); | ||
var path_2_curve_1 = require("./path-2-curve"); | ||
exports.equalizeSegments = exports.distanceSquareRoot = exports.isPointInStroke = exports.getPointAtLength = exports.getDrawDirection = exports.getPathArea = exports.getRotatedCurve = exports.getPathBBoxTotalLength = exports.getTotalLength = exports.getPathBBox = exports.reverseCurve = exports.normalizePath = exports.clonePath = exports.path2Absolute = exports.path2Curve = exports.path2String = void 0; | ||
var path_2_string_1 = require("./convert/path-2-string"); | ||
Object.defineProperty(exports, "path2String", { enumerable: true, get: function () { return path_2_string_1.path2String; } }); | ||
var path_2_curve_1 = require("./convert/path-2-curve"); | ||
Object.defineProperty(exports, "path2Curve", { enumerable: true, get: function () { return path_2_curve_1.path2Curve; } }); | ||
var path_2_absolute_1 = require("./path-2-absolute"); | ||
var path_2_absolute_1 = require("./convert/path-2-absolute"); | ||
Object.defineProperty(exports, "path2Absolute", { enumerable: true, get: function () { return path_2_absolute_1.path2Absolute; } }); | ||
var rect_path_1 = require("./rect-path"); | ||
Object.defineProperty(exports, "rectPath", { enumerable: true, get: function () { return rect_path_1.rectPath; } }); | ||
var get_arc_params_1 = require("./get-arc-params"); | ||
Object.defineProperty(exports, "getArcParams", { enumerable: true, get: function () { return get_arc_params_1.getArcParams; } }); | ||
var path_2_segments_1 = require("./path-2-segments"); | ||
Object.defineProperty(exports, "path2Segments", { enumerable: true, get: function () { return path_2_segments_1.path2Segments; } }); | ||
var get_line_intersect_1 = require("./get-line-intersect"); | ||
Object.defineProperty(exports, "getLineIntersect", { enumerable: true, get: function () { return get_line_intersect_1.getLineIntersect; } }); | ||
var is_polygons_intersect_1 = require("./is-polygons-intersect"); | ||
Object.defineProperty(exports, "isPolygonsIntersect", { enumerable: true, get: function () { return is_polygons_intersect_1.isPolygonsIntersect; } }); | ||
var point_in_polygon_1 = require("./point-in-polygon"); | ||
Object.defineProperty(exports, "isPointInPolygon", { enumerable: true, get: function () { return point_in_polygon_1.isPointInPolygon; } }); | ||
var clone_path_1 = require("./process/clone-path"); | ||
Object.defineProperty(exports, "clonePath", { enumerable: true, get: function () { return clone_path_1.clonePath; } }); | ||
var normalize_path_1 = require("./process/normalize-path"); | ||
Object.defineProperty(exports, "normalizePath", { enumerable: true, get: function () { return normalize_path_1.normalizePath; } }); | ||
var reverse_curve_1 = require("./process/reverse-curve"); | ||
Object.defineProperty(exports, "reverseCurve", { enumerable: true, get: function () { return reverse_curve_1.reverseCurve; } }); | ||
var get_path_bbox_1 = require("./util/get-path-bbox"); | ||
Object.defineProperty(exports, "getPathBBox", { enumerable: true, get: function () { return get_path_bbox_1.getPathBBox; } }); | ||
var get_total_length_1 = require("./util/get-total-length"); | ||
Object.defineProperty(exports, "getTotalLength", { enumerable: true, get: function () { return get_total_length_1.getTotalLength; } }); | ||
var get_path_bbox_total_length_1 = require("./util/get-path-bbox-total-length"); | ||
Object.defineProperty(exports, "getPathBBoxTotalLength", { enumerable: true, get: function () { return get_path_bbox_total_length_1.getPathBBoxTotalLength; } }); | ||
var get_rotated_curve_1 = require("./util/get-rotated-curve"); | ||
Object.defineProperty(exports, "getRotatedCurve", { enumerable: true, get: function () { return get_rotated_curve_1.getRotatedCurve; } }); | ||
var get_path_area_1 = require("./util/get-path-area"); | ||
Object.defineProperty(exports, "getPathArea", { enumerable: true, get: function () { return get_path_area_1.getPathArea; } }); | ||
var get_draw_direction_1 = require("./util/get-draw-direction"); | ||
Object.defineProperty(exports, "getDrawDirection", { enumerable: true, get: function () { return get_draw_direction_1.getDrawDirection; } }); | ||
var get_point_at_length_1 = require("./util/get-point-at-length"); | ||
Object.defineProperty(exports, "getPointAtLength", { enumerable: true, get: function () { return get_point_at_length_1.getPointAtLength; } }); | ||
var is_point_in_stroke_1 = require("./util/is-point-in-stroke"); | ||
Object.defineProperty(exports, "isPointInStroke", { enumerable: true, get: function () { return is_point_in_stroke_1.isPointInStroke; } }); | ||
var distance_square_root_1 = require("./util/distance-square-root"); | ||
Object.defineProperty(exports, "distanceSquareRoot", { enumerable: true, get: function () { return distance_square_root_1.distanceSquareRoot; } }); | ||
var equalize_segments_1 = require("./util/equalize-segments"); | ||
Object.defineProperty(exports, "equalizeSegments", { enumerable: true, get: function () { return equalize_segments_1.equalizeSegments; } }); | ||
__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,7 @@ | ||
export declare function arcToCubic(x1: number, y1: number, rx: number, ry: number, angle: number, LAF: number, SF: number, x2: number, y2: number): number[]; | ||
/** | ||
* Converts A (arc-to) segments to C (cubic-bezier-to). | ||
* | ||
* For more information of where this math came from visit: | ||
* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
*/ | ||
export declare function arcToCubic(X1: number, Y1: number, RX: number, RY: number, angle: number, LAF: number, SF: number, X2: number, Y2: number, recursive: number[]): any[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.arcToCubic = void 0; | ||
var TAU = Math.PI * 2; | ||
var mapToEllipse = function (_a, rx, ry, cosphi, sinphi, centerx, centery) { | ||
var x = _a.x, y = _a.y; | ||
x *= rx; | ||
y *= ry; | ||
var xp = cosphi * x - sinphi * y; | ||
var yp = sinphi * x + cosphi * y; | ||
return { | ||
x: xp + centerx, | ||
y: yp + centery, | ||
}; | ||
}; | ||
var approxUnitArc = function (ang1, ang2) { | ||
// If 90 degree circular arc, use a constant | ||
// as derived from http://spencermortensen.com/articles/bezier-circle | ||
var a = ang2 === 1.5707963267948966 | ||
? 0.551915024494 | ||
: ang2 === -1.5707963267948966 | ||
? -0.551915024494 | ||
: (4 / 3) * Math.tan(ang2 / 4); | ||
var x1 = Math.cos(ang1); | ||
var y1 = Math.sin(ang1); | ||
var x2 = Math.cos(ang1 + ang2); | ||
var y2 = Math.sin(ang1 + ang2); | ||
return [ | ||
{ | ||
x: x1 - y1 * a, | ||
y: y1 + x1 * a, | ||
}, | ||
{ | ||
x: x2 + y2 * a, | ||
y: y2 - x2 * a, | ||
}, | ||
{ | ||
x: x2, | ||
y: y2, | ||
}, | ||
]; | ||
}; | ||
var vectorAngle = function (ux, uy, vx, vy) { | ||
var sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
var dot = ux * vx + uy * vy; | ||
if (dot > 1) { | ||
dot = 1; | ||
const rotate_vector_1 = require("../util/rotate-vector"); | ||
/** | ||
* Converts A (arc-to) segments to C (cubic-bezier-to). | ||
* | ||
* For more information of where this math came from visit: | ||
* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
*/ | ||
function arcToCubic(X1, Y1, RX, RY, angle, LAF, SF, X2, Y2, recursive) { | ||
let x1 = X1; | ||
let y1 = Y1; | ||
let rx = RX; | ||
let ry = RY; | ||
let x2 = X2; | ||
let y2 = Y2; | ||
// for more information of where this Math came from visit: | ||
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
const d120 = (Math.PI * 120) / 180; | ||
const rad = (Math.PI / 180) * (+angle || 0); | ||
/** @type {number[]} */ | ||
let res = []; | ||
let xy; | ||
let f1; | ||
let f2; | ||
let cx; | ||
let cy; | ||
if (!recursive) { | ||
xy = (0, rotate_vector_1.rotateVector)(x1, y1, -rad); | ||
x1 = xy.x; | ||
y1 = xy.y; | ||
xy = (0, rotate_vector_1.rotateVector)(x2, y2, -rad); | ||
x2 = xy.x; | ||
y2 = xy.y; | ||
const x = (x1 - x2) / 2; | ||
const y = (y1 - y2) / 2; | ||
let h = (x * x) / (rx * rx) + (y * y) / (ry * ry); | ||
if (h > 1) { | ||
h = Math.sqrt(h); | ||
rx *= h; | ||
ry *= h; | ||
} | ||
const rx2 = rx * rx; | ||
const ry2 = ry * ry; | ||
const k = (LAF === SF ? -1 : 1) * | ||
Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))); | ||
cx = (k * rx * y) / ry + (x1 + x2) / 2; | ||
cy = (k * -ry * x) / rx + (y1 + y2) / 2; | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f1 = ((Math.asin((y1 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9); | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f2 = ((Math.asin((y2 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9); | ||
f1 = x1 < cx ? Math.PI - f1 : f1; | ||
f2 = x2 < cx ? Math.PI - f2 : f2; | ||
if (f1 < 0) | ||
f1 = Math.PI * 2 + f1; | ||
if (f2 < 0) | ||
f2 = Math.PI * 2 + f2; | ||
if (SF && f1 > f2) { | ||
f1 -= Math.PI * 2; | ||
} | ||
if (!SF && f2 > f1) { | ||
f2 -= Math.PI * 2; | ||
} | ||
} | ||
if (dot < -1) { | ||
dot = -1; | ||
else { | ||
[f1, f2, cx, cy] = recursive; | ||
} | ||
return sign * Math.acos(dot); | ||
}; | ||
var getArcCenter = function (px, py, cx, cy, rx, ry, largeArcFlag, sweepFlag, sinphi, cosphi, pxp, pyp) { | ||
var rxsq = Math.pow(rx, 2); | ||
var rysq = Math.pow(ry, 2); | ||
var pxpsq = Math.pow(pxp, 2); | ||
var pypsq = Math.pow(pyp, 2); | ||
var radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
if (radicant < 0) { | ||
radicant = 0; | ||
let df = f2 - f1; | ||
if (Math.abs(df) > d120) { | ||
const f2old = f2; | ||
const x2old = x2; | ||
const y2old = y2; | ||
f2 = f1 + d120 * (SF && f2 > f1 ? 1 : -1); | ||
x2 = cx + rx * Math.cos(f2); | ||
y2 = cy + ry * Math.sin(f2); | ||
res = arcToCubic(x2, y2, rx, ry, angle, 0, SF, x2old, y2old, [f2, f2old, cx, cy]); | ||
} | ||
radicant /= rxsq * pypsq + rysq * pxpsq; | ||
radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
var centerxp = ((radicant * rx) / ry) * pyp; | ||
var centeryp = ((radicant * -ry) / rx) * pxp; | ||
var centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
var centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
var vx1 = (pxp - centerxp) / rx; | ||
var vy1 = (pyp - centeryp) / ry; | ||
var vx2 = (-pxp - centerxp) / rx; | ||
var vy2 = (-pyp - centeryp) / ry; | ||
var ang1 = vectorAngle(1, 0, vx1, vy1); | ||
var ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
if (sweepFlag === 0 && ang2 > 0) { | ||
ang2 -= TAU; | ||
df = f2 - f1; | ||
const c1 = Math.cos(f1); | ||
const s1 = Math.sin(f1); | ||
const c2 = Math.cos(f2); | ||
const s2 = Math.sin(f2); | ||
const t = Math.tan(df / 4); | ||
const hx = (4 / 3) * rx * t; | ||
const hy = (4 / 3) * ry * t; | ||
const m1 = [x1, y1]; | ||
const m2 = [x1 + hx * s1, y1 - hy * c1]; | ||
const m3 = [x2 + hx * s2, y2 - hy * c2]; | ||
const m4 = [x2, y2]; | ||
m2[0] = 2 * m1[0] - m2[0]; | ||
m2[1] = 2 * m1[1] - m2[1]; | ||
if (recursive) { | ||
return [...m2, ...m3, ...m4, ...res]; | ||
} | ||
if (sweepFlag === 1 && ang2 < 0) { | ||
ang2 += TAU; | ||
res = [...m2, ...m3, ...m4, ...res]; | ||
const newres = []; | ||
for (let i = 0, ii = res.length; i < ii; i += 1) { | ||
newres[i] = i % 2 ? (0, rotate_vector_1.rotateVector)(res[i - 1], res[i], rad).y : (0, rotate_vector_1.rotateVector)(res[i], res[i + 1], rad).x; | ||
} | ||
return [centerx, centery, ang1, ang2]; | ||
}; | ||
var arcToBezier = function (_a) { | ||
var px = _a.px, py = _a.py, cx = _a.cx, cy = _a.cy, rx = _a.rx, ry = _a.ry, _b = _a.xAxisRotation, xAxisRotation = _b === void 0 ? 0 : _b, _c = _a.largeArcFlag, largeArcFlag = _c === void 0 ? 0 : _c, _d = _a.sweepFlag, sweepFlag = _d === void 0 ? 0 : _d; | ||
var curves = []; | ||
if (rx === 0 || ry === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
var sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
var cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
var pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
var pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
if (pxp === 0 && pyp === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
rx = Math.abs(rx); | ||
ry = Math.abs(ry); | ||
var lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
if (lambda > 1) { | ||
rx *= Math.sqrt(lambda); | ||
ry *= Math.sqrt(lambda); | ||
} | ||
var _e = getArcCenter(px, py, cx, cy, rx, ry, largeArcFlag, sweepFlag, sinphi, cosphi, pxp, pyp), centerx = _e[0], centery = _e[1], ang1 = _e[2], ang2 = _e[3]; | ||
// If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// this issue, we round to 1.0 when the ratio is close to 1.0. | ||
var ratio = Math.abs(ang2) / (TAU / 4); | ||
if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
ratio = 1.0; | ||
} | ||
var segments = Math.max(Math.ceil(ratio), 1); | ||
ang2 /= segments; | ||
for (var i = 0; i < segments; i++) { | ||
curves.push(approxUnitArc(ang1, ang2)); | ||
ang1 += ang2; | ||
} | ||
return curves.map(function (curve) { | ||
var _a = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery), x1 = _a.x, y1 = _a.y; | ||
var _b = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery), x2 = _b.x, y2 = _b.y; | ||
var _c = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery), x = _c.x, y = _c.y; | ||
return { x1: x1, y1: y1, x2: x2, y2: y2, x: x, y: y }; | ||
}); | ||
}; | ||
function arcToCubic(x1, y1, rx, ry, angle, LAF, SF, x2, y2) { | ||
var curves = arcToBezier({ | ||
px: x1, | ||
py: y1, | ||
cx: x2, | ||
cy: y2, | ||
rx: rx, | ||
ry: ry, | ||
xAxisRotation: angle, | ||
largeArcFlag: LAF, | ||
sweepFlag: SF, | ||
}); | ||
return curves.reduce(function (prev, cur) { | ||
var x1 = cur.x1, y1 = cur.y1, x2 = cur.x2, y2 = cur.y2, x = cur.x, y = cur.y; | ||
prev.push(x1, y1, x2, y2, x, y); | ||
return prev; | ||
}, []); | ||
return newres; | ||
} | ||
exports.arcToCubic = arcToCubic; | ||
// const TAU = Math.PI * 2; | ||
// const mapToEllipse = ( | ||
// { x, y }: { x: number; y: number }, | ||
// rx: number, | ||
// ry: number, | ||
// cosphi: number, | ||
// sinphi: number, | ||
// centerx: number, | ||
// centery: number, | ||
// ) => { | ||
// x *= rx; | ||
// y *= ry; | ||
// const xp = cosphi * x - sinphi * y; | ||
// const yp = sinphi * x + cosphi * y; | ||
// return { | ||
// x: xp + centerx, | ||
// y: yp + centery, | ||
// }; | ||
// }; | ||
// const approxUnitArc = (ang1: number, ang2: number) => { | ||
// // If 90 degree circular arc, use a constant | ||
// // as derived from http://spencermortensen.com/articles/bezier-circle | ||
// const a = | ||
// ang2 === 1.5707963267948966 | ||
// ? 0.551915024494 | ||
// : ang2 === -1.5707963267948966 | ||
// ? -0.551915024494 | ||
// : (4 / 3) * Math.tan(ang2 / 4); | ||
// const x1 = Math.cos(ang1); | ||
// const y1 = Math.sin(ang1); | ||
// const x2 = Math.cos(ang1 + ang2); | ||
// const y2 = Math.sin(ang1 + ang2); | ||
// return [ | ||
// { | ||
// x: x1 - y1 * a, | ||
// y: y1 + x1 * a, | ||
// }, | ||
// { | ||
// x: x2 + y2 * a, | ||
// y: y2 - x2 * a, | ||
// }, | ||
// { | ||
// x: x2, | ||
// y: y2, | ||
// }, | ||
// ]; | ||
// }; | ||
// const vectorAngle = (ux: number, uy: number, vx: number, vy: number) => { | ||
// const sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
// let dot = ux * vx + uy * vy; | ||
// if (dot > 1) { | ||
// dot = 1; | ||
// } | ||
// if (dot < -1) { | ||
// dot = -1; | ||
// } | ||
// return sign * Math.acos(dot); | ||
// }; | ||
// const getArcCenter = ( | ||
// px: any, | ||
// py: any, | ||
// cx: any, | ||
// cy: any, | ||
// rx: number, | ||
// ry: number, | ||
// largeArcFlag: number, | ||
// sweepFlag: number, | ||
// sinphi: number, | ||
// cosphi: number, | ||
// pxp: number, | ||
// pyp: number, | ||
// ) => { | ||
// const rxsq = Math.pow(rx, 2); | ||
// const rysq = Math.pow(ry, 2); | ||
// const pxpsq = Math.pow(pxp, 2); | ||
// const pypsq = Math.pow(pyp, 2); | ||
// let radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
// if (radicant < 0) { | ||
// radicant = 0; | ||
// } | ||
// radicant /= rxsq * pypsq + rysq * pxpsq; | ||
// radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
// const centerxp = ((radicant * rx) / ry) * pyp; | ||
// const centeryp = ((radicant * -ry) / rx) * pxp; | ||
// const centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
// const centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
// const vx1 = (pxp - centerxp) / rx; | ||
// const vy1 = (pyp - centeryp) / ry; | ||
// const vx2 = (-pxp - centerxp) / rx; | ||
// const vy2 = (-pyp - centeryp) / ry; | ||
// const ang1 = vectorAngle(1, 0, vx1, vy1); | ||
// let ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
// if (sweepFlag === 0 && ang2 > 0) { | ||
// ang2 -= TAU; | ||
// } | ||
// if (sweepFlag === 1 && ang2 < 0) { | ||
// ang2 += TAU; | ||
// } | ||
// return [centerx, centery, ang1, ang2]; | ||
// }; | ||
// const arcToBezier = ({ px, py, cx, cy, rx, ry, xAxisRotation = 0, largeArcFlag = 0, sweepFlag = 0 }) => { | ||
// const curves = []; | ||
// if (rx === 0 || ry === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
// const sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
// const cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
// const pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
// const pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
// if (pxp === 0 && pyp === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
// rx = Math.abs(rx); | ||
// ry = Math.abs(ry); | ||
// const lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
// if (lambda > 1) { | ||
// rx *= Math.sqrt(lambda); | ||
// ry *= Math.sqrt(lambda); | ||
// } | ||
// let [centerx, centery, ang1, ang2] = getArcCenter( | ||
// px, | ||
// py, | ||
// cx, | ||
// cy, | ||
// rx, | ||
// ry, | ||
// largeArcFlag, | ||
// sweepFlag, | ||
// sinphi, | ||
// cosphi, | ||
// pxp, | ||
// pyp, | ||
// ); | ||
// // If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// // 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// // unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// // this issue, we round to 1.0 when the ratio is close to 1.0. | ||
// let ratio = Math.abs(ang2) / (TAU / 4); | ||
// if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
// ratio = 1.0; | ||
// } | ||
// const segments = Math.max(Math.ceil(ratio), 1); | ||
// ang2 /= segments; | ||
// for (let i = 0; i < segments; i++) { | ||
// curves.push(approxUnitArc(ang1, ang2)); | ||
// ang1 += ang2; | ||
// } | ||
// return curves.map((curve) => { | ||
// const { x: x1, y: y1 } = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x: x2, y: y2 } = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x, y } = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery); | ||
// return { x1, y1, x2, y2, x, y }; | ||
// }); | ||
// }; | ||
// export function arcToCubic( | ||
// x1: number, | ||
// y1: number, | ||
// rx: number, | ||
// ry: number, | ||
// angle: number, | ||
// LAF: number, | ||
// SF: number, | ||
// x2: number, | ||
// y2: number, | ||
// ) { | ||
// const curves = arcToBezier({ | ||
// px: x1, | ||
// py: y1, | ||
// cx: x2, | ||
// cy: y2, | ||
// rx, | ||
// ry, | ||
// xAxisRotation: angle, | ||
// largeArcFlag: LAF, | ||
// sweepFlag: SF, | ||
// }); | ||
// return curves.reduce((prev, cur) => { | ||
// const { x1, y1, x2, y2, x, y } = cur; | ||
// prev.push(x1, y1, x2, y2, x, y); | ||
// return prev; | ||
// }, [] as number[]); | ||
// } | ||
//# sourceMappingURL=arc-2-cubic.js.map |
"use strict"; | ||
// export function getPointAtSegLength(p1x: number, p1y: number, c1x: number, c1y: number, c2x: number, c2y: number, p2x: number, p2y: number, t: number) { | ||
// const t1 = 1 - t; | ||
// return { | ||
// x: (t1 ** 3) * p1x | ||
// + t1 * t1 * 3 * t * c1x | ||
// + t1 * 3 * t * t * c2x | ||
// + (t ** 3) * p2x, | ||
// y: (t1 ** 3) * p1y | ||
// + t1 * t1 * 3 * t * c1y | ||
// + t1 * 3 * t * t * c2y | ||
// + (t ** 3) * p2y, | ||
// }; | ||
// } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lineToCubic = void 0; | ||
// export function midPoint(a: number[], b: number[], t: number) { | ||
// const ax = a[0]; | ||
// const ay = a[1]; | ||
// const bx = b[0]; | ||
// const by = b[1]; | ||
// return [ax + (bx - ax) * t, ay + (by - ay) * t]; | ||
// } | ||
const segment_line_factory_1 = require("../util/segment-line-factory"); | ||
const mid_point_1 = require("../util/mid-point"); | ||
function lineToCubic(x1, y1, x2, y2) { | ||
return [x1, y1, x2, y2, x2, y2]; | ||
// const t = 0.5; | ||
// const p0 = [x1, y1]; | ||
// const p1 = [x2, y2]; | ||
// const p2 = midPoint(p0, p1, t); | ||
// const p3 = midPoint(p1, p2, t); | ||
// const p4 = midPoint(p2, p3, t); | ||
// const p5 = midPoint(p3, p4, t); | ||
// const p6 = midPoint(p4, p5, t); | ||
// const cp1 = getPointAtSegLength.apply(0, p0.concat(p2, p4, p6, t)); | ||
// const cp2 = getPointAtSegLength.apply(0, p6.concat(p5, p3, p1, 0)); | ||
// return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
const t = 0.5; | ||
const p0 = [x1, y1]; | ||
const p1 = [x2, y2]; | ||
const p2 = (0, mid_point_1.midPoint)(p0, p1, t); | ||
const p3 = (0, mid_point_1.midPoint)(p1, p2, t); | ||
const p4 = (0, mid_point_1.midPoint)(p2, p3, t); | ||
const p5 = (0, mid_point_1.midPoint)(p3, p4, t); | ||
const p6 = (0, mid_point_1.midPoint)(p4, p5, t); | ||
const seg1 = [...p0, ...p2, ...p4, ...p6, t]; | ||
// @ts-ignore | ||
const cp1 = (0, segment_line_factory_1.segmentLineFactory)(...seg1).point; | ||
const seg2 = [...p6, ...p5, ...p3, ...p1, 0]; | ||
// @ts-ignore | ||
const cp2 = (0, segment_line_factory_1.segmentLineFactory)(...seg2).point; | ||
return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
} | ||
exports.lineToCubic = lineToCubic; | ||
//# sourceMappingURL=line-2-cubic.js.map |
@@ -5,4 +5,4 @@ "use strict"; | ||
function quadToCubic(x1, y1, qx, qy, x2, y2) { | ||
var r13 = 1 / 3; | ||
var r23 = 2 / 3; | ||
const r13 = 1 / 3; | ||
const r23 = 2 / 3; | ||
return [ | ||
@@ -9,0 +9,0 @@ r13 * x1 + r23 * qx, |
@@ -1,2 +0,2 @@ | ||
import type { PathCommand, ProcessParams } from '../types'; | ||
export declare function segmentToCubic(segment: PathCommand, params: ProcessParams): PathCommand; | ||
import type { PathSegment, ParserParams, CSegment, MSegment } from '../types'; | ||
export declare function segmentToCubic(segment: PathSegment, params: ParserParams): CSegment | MSegment; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.segmentToCubic = void 0; | ||
var arc_2_cubic_1 = require("./arc-2-cubic"); | ||
var quad_2_cubic_1 = require("./quad-2-cubic"); | ||
var line_2_cubic_1 = require("./line-2-cubic"); | ||
const arc_2_cubic_1 = require("./arc-2-cubic"); | ||
const quad_2_cubic_1 = require("./quad-2-cubic"); | ||
const line_2_cubic_1 = require("./line-2-cubic"); | ||
function segmentToCubic(segment, params) { | ||
if ('TQ'.indexOf(segment[0]) < 0) { | ||
const [pathCommand] = segment; | ||
const values = segment.slice(1).map(Number); | ||
const [x, y] = values; | ||
let args; | ||
const { x1: px1, y1: py1, x: px, y: py } = params; | ||
if (!'TQ'.includes(pathCommand)) { | ||
params.qx = null; | ||
params.qy = null; | ||
} | ||
var _a = segment.slice(1), s1 = _a[0], s2 = _a[1]; | ||
switch (segment[0]) { | ||
switch (pathCommand) { | ||
case 'M': | ||
params.x = s1; | ||
params.y = s2; | ||
params.x = x; | ||
params.y = y; | ||
return segment; | ||
case 'A': | ||
return ['C'].concat(arc_2_cubic_1.arcToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1)))); | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...(0, arc_2_cubic_1.arcToCubic)(...args)]; | ||
case 'Q': | ||
params.qx = s1; | ||
params.qy = s2; | ||
return ['C'].concat(quad_2_cubic_1.quadToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1)))); | ||
params.qx = x; | ||
params.qy = y; | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...(0, quad_2_cubic_1.quadToCubic)(...args)]; | ||
case 'L': | ||
// @ts-ignore | ||
return ['C'].concat((0, line_2_cubic_1.lineToCubic)(params.x1, params.y1, segment[1], segment[2])); | ||
case 'H': | ||
// @ts-ignore | ||
return ['C'].concat((0, line_2_cubic_1.lineToCubic)(params.x1, params.y1, segment[1], params.y1)); | ||
case 'V': | ||
// @ts-ignore | ||
return ['C'].concat((0, line_2_cubic_1.lineToCubic)(params.x1, params.y1, params.x1, segment[1])); | ||
return ['C', ...(0, line_2_cubic_1.lineToCubic)(px1, py1, x, y)]; | ||
case 'Z': | ||
// @ts-ignore | ||
return ['C'].concat((0, line_2_cubic_1.lineToCubic)(params.x1, params.y1, params.x, params.y)); | ||
// prevent NaN from divide 0 | ||
if (px1 === px && py1 === py) { | ||
return ['C', px1, py1, px, py, px, py]; | ||
} | ||
return ['C', ...(0, line_2_cubic_1.lineToCubic)(px1, py1, px, py)]; | ||
default: | ||
@@ -37,0 +41,0 @@ } |
@@ -1,16 +0,82 @@ | ||
declare type A = ['a' | 'A', number, number, number, number, number, number, number]; | ||
declare type C = ['c' | 'C', number, number, number, number, number, number]; | ||
declare type O = ['o' | 'O', number, number]; | ||
declare type H = ['h' | 'H', number]; | ||
declare type L = ['l' | 'L', number, number]; | ||
declare type M = ['m' | 'M', number, number]; | ||
declare type R = ['r' | 'R', number, number, number, number]; | ||
declare type Q = ['q' | 'Q', number, number, number, number]; | ||
declare type S = ['s' | 'S', number, number, number, number, number, number, number]; | ||
declare type T = ['t' | 'T', number, number]; | ||
declare type V = ['v' | 'V', number]; | ||
declare type U = ['u' | 'U', number, number, number]; | ||
declare type Z = ['z' | 'Z']; | ||
export declare type PathCommand = A | C | O | H | L | M | R | Q | S | T | V | U | Z; | ||
export declare type ProcessParams = { | ||
export declare type Point = { | ||
x: number; | ||
y: number; | ||
}; | ||
export declare type MCommand = 'M'; | ||
export declare type mCommand = 'm'; | ||
export declare type LCommand = 'L'; | ||
export declare type lCommand = 'l'; | ||
export declare type VCommand = 'V'; | ||
export declare type vCommand = 'v'; | ||
export declare type HCommand = 'H'; | ||
export declare type hCommand = 'h'; | ||
export declare type ZCommand = 'Z'; | ||
export declare type zCommand = 'z'; | ||
export declare type CCommand = 'C'; | ||
export declare type cCommand = 'c'; | ||
export declare type SCommand = 'S'; | ||
export declare type sCommand = 's'; | ||
export declare type QCommand = 'Q'; | ||
export declare type qCommand = 'q'; | ||
export declare type TCommand = 'T'; | ||
export declare type tCommand = 't'; | ||
export declare type ACommand = 'A'; | ||
export declare type aCommand = 'a'; | ||
export declare type AbsoluteCommand = MCommand | LCommand | VCommand | HCommand | ZCommand | CCommand | SCommand | QCommand | TCommand | ACommand; | ||
export declare type RelativeCommand = mCommand | lCommand | vCommand | hCommand | zCommand | cCommand | sCommand | qCommand | tCommand | aCommand; | ||
export declare type PathCommand = AbsoluteCommand | RelativeCommand; | ||
export declare type MSegment = [MCommand, number, number]; | ||
export declare type mSegment = [mCommand, number, number]; | ||
export declare type MoveSegment = MSegment | mSegment; | ||
export declare type LSegment = [LCommand, number, number]; | ||
export declare type lSegment = [lCommand, number, number]; | ||
export declare type LineSegment = LSegment | lSegment; | ||
export declare type VSegment = [VCommand, number]; | ||
export declare type vSegment = [vCommand, number]; | ||
export declare type VertLineSegment = vSegment | VSegment; | ||
export declare type HSegment = [HCommand, number]; | ||
export declare type hSegment = [hCommand, number]; | ||
export declare type HorLineSegment = HSegment | hSegment; | ||
export declare type ZSegment = [ZCommand]; | ||
export declare type zSegment = [zCommand]; | ||
export declare type CloseSegment = ZSegment | zSegment; | ||
export declare type CSegment = [CCommand, number, number, number, number, number, number]; | ||
export declare type cSegment = [cCommand, number, number, number, number, number, number]; | ||
export declare type CubicSegment = CSegment | cSegment; | ||
export declare type SSegment = [SCommand, number, number, number, number]; | ||
export declare type sSegment = [sCommand, number, number, number, number]; | ||
export declare type ShortCubicSegment = SSegment | sSegment; | ||
export declare type QSegment = [QCommand, number, number, number, number]; | ||
export declare type qSegment = [qCommand, number, number, number, number]; | ||
export declare type QuadSegment = QSegment | qSegment; | ||
export declare type TSegment = [TCommand, number, number]; | ||
export declare type tSegment = [tCommand, number, number]; | ||
export declare type ShortQuadSegment = TSegment | tSegment; | ||
export declare type ASegment = [ACommand, number, number, number, number, number, number, number]; | ||
export declare type aSegment = [aCommand, number, number, number, number, number, number, number]; | ||
export declare type ArcSegment = ASegment | aSegment; | ||
export declare type PathSegment = MoveSegment | LineSegment | VertLineSegment | HorLineSegment | CloseSegment | CubicSegment | ShortCubicSegment | QuadSegment | ShortQuadSegment | ArcSegment; | ||
export interface SegmentProperties { | ||
/** the segment */ | ||
segment: PathSegment; | ||
/** the segment index */ | ||
index: number; | ||
/** the segment length */ | ||
length: number; | ||
/** the length including the segment length */ | ||
lengthAtSegment: number; | ||
[key: string]: any; | ||
} | ||
export declare type ShortSegment = VertLineSegment | HorLineSegment | ShortCubicSegment | ShortQuadSegment | CloseSegment; | ||
export declare type AbsoluteSegment = MSegment | LSegment | VSegment | HSegment | CSegment | SSegment | QSegment | TSegment | ASegment | ZSegment; | ||
export declare type RelativeSegment = mSegment | lSegment | vSegment | hSegment | cSegment | sSegment | qSegment | tSegment | aSegment | zSegment; | ||
export declare type NormalSegment = MSegment | LSegment | CSegment | QSegment | ASegment | ZSegment; | ||
export declare type PathArray = [MSegment | mSegment, ...PathSegment[]]; | ||
export declare type AbsoluteArray = [MSegment, ...AbsoluteSegment[]]; | ||
export declare type RelativeArray = [MSegment, ...RelativeSegment[]]; | ||
export declare type NormalArray = [MSegment, ...NormalSegment[]]; | ||
export declare type CurveArray = [MSegment, ...CSegment[]]; | ||
export declare type PolygonArray = [MSegment, ...LSegment[], ZSegment]; | ||
export declare type PolylineArray = [MSegment, ...LSegment[]]; | ||
export interface ParserParams { | ||
x1: number; | ||
@@ -24,3 +90,34 @@ y1: number; | ||
qy: number | null; | ||
}; | ||
export {}; | ||
} | ||
export interface PathBBox { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
x2: number; | ||
y2: number; | ||
cx: number; | ||
cy: number; | ||
cz: number; | ||
} | ||
export interface PathBBoxTotalLength extends PathBBox { | ||
length: number; | ||
} | ||
export interface SegmentLimits { | ||
min: Point; | ||
max: Point; | ||
} | ||
export interface PointProperties { | ||
closest: { | ||
x: number; | ||
y: number; | ||
}; | ||
distance: number; | ||
segment?: SegmentProperties; | ||
} | ||
export interface LengthFactory { | ||
length: number; | ||
point: Point; | ||
min: Point; | ||
max: Point; | ||
} |
{ | ||
"name": "@antv/util", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
171
README.md
@@ -5,3 +5,2 @@ # util | ||
[![Build Status](https://github.com/antvis/util/workflows/build/badge.svg)](https://github.com/antvis/util/actions) | ||
@@ -12,3 +11,2 @@ [![npm Version](https://img.shields.io/npm/v/@antv/util.svg)](https://www.npmjs.com/package/@antv/util) | ||
## Usage | ||
@@ -18,6 +16,4 @@ | ||
import { gradient } from '@antv/util'; | ||
``` | ||
## 原则 | ||
@@ -30,6 +26,173 @@ | ||
## API | ||
提供以下 Path 工具方法,包含转换、几何计算等。 | ||
### path2String | ||
将 PathArray 转换成字符串形式,不会对原始定义中的命令进行修改: | ||
```js | ||
const str: PathArray = [ | ||
['M', 10, 10], | ||
['L', 100, 100], | ||
['l', 10, 10], | ||
['h', 20], | ||
['v', 20], | ||
]; | ||
expect(path2String(str)).toEqual('M10 10L100 100l10 10h20v20'); | ||
``` | ||
### path2Absolute | ||
将定义中的相对命令转换成绝对命令,例如: | ||
- l -> L | ||
- h -> H | ||
- v -> V | ||
完整方法签名如下: | ||
```js | ||
path2Absolute(pathInput: string | PathArray): AbsoluteArray; | ||
``` | ||
```js | ||
const str: PathArray = [ | ||
['M', 10, 10], | ||
['L', 100, 100], | ||
['l', 10, 10], | ||
['h', 20], | ||
['v', 20], | ||
]; | ||
const arr = path2Absolute(str); | ||
expect(arr).toEqual([ | ||
['M', 10, 10], | ||
['L', 100, 100], | ||
['L', 110, 110], | ||
['H', 130], | ||
['V', 130], | ||
]); | ||
``` | ||
### path2Curve | ||
将部分命令转曲,例如 L / A 转成 C 命令,借助 cubic bezier 易于分割的特性用于实现形变动画。 | ||
该方法内部会调用 [path2Absolute](#path2Absolute),因此最终返回的 PathArray 中仅包含 M 和 C 命令。 | ||
完整方法签名如下: | ||
```js | ||
path2Curve(pathInput: string | PathArray): CurveArray; | ||
``` | ||
```js | ||
expect( | ||
path2Curve([ | ||
['M', 0, 0], | ||
['L', 100, 100], | ||
]), | ||
).toEqual([ | ||
['M', 0, 0], | ||
['C', 44.194173824159215, 44.194173824159215, 68.75, 68.75, 100, 100], | ||
]); | ||
``` | ||
### clonePath | ||
复制路径: | ||
```js | ||
const cloned = clonePath(pathInput); | ||
``` | ||
### reverseCurve | ||
```js | ||
const pathArray: CurveArray = [ | ||
['M', 170, 90], | ||
['C', 150, 90, 155, 10, 130, 10], | ||
['C', 105, 10, 110, 90, 90, 90], | ||
['C', 70, 90, 75, 10, 50, 10], | ||
['C', 25, 10, 30, 90, 10, 90], | ||
]; | ||
const reversed = reverseCurve(pathArray); | ||
``` | ||
### getPathBBox | ||
获取几何定义下的包围盒,形如: | ||
```js | ||
export interface PathBBox { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
x2: number; | ||
y2: number; | ||
cx: number; | ||
cy: number; | ||
cz: number; | ||
} | ||
``` | ||
```js | ||
const bbox = getPathBBox([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']]); | ||
expect(bbox).toEqual({ cx: 50, cy: 50, cz: 150, height: 100, width: 100, x: 0, x2: 100, y: 0, y2: 100 }); | ||
``` | ||
### getTotalLength | ||
获取路径总长度。 | ||
```js | ||
const length = getTotalLength([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']]); | ||
expect(length).toEqual(400); | ||
``` | ||
### getPointAtLength | ||
获取路径上从起点出发,到指定距离的点。 | ||
```js | ||
const point = getPointAtLength([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']], 0); | ||
expect(point).toEqual({ x: 0, y: 0 }); | ||
``` | ||
### getPathArea | ||
计算路径包围的面积。内部实现中首先通过 [path2Curve](#path2Curve) 转曲,再计算 cubic curve 面积,[详见](https://stackoverflow.com/a/15845996)。 | ||
### isPointInStroke | ||
判断一个点是否在路径上,仅通过几何定义,不考虑其他样式属性例如线宽、lineJoin、miter 等。 | ||
```js | ||
const result = isPointInStroke(segments, { x: 10, y: 10 }); | ||
``` | ||
### distanceSquareRoot | ||
计算两点之间的距离。 | ||
方法签名如下: | ||
```js | ||
distanceSquareRoot(a: [number, number], b: [number, number]): number; | ||
``` | ||
### equalizeSegments | ||
将两条路径处理成段数相同,用于形变动画前的分割操作。 | ||
```js | ||
const [formattedPath1, formattedPath2] = equalizeSegments(path1, path2); | ||
``` | ||
## License | ||
MIT@[AntV](https://github.com/antvis). |
@@ -1,16 +0,18 @@ | ||
export { parsePath } from './parse-path'; | ||
export { catmullRom2Bezier } from './catmull-rom-2-bezier'; | ||
export { fillPath } from './fill-path'; | ||
export { fillPathByDiff } from './fill-path-by-diff'; | ||
export { formatPath } from './format-path'; | ||
export { pathIntersection } from './path-intersection'; | ||
export { parsePathArray } from './parse-path-array'; | ||
export { parsePathString } from './parse-path-string'; | ||
export { path2Curve } from './path-2-curve'; | ||
export { path2Absolute } from './path-2-absolute'; | ||
export { rectPath } from './rect-path'; | ||
export { getArcParams } from './get-arc-params'; | ||
export { path2Segments } from './path-2-segments'; | ||
export { getLineIntersect } from './get-line-intersect'; | ||
export { isPolygonsIntersect } from './is-polygons-intersect'; | ||
export { isPointInPolygon } from './point-in-polygon'; | ||
export { path2String } from './convert/path-2-string'; | ||
export { path2Curve } from './convert/path-2-curve'; | ||
export { path2Absolute } from './convert/path-2-absolute'; | ||
export { clonePath } from './process/clone-path'; | ||
export { normalizePath } from './process/normalize-path'; | ||
export { reverseCurve } from './process/reverse-curve'; | ||
export { getPathBBox } from './util/get-path-bbox'; | ||
export { getTotalLength } from './util/get-total-length'; | ||
export { getPathBBoxTotalLength } from './util/get-path-bbox-total-length'; | ||
export { getRotatedCurve } from './util/get-rotated-curve'; | ||
export { getPathArea } from './util/get-path-area'; | ||
export { getDrawDirection } from './util/get-draw-direction'; | ||
export { getPointAtLength } from './util/get-point-at-length'; | ||
export { isPointInStroke } from './util/is-point-in-stroke'; | ||
export { distanceSquareRoot } from './util/distance-square-root'; | ||
export { equalizeSegments } from './util/equalize-segments'; | ||
export * from './types'; |
@@ -1,221 +0,338 @@ | ||
const TAU = Math.PI * 2; | ||
import { rotateVector } from '../util/rotate-vector'; | ||
const mapToEllipse = ( | ||
{ x, y }: { x: number; y: number }, | ||
rx: number, | ||
ry: number, | ||
cosphi: number, | ||
sinphi: number, | ||
centerx: number, | ||
centery: number, | ||
) => { | ||
x *= rx; | ||
y *= ry; | ||
/** | ||
* Converts A (arc-to) segments to C (cubic-bezier-to). | ||
* | ||
* For more information of where this math came from visit: | ||
* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
*/ | ||
export function arcToCubic( | ||
X1: number, | ||
Y1: number, | ||
RX: number, | ||
RY: number, | ||
angle: number, | ||
LAF: number, | ||
SF: number, | ||
X2: number, | ||
Y2: number, | ||
recursive: number[], | ||
) { | ||
let x1 = X1; | ||
let y1 = Y1; | ||
let rx = RX; | ||
let ry = RY; | ||
let x2 = X2; | ||
let y2 = Y2; | ||
// for more information of where this Math came from visit: | ||
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes | ||
const d120 = (Math.PI * 120) / 180; | ||
const xp = cosphi * x - sinphi * y; | ||
const yp = sinphi * x + cosphi * y; | ||
const rad = (Math.PI / 180) * (+angle || 0); | ||
/** @type {number[]} */ | ||
let res = []; | ||
let xy; | ||
let f1; | ||
let f2; | ||
let cx; | ||
let cy; | ||
return { | ||
x: xp + centerx, | ||
y: yp + centery, | ||
}; | ||
}; | ||
if (!recursive) { | ||
xy = rotateVector(x1, y1, -rad); | ||
x1 = xy.x; | ||
y1 = xy.y; | ||
xy = rotateVector(x2, y2, -rad); | ||
x2 = xy.x; | ||
y2 = xy.y; | ||
const approxUnitArc = (ang1: number, ang2: number) => { | ||
// If 90 degree circular arc, use a constant | ||
// as derived from http://spencermortensen.com/articles/bezier-circle | ||
const a = | ||
ang2 === 1.5707963267948966 | ||
? 0.551915024494 | ||
: ang2 === -1.5707963267948966 | ||
? -0.551915024494 | ||
: (4 / 3) * Math.tan(ang2 / 4); | ||
const x = (x1 - x2) / 2; | ||
const y = (y1 - y2) / 2; | ||
let h = (x * x) / (rx * rx) + (y * y) / (ry * ry); | ||
if (h > 1) { | ||
h = Math.sqrt(h); | ||
rx *= h; | ||
ry *= h; | ||
} | ||
const rx2 = rx * rx; | ||
const ry2 = ry * ry; | ||
const x1 = Math.cos(ang1); | ||
const y1 = Math.sin(ang1); | ||
const x2 = Math.cos(ang1 + ang2); | ||
const y2 = Math.sin(ang1 + ang2); | ||
const k = | ||
(LAF === SF ? -1 : 1) * | ||
Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))); | ||
return [ | ||
{ | ||
x: x1 - y1 * a, | ||
y: y1 + x1 * a, | ||
}, | ||
{ | ||
x: x2 + y2 * a, | ||
y: y2 - x2 * a, | ||
}, | ||
{ | ||
x: x2, | ||
y: y2, | ||
}, | ||
]; | ||
}; | ||
cx = (k * rx * y) / ry + (x1 + x2) / 2; | ||
cy = (k * -ry * x) / rx + (y1 + y2) / 2; | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f1 = ((Math.asin((y1 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9; | ||
// eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise | ||
f2 = ((Math.asin((y2 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9; | ||
const vectorAngle = (ux: number, uy: number, vx: number, vy: number) => { | ||
const sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
f1 = x1 < cx ? Math.PI - f1 : f1; | ||
f2 = x2 < cx ? Math.PI - f2 : f2; | ||
if (f1 < 0) f1 = Math.PI * 2 + f1; | ||
if (f2 < 0) f2 = Math.PI * 2 + f2; | ||
if (SF && f1 > f2) { | ||
f1 -= Math.PI * 2; | ||
} | ||
if (!SF && f2 > f1) { | ||
f2 -= Math.PI * 2; | ||
} | ||
} else { | ||
[f1, f2, cx, cy] = recursive; | ||
} | ||
let df = f2 - f1; | ||
if (Math.abs(df) > d120) { | ||
const f2old = f2; | ||
const x2old = x2; | ||
const y2old = y2; | ||
f2 = f1 + d120 * (SF && f2 > f1 ? 1 : -1); | ||
x2 = cx + rx * Math.cos(f2); | ||
y2 = cy + ry * Math.sin(f2); | ||
res = arcToCubic(x2, y2, rx, ry, angle, 0, SF, x2old, y2old, [f2, f2old, cx, cy]); | ||
} | ||
df = f2 - f1; | ||
const c1 = Math.cos(f1); | ||
const s1 = Math.sin(f1); | ||
const c2 = Math.cos(f2); | ||
const s2 = Math.sin(f2); | ||
const t = Math.tan(df / 4); | ||
const hx = (4 / 3) * rx * t; | ||
const hy = (4 / 3) * ry * t; | ||
const m1 = [x1, y1]; | ||
const m2 = [x1 + hx * s1, y1 - hy * c1]; | ||
const m3 = [x2 + hx * s2, y2 - hy * c2]; | ||
const m4 = [x2, y2]; | ||
m2[0] = 2 * m1[0] - m2[0]; | ||
m2[1] = 2 * m1[1] - m2[1]; | ||
if (recursive) { | ||
return [...m2, ...m3, ...m4, ...res]; | ||
} | ||
res = [...m2, ...m3, ...m4, ...res]; | ||
const newres = []; | ||
for (let i = 0, ii = res.length; i < ii; i += 1) { | ||
newres[i] = i % 2 ? rotateVector(res[i - 1], res[i], rad).y : rotateVector(res[i], res[i + 1], rad).x; | ||
} | ||
return newres; | ||
} | ||
let dot = ux * vx + uy * vy; | ||
// const TAU = Math.PI * 2; | ||
if (dot > 1) { | ||
dot = 1; | ||
} | ||
// const mapToEllipse = ( | ||
// { x, y }: { x: number; y: number }, | ||
// rx: number, | ||
// ry: number, | ||
// cosphi: number, | ||
// sinphi: number, | ||
// centerx: number, | ||
// centery: number, | ||
// ) => { | ||
// x *= rx; | ||
// y *= ry; | ||
if (dot < -1) { | ||
dot = -1; | ||
} | ||
// const xp = cosphi * x - sinphi * y; | ||
// const yp = sinphi * x + cosphi * y; | ||
return sign * Math.acos(dot); | ||
}; | ||
// return { | ||
// x: xp + centerx, | ||
// y: yp + centery, | ||
// }; | ||
// }; | ||
const getArcCenter = ( | ||
px: any, | ||
py: any, | ||
cx: any, | ||
cy: any, | ||
rx: number, | ||
ry: number, | ||
largeArcFlag: number, | ||
sweepFlag: number, | ||
sinphi: number, | ||
cosphi: number, | ||
pxp: number, | ||
pyp: number, | ||
) => { | ||
const rxsq = Math.pow(rx, 2); | ||
const rysq = Math.pow(ry, 2); | ||
const pxpsq = Math.pow(pxp, 2); | ||
const pypsq = Math.pow(pyp, 2); | ||
// const approxUnitArc = (ang1: number, ang2: number) => { | ||
// // If 90 degree circular arc, use a constant | ||
// // as derived from http://spencermortensen.com/articles/bezier-circle | ||
// const a = | ||
// ang2 === 1.5707963267948966 | ||
// ? 0.551915024494 | ||
// : ang2 === -1.5707963267948966 | ||
// ? -0.551915024494 | ||
// : (4 / 3) * Math.tan(ang2 / 4); | ||
let radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
// const x1 = Math.cos(ang1); | ||
// const y1 = Math.sin(ang1); | ||
// const x2 = Math.cos(ang1 + ang2); | ||
// const y2 = Math.sin(ang1 + ang2); | ||
if (radicant < 0) { | ||
radicant = 0; | ||
} | ||
// return [ | ||
// { | ||
// x: x1 - y1 * a, | ||
// y: y1 + x1 * a, | ||
// }, | ||
// { | ||
// x: x2 + y2 * a, | ||
// y: y2 - x2 * a, | ||
// }, | ||
// { | ||
// x: x2, | ||
// y: y2, | ||
// }, | ||
// ]; | ||
// }; | ||
radicant /= rxsq * pypsq + rysq * pxpsq; | ||
radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
// const vectorAngle = (ux: number, uy: number, vx: number, vy: number) => { | ||
// const sign = ux * vy - uy * vx < 0 ? -1 : 1; | ||
const centerxp = ((radicant * rx) / ry) * pyp; | ||
const centeryp = ((radicant * -ry) / rx) * pxp; | ||
// let dot = ux * vx + uy * vy; | ||
const centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
const centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
// if (dot > 1) { | ||
// dot = 1; | ||
// } | ||
const vx1 = (pxp - centerxp) / rx; | ||
const vy1 = (pyp - centeryp) / ry; | ||
const vx2 = (-pxp - centerxp) / rx; | ||
const vy2 = (-pyp - centeryp) / ry; | ||
// if (dot < -1) { | ||
// dot = -1; | ||
// } | ||
const ang1 = vectorAngle(1, 0, vx1, vy1); | ||
let ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
// return sign * Math.acos(dot); | ||
// }; | ||
if (sweepFlag === 0 && ang2 > 0) { | ||
ang2 -= TAU; | ||
} | ||
// const getArcCenter = ( | ||
// px: any, | ||
// py: any, | ||
// cx: any, | ||
// cy: any, | ||
// rx: number, | ||
// ry: number, | ||
// largeArcFlag: number, | ||
// sweepFlag: number, | ||
// sinphi: number, | ||
// cosphi: number, | ||
// pxp: number, | ||
// pyp: number, | ||
// ) => { | ||
// const rxsq = Math.pow(rx, 2); | ||
// const rysq = Math.pow(ry, 2); | ||
// const pxpsq = Math.pow(pxp, 2); | ||
// const pypsq = Math.pow(pyp, 2); | ||
if (sweepFlag === 1 && ang2 < 0) { | ||
ang2 += TAU; | ||
} | ||
// let radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq; | ||
return [centerx, centery, ang1, ang2]; | ||
}; | ||
// if (radicant < 0) { | ||
// radicant = 0; | ||
// } | ||
const arcToBezier = ({ px, py, cx, cy, rx, ry, xAxisRotation = 0, largeArcFlag = 0, sweepFlag = 0 }) => { | ||
const curves = []; | ||
// radicant /= rxsq * pypsq + rysq * pxpsq; | ||
// radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1); | ||
if (rx === 0 || ry === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
// const centerxp = ((radicant * rx) / ry) * pyp; | ||
// const centeryp = ((radicant * -ry) / rx) * pxp; | ||
const sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
const cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
// const centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2; | ||
// const centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2; | ||
const pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
const pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
// const vx1 = (pxp - centerxp) / rx; | ||
// const vy1 = (pyp - centeryp) / ry; | ||
// const vx2 = (-pxp - centerxp) / rx; | ||
// const vy2 = (-pyp - centeryp) / ry; | ||
if (pxp === 0 && pyp === 0) { | ||
return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
} | ||
// const ang1 = vectorAngle(1, 0, vx1, vy1); | ||
// let ang2 = vectorAngle(vx1, vy1, vx2, vy2); | ||
rx = Math.abs(rx); | ||
ry = Math.abs(ry); | ||
// if (sweepFlag === 0 && ang2 > 0) { | ||
// ang2 -= TAU; | ||
// } | ||
const lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
// if (sweepFlag === 1 && ang2 < 0) { | ||
// ang2 += TAU; | ||
// } | ||
if (lambda > 1) { | ||
rx *= Math.sqrt(lambda); | ||
ry *= Math.sqrt(lambda); | ||
} | ||
// return [centerx, centery, ang1, ang2]; | ||
// }; | ||
let [centerx, centery, ang1, ang2] = getArcCenter( | ||
px, | ||
py, | ||
cx, | ||
cy, | ||
rx, | ||
ry, | ||
largeArcFlag, | ||
sweepFlag, | ||
sinphi, | ||
cosphi, | ||
pxp, | ||
pyp, | ||
); | ||
// const arcToBezier = ({ px, py, cx, cy, rx, ry, xAxisRotation = 0, largeArcFlag = 0, sweepFlag = 0 }) => { | ||
// const curves = []; | ||
// If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// this issue, we round to 1.0 when the ratio is close to 1.0. | ||
let ratio = Math.abs(ang2) / (TAU / 4); | ||
if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
ratio = 1.0; | ||
} | ||
// if (rx === 0 || ry === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
const segments = Math.max(Math.ceil(ratio), 1); | ||
// const sinphi = Math.sin((xAxisRotation * TAU) / 360); | ||
// const cosphi = Math.cos((xAxisRotation * TAU) / 360); | ||
ang2 /= segments; | ||
// const pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2; | ||
// const pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2; | ||
for (let i = 0; i < segments; i++) { | ||
curves.push(approxUnitArc(ang1, ang2)); | ||
ang1 += ang2; | ||
} | ||
// if (pxp === 0 && pyp === 0) { | ||
// return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }]; | ||
// } | ||
return curves.map((curve) => { | ||
const { x: x1, y: y1 } = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery); | ||
const { x: x2, y: y2 } = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery); | ||
const { x, y } = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery); | ||
// rx = Math.abs(rx); | ||
// ry = Math.abs(ry); | ||
return { x1, y1, x2, y2, x, y }; | ||
}); | ||
}; | ||
// const lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2); | ||
export function arcToCubic( | ||
x1: number, | ||
y1: number, | ||
rx: number, | ||
ry: number, | ||
angle: number, | ||
LAF: number, | ||
SF: number, | ||
x2: number, | ||
y2: number, | ||
) { | ||
const curves = arcToBezier({ | ||
px: x1, | ||
py: y1, | ||
cx: x2, | ||
cy: y2, | ||
rx, | ||
ry, | ||
xAxisRotation: angle, | ||
largeArcFlag: LAF, | ||
sweepFlag: SF, | ||
}); | ||
// if (lambda > 1) { | ||
// rx *= Math.sqrt(lambda); | ||
// ry *= Math.sqrt(lambda); | ||
// } | ||
return curves.reduce((prev, cur) => { | ||
const { x1, y1, x2, y2, x, y } = cur; | ||
prev.push(x1, y1, x2, y2, x, y); | ||
return prev; | ||
}, [] as number[]); | ||
} | ||
// let [centerx, centery, ang1, ang2] = getArcCenter( | ||
// px, | ||
// py, | ||
// cx, | ||
// cy, | ||
// rx, | ||
// ry, | ||
// largeArcFlag, | ||
// sweepFlag, | ||
// sinphi, | ||
// cosphi, | ||
// pxp, | ||
// pyp, | ||
// ); | ||
// // If 'ang2' == 90.0000000001, then `ratio` will evaluate to | ||
// // 1.0000000001. This causes `segments` to be greater than one, which is an | ||
// // unecessary split, and adds extra points to the bezier curve. To alleviate | ||
// // this issue, we round to 1.0 when the ratio is close to 1.0. | ||
// let ratio = Math.abs(ang2) / (TAU / 4); | ||
// if (Math.abs(1.0 - ratio) < 0.0000001) { | ||
// ratio = 1.0; | ||
// } | ||
// const segments = Math.max(Math.ceil(ratio), 1); | ||
// ang2 /= segments; | ||
// for (let i = 0; i < segments; i++) { | ||
// curves.push(approxUnitArc(ang1, ang2)); | ||
// ang1 += ang2; | ||
// } | ||
// return curves.map((curve) => { | ||
// const { x: x1, y: y1 } = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x: x2, y: y2 } = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery); | ||
// const { x, y } = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery); | ||
// return { x1, y1, x2, y2, x, y }; | ||
// }); | ||
// }; | ||
// export function arcToCubic( | ||
// x1: number, | ||
// y1: number, | ||
// rx: number, | ||
// ry: number, | ||
// angle: number, | ||
// LAF: number, | ||
// SF: number, | ||
// x2: number, | ||
// y2: number, | ||
// ) { | ||
// const curves = arcToBezier({ | ||
// px: x1, | ||
// py: y1, | ||
// cx: x2, | ||
// cy: y2, | ||
// rx, | ||
// ry, | ||
// xAxisRotation: angle, | ||
// largeArcFlag: LAF, | ||
// sweepFlag: SF, | ||
// }); | ||
// return curves.reduce((prev, cur) => { | ||
// const { x1, y1, x2, y2, x, y } = cur; | ||
// prev.push(x1, y1, x2, y2, x, y); | ||
// return prev; | ||
// }, [] as number[]); | ||
// } |
@@ -1,37 +0,21 @@ | ||
// export function getPointAtSegLength(p1x: number, p1y: number, c1x: number, c1y: number, c2x: number, c2y: number, p2x: number, p2y: number, t: number) { | ||
// const t1 = 1 - t; | ||
// return { | ||
// x: (t1 ** 3) * p1x | ||
// + t1 * t1 * 3 * t * c1x | ||
// + t1 * 3 * t * t * c2x | ||
// + (t ** 3) * p2x, | ||
// y: (t1 ** 3) * p1y | ||
// + t1 * t1 * 3 * t * c1y | ||
// + t1 * 3 * t * t * c2y | ||
// + (t ** 3) * p2y, | ||
// }; | ||
// } | ||
import { segmentLineFactory } from '../util/segment-line-factory'; | ||
import { midPoint } from '../util/mid-point'; | ||
// export function midPoint(a: number[], b: number[], t: number) { | ||
// const ax = a[0]; | ||
// const ay = a[1]; | ||
// const bx = b[0]; | ||
// const by = b[1]; | ||
// return [ax + (bx - ax) * t, ay + (by - ay) * t]; | ||
// } | ||
export function lineToCubic(x1: number, y1: number, x2: number, y2: number) { | ||
return [x1, y1, x2, y2, x2, y2]; | ||
// const t = 0.5; | ||
// const p0 = [x1, y1]; | ||
// const p1 = [x2, y2]; | ||
// const p2 = midPoint(p0, p1, t); | ||
// const p3 = midPoint(p1, p2, t); | ||
// const p4 = midPoint(p2, p3, t); | ||
// const p5 = midPoint(p3, p4, t); | ||
// const p6 = midPoint(p4, p5, t); | ||
// const cp1 = getPointAtSegLength.apply(0, p0.concat(p2, p4, p6, t)); | ||
// const cp2 = getPointAtSegLength.apply(0, p6.concat(p5, p3, p1, 0)); | ||
const t = 0.5; | ||
const p0 = [x1, y1]; | ||
const p1 = [x2, y2]; | ||
const p2 = midPoint(p0, p1, t); | ||
const p3 = midPoint(p1, p2, t); | ||
const p4 = midPoint(p2, p3, t); | ||
const p5 = midPoint(p3, p4, t); | ||
const p6 = midPoint(p4, p5, t); | ||
const seg1 = [...p0, ...p2, ...p4, ...p6, t]; | ||
// @ts-ignore | ||
const cp1 = segmentLineFactory(...seg1).point; | ||
const seg2 = [...p6, ...p5, ...p3, ...p1, 0]; | ||
// @ts-ignore | ||
const cp2 = segmentLineFactory(...seg2).point; | ||
// return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { PathCommand, ProcessParams } from '../types'; | ||
import type { PathSegment, ParserParams, CSegment, MSegment } from '../types'; | ||
import { arcToCubic } from './arc-2-cubic'; | ||
@@ -6,4 +6,10 @@ import { quadToCubic } from './quad-2-cubic'; | ||
export function segmentToCubic(segment: PathCommand, params: ProcessParams): PathCommand { | ||
if ('TQ'.indexOf(segment[0]) < 0) { | ||
export function segmentToCubic(segment: PathSegment, params: ParserParams): CSegment | MSegment { | ||
const [pathCommand] = segment; | ||
const values = segment.slice(1).map(Number); | ||
const [x, y] = values; | ||
let args: any[]; | ||
const { x1: px1, y1: py1, x: px, y: py } = params; | ||
if (!'TQ'.includes(pathCommand)) { | ||
params.qx = null; | ||
@@ -13,34 +19,29 @@ params.qy = null; | ||
const [s1, s2] = segment.slice(1); | ||
switch (segment[0]) { | ||
switch (pathCommand) { | ||
case 'M': | ||
params.x = s1 as number; | ||
params.y = s2 as number; | ||
params.x = x; | ||
params.y = y; | ||
return segment; | ||
case 'A': | ||
return ['C'].concat( | ||
arcToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1) as number[])), | ||
) as PathCommand; | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...arcToCubic(...args)] as CubicSegment; | ||
case 'Q': | ||
params.qx = s1 as number; | ||
params.qy = s2 as number; | ||
return ['C'].concat( | ||
quadToCubic.apply(0, [params.x1, params.y1].concat(segment.slice(1) as number[])), | ||
) as PathCommand; | ||
params.qx = x; | ||
params.qy = y; | ||
args = [px1, py1, ...values]; | ||
// @ts-ignore | ||
return ['C', ...quadToCubic(...args)] as CubicSegment; | ||
case 'L': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, segment[1], segment[2])) as PathCommand; | ||
case 'H': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, segment[1], params.y1)) as PathCommand; | ||
case 'V': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, params.x1, segment[1])) as PathCommand; | ||
return ['C', ...lineToCubic(px1, py1, x, y)] as CSegment; | ||
case 'Z': | ||
// @ts-ignore | ||
return ['C'].concat(lineToCubic(params.x1, params.y1, params.x, params.y)) as PathCommand; | ||
// prevent NaN from divide 0 | ||
if (px1 === px && py1 === py) { | ||
return ['C', px1, py1, px, py, px, py]; | ||
} | ||
return ['C', ...lineToCubic(px1, py1, px, py)] as CSegment; | ||
default: | ||
} | ||
return segment; | ||
return segment as CSegment; | ||
} |
@@ -1,17 +0,155 @@ | ||
type A = ['a' | 'A', number, number, number, number, number, number, number]; | ||
type C = ['c' | 'C', number, number, number, number, number, number]; | ||
type O = ['o' | 'O', number, number]; | ||
type H = ['h' | 'H', number]; | ||
type L = ['l' | 'L', number, number]; | ||
type M = ['m' | 'M', number, number]; | ||
type R = ['r' | 'R', number, number, number, number]; | ||
type Q = ['q' | 'Q', number, number, number, number]; | ||
type S = ['s' | 'S', number, number, number, number, number, number, number]; | ||
type T = ['t' | 'T', number, number]; | ||
type V = ['v' | 'V', number]; | ||
type U = ['u' | 'U', number, number, number]; | ||
type Z = ['z' | 'Z']; | ||
export type PathCommand = A | C | O | H | L | M | R | Q | S | T | V | U | Z; | ||
export type Point = { x: number; y: number }; | ||
export type MCommand = 'M'; | ||
export type mCommand = 'm'; | ||
export type ProcessParams = { | ||
export type LCommand = 'L'; | ||
export type lCommand = 'l'; | ||
export type VCommand = 'V'; | ||
export type vCommand = 'v'; | ||
export type HCommand = 'H'; | ||
export type hCommand = 'h'; | ||
export type ZCommand = 'Z'; | ||
export type zCommand = 'z'; | ||
export type CCommand = 'C'; | ||
export type cCommand = 'c'; | ||
export type SCommand = 'S'; | ||
export type sCommand = 's'; | ||
export type QCommand = 'Q'; | ||
export type qCommand = 'q'; | ||
export type TCommand = 'T'; | ||
export type tCommand = 't'; | ||
export type ACommand = 'A'; | ||
export type aCommand = 'a'; | ||
export type AbsoluteCommand = | ||
| MCommand | ||
| LCommand | ||
| VCommand | ||
| HCommand | ||
| ZCommand | ||
| CCommand | ||
| SCommand | ||
| QCommand | ||
| TCommand | ||
| ACommand; | ||
export type RelativeCommand = | ||
| mCommand | ||
| lCommand | ||
| vCommand | ||
| hCommand | ||
| zCommand | ||
| cCommand | ||
| sCommand | ||
| qCommand | ||
| tCommand | ||
| aCommand; | ||
export type PathCommand = AbsoluteCommand | RelativeCommand; | ||
export type MSegment = [MCommand, number, number]; | ||
export type mSegment = [mCommand, number, number]; | ||
export type MoveSegment = MSegment | mSegment; | ||
export type LSegment = [LCommand, number, number]; | ||
export type lSegment = [lCommand, number, number]; | ||
export type LineSegment = LSegment | lSegment; | ||
export type VSegment = [VCommand, number]; | ||
export type vSegment = [vCommand, number]; | ||
export type VertLineSegment = vSegment | VSegment; | ||
export type HSegment = [HCommand, number]; | ||
export type hSegment = [hCommand, number]; | ||
export type HorLineSegment = HSegment | hSegment; | ||
export type ZSegment = [ZCommand]; | ||
export type zSegment = [zCommand]; | ||
export type CloseSegment = ZSegment | zSegment; | ||
export type CSegment = [CCommand, number, number, number, number, number, number]; | ||
export type cSegment = [cCommand, number, number, number, number, number, number]; | ||
export type CubicSegment = CSegment | cSegment; | ||
export type SSegment = [SCommand, number, number, number, number]; | ||
export type sSegment = [sCommand, number, number, number, number]; | ||
export type ShortCubicSegment = SSegment | sSegment; | ||
export type QSegment = [QCommand, number, number, number, number]; | ||
export type qSegment = [qCommand, number, number, number, number]; | ||
export type QuadSegment = QSegment | qSegment; | ||
export type TSegment = [TCommand, number, number]; | ||
export type tSegment = [tCommand, number, number]; | ||
export type ShortQuadSegment = TSegment | tSegment; | ||
export type ASegment = [ACommand, number, number, number, number, number, number, number]; | ||
export type aSegment = [aCommand, number, number, number, number, number, number, number]; | ||
export type ArcSegment = ASegment | aSegment; | ||
export type PathSegment = | ||
| MoveSegment | ||
| LineSegment | ||
| VertLineSegment | ||
| HorLineSegment | ||
| CloseSegment | ||
| CubicSegment | ||
| ShortCubicSegment | ||
| QuadSegment | ||
| ShortQuadSegment | ||
| ArcSegment; | ||
export interface SegmentProperties { | ||
/** the segment */ | ||
segment: PathSegment; | ||
/** the segment index */ | ||
index: number; | ||
/** the segment length */ | ||
length: number; | ||
/** the length including the segment length */ | ||
lengthAtSegment: number; | ||
[key: string]: any; | ||
} | ||
export type ShortSegment = VertLineSegment | HorLineSegment | ShortCubicSegment | ShortQuadSegment | CloseSegment; | ||
export type AbsoluteSegment = | ||
| MSegment | ||
| LSegment | ||
| VSegment | ||
| HSegment | ||
| CSegment | ||
| SSegment | ||
| QSegment | ||
| TSegment | ||
| ASegment | ||
| ZSegment; | ||
export type RelativeSegment = | ||
| mSegment | ||
| lSegment | ||
| vSegment | ||
| hSegment | ||
| cSegment | ||
| sSegment | ||
| qSegment | ||
| tSegment | ||
| aSegment | ||
| zSegment; | ||
export type NormalSegment = MSegment | LSegment | CSegment | QSegment | ASegment | ZSegment; | ||
export type PathArray = [MSegment | mSegment, ...PathSegment[]]; | ||
export type AbsoluteArray = [MSegment, ...AbsoluteSegment[]]; | ||
export type RelativeArray = [MSegment, ...RelativeSegment[]]; | ||
export type NormalArray = [MSegment, ...NormalSegment[]]; | ||
export type CurveArray = [MSegment, ...CSegment[]]; | ||
export type PolygonArray = [MSegment, ...LSegment[], ZSegment]; | ||
export type PolylineArray = [MSegment, ...LSegment[]]; | ||
export interface ParserParams { | ||
x1: number; | ||
@@ -25,2 +163,37 @@ y1: number; | ||
qy: number | null; | ||
}; | ||
} | ||
export interface PathBBox { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
x2: number; | ||
y2: number; | ||
cx: number; | ||
cy: number; | ||
cz: number; | ||
} | ||
export interface PathBBoxTotalLength extends PathBBox { | ||
length: number; | ||
} | ||
export interface SegmentLimits { | ||
min: Point; | ||
max: Point; | ||
} | ||
export interface PointProperties { | ||
closest: { | ||
x: number; | ||
y: number; | ||
}; | ||
distance: number; | ||
segment?: SegmentProperties; | ||
} | ||
export interface LengthFactory { | ||
length: number; | ||
point: Point; | ||
min: Point; | ||
max: Point; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
451038
440
7714
195
3