var_gsScope=(typeof(module)!=="undefined"&&module.exports&&typeof(global)!=="undefined")?global:this||window;//helps ensure compatibility with AMD/RequireJS and CommonJS/Node
//now transform according to the actual size of the ellipse/arc (the beziers were noramlized, between 0 and 1 on a circle).
for(i=0;i<rawPath.length;i+=2){
x1=rawPath[i];
y1=rawPath[i+1];
rawPath[i]=x1*ma+y1*mc+cx;
rawPath[i+1]=x1*mb+y1*md+cy;
}
rawPath[i-2]=x;//always set the end to exactly where it's supposed to be
rawPath[i-1]=y;
returnrawPath;
},
//Spits back an array of cubic Bezier segments that use absolute coordinates. Each segment starts with a "moveTo" command (x coordinate, then y) and then 2 control points (x, y, x, y), then anchor. The goal is to minimize memory and maximize speed.
_stringToRawPath=function(d){
vara=(d+"").replace(_scientific,function(m){varn=+m;return(n<0.0001&&n>-0.0001)?0:n;}).match(_svgPathExp)||[],//some authoring programs spit out very small numbers in scientific notation like "1e-5", so make sure we round that down to 0 first.
//adds a certain number of Beziers while maintaining the path shape (so that the start/end values can have a matching quantity of points to animate). Only pass in ONE segment of the Bezier at a time. Format: [xAnchor, yAnchor, xControlPoint1, yControlPoint1, xControlPoint2, yControlPoint2, xAnchor, yAnchor, xControlPoint1, etc...]
_subdivideSegment=function(segment,quantity){
vartally=0,
max=0.999999,
l=segment.length,
newPointsPerSegment=quantity/((l-2)/6),
ax,ay,cp1x,cp1y,cp2x,cp2y,bx,by,
x1,y1,x2,y2,i,t;
for(i=2;i<l;i+=6){
tally+=newPointsPerSegment;
while(tally>max){//compare with 0.99999 instead of 1 in order to prevent rounding errors
ax=segment[i-2];
ay=segment[i-1];
cp1x=segment[i];
cp1y=segment[i+1];
cp2x=segment[i+2];
cp2y=segment[i+3];
bx=segment[i+4];
by=segment[i+5];
t=1/((Math.floor(tally)||1)+1);//progress along the bezier (value between 0 and 1)
for(i=2;i<l;i++){//this is actually faster than just doing a join() on the array, possibly because the numbers have so many decimal places
s+=(((segment[i]*rnd)|0)/rnd)+space;
}
if(segment.closed){
s+="z";
}
}
returns;
},
_reverseBezier=function(segment){
vara=[],
i=segment.length-1,
l=0;
while(--i>-1){
a[l++]=segment[i];
a[l++]=segment[i+1];
i--;
}
for(i=0;i<l;i++){
segment[i]=a[i];
}
segment.reversed=!segment.reversed;
},
_getAverageXY=function(segment){
varl=segment.length,
x=0,
y=0,
i;
for(i=0;i<l;i++){
x+=segment[i++];
y+=segment[i];
}
return[x/(l/2),y/(l/2)];
},
_getSize=function(segment){//rough estimate of the bounding box (based solely on the anchors) of a single segment. sets "size", "centerX", and "centerY" properties on the bezier array itself, and returns the size (width * height)
varl=segment.length,
xMax=segment[0],
xMin=xMax,
yMax=segment[1],
yMin=yMax,
x,y,i;
for(i=6;i<l;i+=6){
x=segment[i];
y=segment[i+1];
if(x>xMax){
xMax=x;
}elseif(x<xMin){
xMin=x;
}
if(y>yMax){
yMax=y;
}elseif(y<yMin){
yMin=y;
}
}
segment.centerX=(xMax+xMin)/2;
segment.centerY=(yMax+yMin)/2;
return(segment.size=(xMax-xMin)*(yMax-yMin));
},
_getTotalSize=function(rawPath,samplesPerBezier){//rough estimate of the bounding box of the entire list of Bezier segments (based solely on the anchors). sets "size", "centerX", and "centerY" properties on the bezier array itself, and returns the size (width * height)
samplesPerBezier=samplesPerBezier||3;
varj=rawPath.length,
xMax=rawPath[0][0],
xMin=xMax,
yMax=rawPath[0][1],
yMin=yMax,
inc=1/samplesPerBezier,
l,x,y,i,segment,k,t,inv,x1,y1,x2,x3,x4,y2,y3,y4;
while(--j>-1){
segment=rawPath[j];
l=segment.length;
for(i=6;i<l;i+=6){
x1=segment[i];
y1=segment[i+1];
x2=segment[i+2]-x1;
y2=segment[i+3]-y1;
x3=segment[i+4]-x1;
y3=segment[i+5]-y1;
x4=segment[i+6]-x1;
y4=segment[i+7]-y1;
k=samplesPerBezier;
while(--k>-1){
t=inc*k;
inv=1-t;
x=(t*t*x4+3*inv*(t*x3+inv*x2))*t+x1;
y=(t*t*y4+3*inv*(t*y3+inv*y2))*t+y1;
if(x>xMax){
xMax=x;
}elseif(x<xMin){
xMin=x;
}
if(y>yMax){
yMax=y;
}elseif(y<yMin){
yMin=y;
}
}
}
}
rawPath.centerX=(xMax+xMin)/2;
rawPath.centerY=(yMax+yMin)/2;
rawPath.left=xMin;
rawPath.width=(xMax-xMin);
rawPath.top=yMin;
rawPath.height=(yMax-yMin);
return(rawPath.size=(xMax-xMin)*(yMax-yMin));
},
_sortByComplexity=function(a,b){
returnb.length-a.length;
},
_sortBySize=function(a,b){
varsizeA=a.size||_getSize(a),
sizeB=b.size||_getSize(b);
return(Math.abs(sizeB-sizeA)<(sizeA+sizeB)/20)?(b.centerX-a.centerX)||(b.centerY-a.centerY):sizeB-sizeA;//if the size is within 10% of each other, prioritize position from left to right, then top to bottom.
_getClosestShapeIndex=function(sb,eb,checkReverse){//finds the index in a closed cubic bezier array that's closest to the angle provided (angle measured from the center or average x/y).
varl=sb.length,
sCenter=_getAverageXY(sb),//when comparing distances, adjust the coordinates as if the shapes are centered with each other.
eCenter=_getAverageXY(eb),
offsetX=eCenter[0]-sCenter[0],
offsetY=eCenter[1]-sCenter[1],
min=_getTotalMovement(sb,eb,0,offsetX,offsetY),
minIndex=0,
copy,d,i;
for(i=6;i<l;i+=6){
d=_getTotalMovement(sb,eb,i/6,offsetX,offsetY);
if(d<min){
min=d;
minIndex=i;
}
}
if(checkReverse){
copy=sb.slice(0);
_reverseBezier(copy);
for(i=6;i<l;i+=6){
d=_getTotalMovement(copy,eb,i/6,offsetX,offsetY);
if(d<min){
min=d;
minIndex=-i;
}
}
}
returnminIndex/6;
},
_getClosestAnchor=function(bezier,x,y){//finds the x/y of the anchor that's closest to the provided x/y coordinate (returns an array, like [x, y]). The bezier should be the top-level type that contains an array for each segment.
varj=bezier.length,
closestDistance=99999999999,
closestX=0,
closestY=0,
b,dx,dy,d,i,l;
while(--j>-1){
b=bezier[j];
l=b.length;
for(i=0;i<l;i+=6){
dx=b[i]-x;
dy=b[i+1]-y;
d=_sqrt(dx*dx+dy*dy);
if(d<closestDistance){
closestDistance=d;
closestX=b[i];
closestY=b[i+1];
}
}
}
return[closestX,closestY];
},
_getClosestSegment=function(bezier,pool,startIndex,sortRatio,offsetX,offsetY){//matches the bezier to the closest one in a pool (array) of beziers, assuming they are in order of size and we shouldn't drop more than 20% of the size, otherwise prioritizing location (total distance to the center). Extracts the segment out of the pool array and returns it.
varl=pool.length,
index=0,
minSize=Math.min(bezier.size||_getSize(bezier),pool[startIndex].size||_getSize(pool[startIndex]))*sortRatio,//limit things based on a percentage of the size of either the bezier or the next element in the array, whichever is smaller.
min=999999999999,
cx=bezier.centerX+offsetX,
cy=bezier.centerY+offsetY,
size,i,dx,dy,d;
for(i=startIndex;i<l;i++){
size=pool[i].size||_getSize(pool[i]);
if(size<minSize){
break;
}
dx=pool[i].centerX-cx;
dy=pool[i].centerY-cy;
d=_sqrt(dx*dx+dy*dy);
if(d<min){
index=i;
min=d;
}
}
d=pool[index];
pool.splice(index,1);
returnd;
},
_equalizeSegmentQuantity=function(start,end,shapeIndex,map,fillSafe){//returns an array of shape indexes, 1 for each segment.
if(longer[0].length>shorter[0].length){//since we use shorter[0] as the one to map the origination point of any brand new fabricated segments, do any subdividing first so that there are more points to choose from (if necessary)
//if start shape is closed, find the closest point to the start/end, and re-organize the bezier points accordingly so that the shape morphs in a more intuitive way.
if(i&&shapeIndex<0){//only happens if an array is passed as shapeIndex and a negative value is defined for an index beyond 0. Very rare, but helpful sometimes.
if(sb.closed!==eb.closed){//if one is closed and one isn't, don't close either one otherwise the tweening will look weird (but remember, the beginning and final states will honor the actual values, so this only affects the inbetween state)
//adds a certain number of points while maintaining the polygon/polyline shape (so that the start/end values can have a matching quantity of points to animate). Returns the revised string.
_equalizePointQuantity=function(a,quantity){
vartally=0,
x=parseFloat(a[0]),
y=parseFloat(a[1]),
s=x+","+y+" ",
max=0.999999,
newPointsPerSegment,i,l,j,factor,nextX,nextY;
l=a.length;
newPointsPerSegment=quantity*0.5/(l*0.5-1);
for(i=0;i<l-2;i+=2){
tally+=newPointsPerSegment;
nextX=parseFloat(a[i+2]);
nextY=parseFloat(a[i+3]);
if(tally>max){//compare with 0.99999 instead of 1 in order to prevent rounding errors
name=attr[i].nodeName.toLowerCase();//in Microsoft Edge, if you don't set the attribute with a lowercase name, it doesn't render correctly! Super weird.
data="M"+attr.x1+","+attr.y1+" L"+attr.x2+","+attr.y2;//previously, we just converted to "Mx,y Lx,y" but Safari has bugs that cause that not to render properly when using a stroke-dasharray that's not fully visible! Using a cubic bezier fixes that issue.
if(e===target){//if the shape matches the target element, the user wants to revert to the original which should have been stored in the data-original attribute
//adds an "isSmooth" array to each segment and populates it with a boolean value indicating whether or not it's smooth (the control points have basically the same slope). For any smooth control points, it converts the coordinates into angle (x, in radians) and length (y) and puts them into the same index value in a smoothData array.
//if the first and last points are identical, check to see if there's a smooth transition. We must handle this a bit differently due to their positions in the array.
_lastLinkedAnchor=0;//reset; we use _lastLinkedAnchor in the _tweenRotation() method to help make sure that close points don't get ripped apart and rotate opposite directions. Typically we want to go the shortest direction, but if the previous anchor is going a different direction, we override this logic (within certain thresholds)
if(startSmooth[i]&&endSmooth[i]){//if BOTH starting and ending values are smooth (meaning control points have basically the same slope), interpolate the rotation and length instead of the coordinates (this is what makes things smooth).
sData=startSeg.smoothData;
eData=endSeg.smoothData;
offset=i+((i===l-4)?7-l:5);//helps us accommodate wrapping (like if the end and start anchors are identical and the control points are smooth).
offset=i+((i===segment.length-4)?7-segment.length:5);//accommodates wrapping around of smooth points, like if the start and end anchors are on top of each other and their handles are smooth.
angle=_atan2(segment[offset]-segment[i+1],segment[offset-1]-segment[i]);//average the angles
d=_sqrt(dx*dx+dy*dy),//length from starting origin to starting point
sa=_atan2(dy,dx),
angleDif,short;
dx=end[i]-eo.x;
dy=end[i+1]-eo.y;
angleDif=_atan2(dy,dx)-sa;
short=_shortAngle(angleDif);
//in the case of control points, we ALWAYS link them to their anchor so that they don't get torn apart and rotate the opposite direction. If it's not a control point, we look at the most recently linked point as long as they're within a certain rotational range of each other.
MorphSVGPlugin.pathDataToBezier=function(data,vars){//converts SVG path data into an array of {x, y} objects that can be plugged directly into a bezier tween. You can optionally pass in a 2D matrix like [a, b, c, d, tx, ty] containing numbers that should transform each point.