!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).SVGAnimationPlayer=e()}(this,(function(){"use strict";function t(t){return void 0===t}function e(t){return document.createElementNS("http://www.w3.org/2000/svg",t)}function n(t,...e){e.forEach(e=>{t.setAttributeNS(null,e[0],e[1])})}const a={showInterface:!0,interfaceAnimation:!0,interfaceSize:1,interfaceColor:"#000",interfacePosition:"auto"};class s{constructor(t){this.item=t}setVariables(){this.startingAttributes=function(t){const e=new Map,{attributes:n}=t;for(let t=0;t{if("id"!==n&&"class"!==n&&"transform"!==n){const a=parseFloat(t);Number.isNaN(a)||a.toString()!==t?e[n]=t:e[n]=a}}),e}(this.startingAttributes)}initMatrix(t){!function(t,e){let n=null;const a=t.transform.baseVal;a.numberOfItems?(a.consolidate(),({matrix:n}=a.getItem(0))):n=e.createSVGMatrix(),a.initialize(e.createSVGTransformFromMatrix(n))}(this.item,t),this.matrix=this.item.transform.baseVal.getItem(0).matrix,this.SVGTransform=this.item.transform.baseVal.getItem(0)}resetAttributes(){!function(t,e){for(;t.attributes.length>0;)t.removeAttribute(t.attributes[0].name);e.forEach((e,n)=>{t.setAttribute(n,e)})}(this.item,this.startingAttributes)}decomposeMatrix(){this.currentAttributes.transform=function(t){const e={};return e.translate={x:t.e,y:t.f},e.scale=Math.sign(t.a)*Math.sqrt(t.a*t.a+t.c*t.c),e.rotate=Math.atan2(-t.c,t.a)*(180/Math.PI),e}(this.matrix)}setMatrix(t){this.SVGTransform.setMatrix(t)}setAttribute(t,e){this.item.setAttributeNS(null,t,e)}}function r(t){const e=[];return t.forEach(t=>{Object.keys(t.animation).forEach(n=>{e.push(...function(t,e){const n=[];return Array.isArray(e.animation[t])?e.animation[t].forEach(a=>{n.push([t,a,e])}):n.push([t,e.animation[t],e]),n}(n,t))})}),e}function i(t,e){const n=t.currentAttributes.transform,a=n.translate,s=Object.keys(e);function r(t){return s.indexOf(t)}let i;return-1!==r("translate")&&-1===r("rotate")&&-1===r("scale")?e.translate.x&&e.translate.y?i=n=>{a.x=e.translate.x(n),a.y=e.translate.y(n);const s=function(t,e,n){const a=t;return a.e=e,a.f=n,a}(t.matrix,a.x,a.y);t.setMatrix(s)}:e.translate.x?i=n=>{a.x=e.translate.x(n);const s=function(t,e){const n=t;return n.e=e,n}(t.matrix,a.x);t.setMatrix(s)}:e.translate.y&&(i=n=>{a.y=e.translate.y(n);const s=function(t,e){const n=t;return n.f=e,n}(t.matrix,a.y);t.setMatrix(s)}):-1===r("translate")&&-1!==r("rotate")&&-1===r("scale")?i=a=>{n.rotate=e.rotate(a);const s=function(t,e,n){const a=t,s=Math.sin(e)*n,r=Math.cos(e)*n;return a.a=r,a.b=-s,a.c=s,a.d=r,a}(t.matrix,n.rotate,n.scale);t.setMatrix(s)}:-1===r("translate")&&-1===r("rotate")&&-1!==r("scale")?i=a=>{n.scale=e.scale(a);const s=function(t,e,n){const a=t,s=Math.sin(n)*e,r=Math.cos(n)*e;return a.a=r,a.b=-s,a.c=s,a.d=r,a}(t.matrix,n.scale,n.rotate);t.setMatrix(s)}:-1!==r("translate")&&-1!==r("rotate")&&-1===r("scale")?e.translate.x&&e.translate.y?i=s=>{a.x=e.translate.x(s),a.y=e.translate.y(s),n.rotate=e.rotate(s);const r=function(t,e,n,a,s){const r=t,i=Math.sin(a)*s,o=Math.cos(a)*s;return r.a=o,r.b=-i,r.c=i,r.d=o,r.e=e,r.f=n,r}(t.matrix,a.x,a.y,n.rotate,n.scale);t.setMatrix(r)}:e.translate.x?i=s=>{a.x=e.translate.x(s),n.rotate=e.rotate(s);const r=function(t,e,n,a){const s=t,r=Math.sin(n)*a,i=Math.cos(n)*a;return s.a=i,s.b=-r,s.c=r,s.d=i,s.e=e,s}(t.matrix,a.x,n.rotate,n.scale);t.setMatrix(r)}:e.translate.y&&(i=s=>{a.y=e.translate.y(s),n.rotate=e.rotate(s);const r=function(t,e,n,a){const s=t,r=Math.sin(n)*a,i=Math.cos(n)*a;return s.a=i,s.b=-r,s.c=r,s.d=i,s.f=e,s}(t.matrix,a.y,n.rotate,n.scale);t.setMatrix(r)}):-1!==r("translate")&&-1===r("rotate")&&-1!==r("scale")?e.translate.x&&e.translate.y?i=s=>{a.x=e.translate.x(s),a.y=e.translate.y(s),n.scale=e.scale(s);const r=function(t,e,n,a,s){const r=t,i=Math.sin(s)*a,o=Math.cos(s)*a;return r.a=o,r.b=-i,r.c=i,r.d=o,r.e=e,r.f=n,r}(t.matrix,a.x,a.y,n.scale,n.rotate);t.setMatrix(r)}:e.translate.x?i=s=>{a.x=e.translate.x(s),n.scale=e.scale(s);const r=function(t,e,n,a){const s=t,r=Math.sin(a)*n,i=Math.cos(a)*n;return s.a=i,s.b=-r,s.c=r,s.d=i,s.e=e,s}(t.matrix,a.x,n.scale,n.rotate);t.setMatrix(r)}:e.translate.y&&(i=s=>{a.y=e.translate.y(s),n.scale=e.scale(s);const r=function(t,e,n,a){const s=t,r=Math.sin(a)*n,i=Math.cos(a)*n;return s.a=i,s.b=-r,s.c=r,s.d=i,s.f=e,s}(t.matrix,a.y,n.scale,n.rotate);t.setMatrix(r)}):-1===r("translate")&&-1!==r("rotate")&&-1!==r("scale")?i=a=>{n.rotate=e.rotate(a),n.scale=e.scale(a);const s=function(t,e,n){const a=t,s=Math.sin(n)*e,r=Math.cos(n)*e;return a.a=r,a.b=-s,a.c=s,a.d=r,a}(t.matrix,n.scale,n.rotate);t.setMatrix(s)}:-1!==r("translate")&&-1!==r("rotate")&&-1!==r("scale")&&(e.translate.x&&e.translate.y?i=s=>{a.x=e.translate.x(s),a.y=e.translate.y(s),n.rotate=e.rotate(s),n.scale=e.scale(s);const r=function(t,e,n,a,s){const r=t,i=Math.sin(s)*a,o=Math.cos(s)*a;return r.a=o,r.b=-i,r.c=i,r.d=o,r.e=e,r.f=n,r}(t.matrix,a.x,a.y,n.scale,n.rotate);t.setMatrix(r)}:e.translate.x?i=s=>{a.x=e.translate.x(s),n.rotate=e.rotate(s),n.scale=e.scale(s);const r=function(t,e,n,a){const s=t,r=Math.sin(a)*n,i=Math.cos(a)*n;return s.a=i,s.b=-r,s.c=r,s.d=i,s.e=e,s}(t.matrix,a.x,n.scale,n.rotate);t.setMatrix(r)}:e.translate.y&&(i=s=>{a.y=e.translate.y(s),n.rotate=e.rotate(s),n.scale=e.scale(s);const r=function(t,e,n,a){const s=t,r=Math.sin(a)*n,i=Math.cos(a)*n;return s.a=i,s.b=-r,s.c=r,s.d=i,s.f=e,s}(t.matrix,a.y,n.scale,n.rotate);t.setMatrix(r)})),i}function o(t,e,n){const{currentAttributes:a}=t;return s=>{const{value:r}=n;a[e]=r(s),t.setAttribute(e,a[e])}}function c(t,e,n){let a;const{range:s}=e;var r;return Array.isArray(s)?1===s.length?a=function(t,e){let n;return n=e.local?n=>{n>=e.range[0]&&t(n-e.range[0])}:n=>{n>=e.range[0]&&t(n)},n}(t,e):2===s.length&&(a=function(t,e,n){let a;return a=e.local?s=>{s>=e.range[0]&&s<=e.range[1]?t(s-e.range[0]):s>e.range[1]&&(t(e.range[1]-e.range[0]),n(a))}:s=>{s>=e.range[0]&&s<=e.range[1]?t(s):s>e.range[1]&&(t(e.range[1]),n(a))},a}(t,e,n)):(r=s,!Number.isNaN(parseFloat(r))&&Number.isFinite(r)&&(a=function(t,e,n){let a;return a=e.local?s=>{s>=e.range&&(t(0),n(a))}:s=>{s>=e.range&&(t(e.range),n(a))},a}(t,e,n))),a}function l(t,e,n,a){return(t/=a/2)<1?n/2*t*t+e:-n/2*(--t*(t-2)-1)+e}var u=Object.freeze({__proto__:null,linear:function(t,e,n,a){return n*t/a+e},easeInQuad:function(t,e,n,a){return n*(t/=a)*t+e},easeOutQuad:function(t,e,n,a){return-n*(t/=a)*(t-2)+e},easeInOutQuad:l,easeInCubic:function(t,e,n,a){return n*(t/=a)*t*t+e},easeOutCubic:function(t,e,n,a){return t/=a,n*(--t*t*t+1)+e},easeInOutCubic:function(t,e,n,a){return(t/=a/2)<1?n/2*t*t*t+e:n/2*((t-=2)*t*t+2)+e}});function f(t,e,n,a){const s=e[1]-e[0];let r=t.from,i=t.to-r;const o=u[t.easing]||l;if(r)return t=>o(t,r,i,s);let c=()=>{if(r=n[a],void 0===r)throw new Error('No starting value! Specify "from" value, or add it to animatated SVG element');return i=t.to-r,c=()=>r,r};return t=>o(t,c(),i,s)}function h(t){const e=[];return t.forEach(t=>{!function(t){const e=t[1];Object.keys(e).forEach(t=>{if("translate"===t){if("number"==typeof e[t].x){const n=Object.assign(e[t].x);e[t].x=()=>n}if("number"==typeof e[t].y){const n=Object.assign(e[t].y);e[t].x=()=>n}}else if("rotate"===t||"scale"===t){if("number"==typeof e[t]){const n=Object.assign(e[t]);e[t]=()=>n}}else if("value"===t&&("number"==typeof e[t]||"string"==typeof e[t])){const n=Object.assign(e[t]);e[t]=()=>n}})}(t),function(t){if(Array.isArray(t[1].range)&&2===t[1].range.length){const{currentAttributes:e}=t[2],n=t[0];if("transform"===n){const n=t[1],a=e.transform;if("object"==typeof n.translate){n.local=!0;const t=e.transform.translate;"object"==typeof n.translate.x&&(n.translate.x=f(n.translate.x,n.range,t,"x")),"object"==typeof n.translate.y&&(n.translate.y=f(n.translate.y,n.range,t,"y"))}"object"==typeof n.rotate&&(n.local=!0,n.rotate=f(n.rotate,n.range,a,"rotate")),"object"==typeof n.scale&&(n.local=!0,n.scale=f(n.scale,n.range,a,"scale"))}else{const a=t[1];"object"==typeof a.value&&(a.local=!0,a.value=f(a.value,a.range,e,n))}}}(t),e.push(t)}),e}function m(t){this.loop.splice(this.loop.indexOf(t),1),this.loop<1&&this.end()}function d(t,e){const n=function(t){const e=[];return t.forEach(t=>{const[n,a,s]=t;"transform"===n?e.push([i(s,a),a]):e.push([o(s,n,a),a])}),e}(h(r(t))),a=m.bind(e);e.loop.push(...function(t,e){const n=[];return t.forEach(t=>{const[a,s]=t;s.range?n.push(c(a,s,e)):n.push(a)}),n}(n,a))}function p(t){const a=function(t,a){const s=e("g");s.className.baseVal="play-pause";const r=e("polygon"),i=e("polygon");n(r,["points","0,0 0,-20 20,-10 20,-10"]),n(i,["points","0,0 0,-20 20,-10 20,-10"]),s.appendChild(r),s.appendChild(i);const o=e("rect");o.id="play-pause",n(o,["x","0"],["y","-20"],["width","20"],["height","20"],["fill-opacity","0"]);const c=e("g");return c.appendChild(s),c.appendChild(o),function(){const t=a.viewBox.baseVal,e=a.createSVGMatrix();e.e=t.x+.05*t.width,e.f=t.y+.95*t.height;const n=t.height/400;e.a=n,e.d=n,c.transform.baseVal.initialize(a.createSVGTransformFromMatrix(e))}(),function(){"none"!==t.interfaceColor&&n(s,["fill",t.interfaceColor]);const{matrix:e}=c.transform.baseVal.getItem(0);e.a*=t.interfaceSize,e.d*=t.interfaceSize,"auto"!==t.interfacePosition&&([e.e,e.f]=t.interfacePosition),c.transform.baseVal.getItem(0).setMatrix(e)}(),a.appendChild(c),{play:function(){let e=10;t.interfaceAnimation||(e=1),function t(){e>=0?(r.setAttribute("points",`${e/10},0 ${e/10},-20 ${10-e/10},${-15-e/2} ${10-e/10},${e/2-5}`),i.setAttribute("points",`${10+e/10},${e/2-5} ${10+e/10},${-15-e/2} ${20-e/10},${-10-e} ${20-e/10},${-10+e}`),e-=1,window.requestAnimationFrame(t)):(r.setAttribute("points","0,0 0,-20 20,-10 20,-10"),i.setAttribute("points","0,0 0,-20 20,-10 20,-10"))}()},pause:function(){let e=1;t.interfaceAnimation||(e=10),function t(){e<=10&&(r.setAttribute("points",`${e/10},0 ${e/10},-20 ${10-e/10},${-15-e/2} ${10-e/10},${e/2-5}`),i.setAttribute("points",`${10+e/10},${e/2-5} ${10+e/10},${-15-e/2} ${20-e/10},${-10-e} ${20-e/10},${-10+e}`),e+=1,window.requestAnimationFrame(t))}()},off:function(){let e=1;t.interfaceAnimation||(e=10),function t(){e<=10?(r.setAttribute("points",`1,${-e} 1,${-20+e} 9,${-20+e} 9,${-e}`),i.setAttribute("points",`11,${-e} 11,${-20+e} 19,${-20+e} 19,${-e}`),e+=1,window.requestAnimationFrame(t)):n(s,["display","none"])}()},on:function(){let e=10;t.interfaceAnimation||(e=0),n(s,["display","block"]),function t(){e>=0?(r.setAttribute("points",`0,${-e} 0,${-20+e} 20,-10 20,-10`),i.setAttribute("points",`0,${-e} 0,${-20+e} 20,-10 20,-10`),e-=1,window.requestAnimationFrame(t)):(r.setAttribute("points","0,0 0,-20 20,-10 20,-10"),i.setAttribute("points","0,0 0,-20 20,-10 20,-10"))}()},button:o}}(t.settings,t.svg),s=function(t,a){const s=e("marker");s.id=10*Math.random(),s.className.baseVal="marker",n(s,["viewBox","0 0 10 10"],["refX","1"],["refY","5"],["markerWidth","3"],["markerHeight","3"],["orient","auto"]);const r=e("path");n(r,["d","M 0 0 L 10 5 L 0 10 z"]),s.appendChild(r);const i=e("g");i.className.baseVal="interface refresh";const o=e("path"),c=e("path");n(o,["d","M0 -10 a 10 10, 90 0 1 10 -10"],["marker-end",`url(#${s.id})`],["stroke-dasharray","15"]),n(c,["d","M20 -10 a 10 10, 90 0 1 -10 10"],["marker-end",`url(#${s.id})`],["stroke-dasharray","15"]),i.appendChild(o),i.appendChild(c),n(i,["fill","none"],["stroke-width","2"],["transform","translate(30, 0)"],["display","none"]);const l=e("rect");l.id="refresh",n(l,["x","0"],["y","-20"],["width","20"],["height","20"],["fill-opacity","0"],["transform","translate(30, 0)"]);const u=e("g");u.appendChild(i),u.appendChild(l);let f=a.getElementsByTagName("defs")[0];return f||(f=e("defs"),a.insertBefore(f,a.firstChild)),function(){const t=a.viewBox.baseVal,e=a.createSVGMatrix();e.e=t.x+.05*t.width,e.f=t.y+.95*t.height;const n=t.height/400;e.a=n,e.d=n,u.transform.baseVal.initialize(a.createSVGTransformFromMatrix(e))}(),function(){"none"!==t.interfaceColor&&(n(i,["stroke",t.interfaceColor]),n(s,["fill",t.interfaceColor]));const{matrix:e}=u.transform.baseVal.getItem(0);e.a*=t.interfaceSize,e.d*=t.interfaceSize,"auto"!==t.interfacePosition&&([e.e,e.f]=t.interfacePosition),u.transform.baseVal.getItem(0).setMatrix(e)}(),f.appendChild(s),a.appendChild(u),{off:function(){let e=1;t.interfaceAnimation||(e=10),function t(){e<=10?(o.setAttribute("stroke-dashoffset",1.5*-e),c.setAttribute("stroke-dashoffset",1.5*-e),e+=1,window.requestAnimationFrame(t)):n(i,["display","none"])}()},on:function(){n(i,["display","block"]);let e=10;t.interfaceAnimation||(e=1),function t(){e>=0&&(o.setAttribute("stroke-dashoffset",1.5*-e),c.setAttribute("stroke-dashoffset",1.5*-e),e-=1,window.requestAnimationFrame(t))}()},button:l}}(t.settings,t.svg);return a.button.addEventListener("click",(function(){"not started"===t.status||"paused"===t.status?t.play():"playing"===t.status&&t.pause()}),!1),s.button.addEventListener("click",(function(){"playing"!==t.status&&"paused"!==t.status&&"ended"!==t.status||t.refresh()}),!1),{playPause:a,refresh:s}}const y=new CustomEvent("svgRefresh");class b{constructor(e,n=document){this.status="not started",this.timer={animationId:0,startTime:0,time:0},this.settings=function(e){const n={};return Object.keys(a).forEach(s=>{t(e)||t(e[s])?n[s]=a[s]:n[s]=e[s]}),n}(e),this.objectList=new Set,this.loop=[],this.context=n}reset(){this.objectList.forEach(t=>{t.resetAttributes(),t.setVariables(),t.initMatrix(this.svg),t.decomposeMatrix()}),this.loop=[],d(this.objectList,this)}}return b.prototype.play=function(){const t=this;function e(){t.timer.time=Date.now()-t.timer.startTime,function(t,e){for(let n=0;n{window.cancelAnimationFrame(t.timer.animationId),this.settings.showInterface&&this.interfaceControler.playPause.off()},10)}},b.prototype.add=function(...t){const e=new Set;t.forEach(t=>{if(!Object.prototype.hasOwnProperty.call(t,"object"))throw new Error(`Object ${t} must have "object" property. which is query selector or actual DOM object`);const n="string"==typeof t.object?this.context.querySelector(t.object):t.object;if(!this.context.contains(n))throw new Error(`Cannot find ${"string"==typeof t.object?`DOM element that match "${t.object}" query selector`:`${t.object} in DOM`}`);const a=new s(n);if(!Object.prototype.hasOwnProperty.call(t,"animation"))throw new Error(`Object ${t} must have "animation" property`);a.animation=t.animation,this.svg||(this.svg=function(t){let e=t.item;for(;e.tagName;){if("svg"===e.tagName.toLowerCase())return e;e=e.parentNode}throw new Error("Cannot find SVG element! All animated elements must have SVG parent")}(a)),e.add(a),a.setVariables(),a.initMatrix(this.svg),a.decomposeMatrix(),this.objectList.add(a)}),!this.interfaceControler&&this.settings.showInterface&&(this.interfaceControler=p(this)),d(e,this)},b}));