forked from ConnorAtherton/walkway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkway.min.js
1 lines (1 loc) · 3.17 KB
/
walkway.min.js
1
!function(t){"use strict";function e(t){var e=["path","line"],n=e.reduce(function(e,n){return e+t+" "+n+", "},"");return n.slice(0,-2)}function n(t){return this instanceof n?("string"==typeof t&&(t={selector:t}),t.selector?(this.opts=t,this.selector=t.selector,this.duration=t.duration||500,this.easing=s[t.easing]||s.easeInOutCubic,this.paths=this.getPaths(),this.setInitialStyles(),void(this.id=!1)):this.error("A selector needs to be specified")):new n(t)}function i(t,e,n){this.el=t,this.length=t.getTotalLength(),this.duration=e,this.easing=n,this.animationStart=null,this.animationStarted=!1}function a(t,e,n){this.el=t,this.length=r(t),this.duration=e,this.easing=n,this.animationStart=null,this.animationStarted=!1}function r(t){var e=t.getAttribute("x1"),n=t.getAttribute("x2"),i=t.getAttribute("y1"),a=t.getAttribute("y2");return Math.sqrt(Math.pow(e-n,2)+Math.pow(i-a,2))}var o=0;t.requestAnimationFrame=t.requestAnimationFrame||t.mozRequestAnimationFrame||t.webkitRequestAnimationFrame||t.msRequestAnimationFrame,t.cancelAnimationFrame=t.cancelAnimationFrame||t.mozCancelAnimationFrame,t.requestAnimationFrame||(t.requestAnimationFrame=function(e){var n=(new Date).getTime(),i=Math.max(0,16-(n-o)),a=t.setTimeout(function(){e(n+i)},i);return o=n+i,a}),t.cancelAnimationFrame||(t.cancelAnimationFrame=function(t){clearTimeout(t)});var s={linear:function(t){return t},easeInQuad:function(t){return t*t},easeOutQuad:function(t){return t*(2-t)},easeInOutQuad:function(t){return.5>t?2*t*t:-1+(4-2*t)*t},easeInCubic:function(t){return t*t*t},easeOutCubic:function(t){return--t*t*t+1},easeInOutCubic:function(t){return.5>t?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1},easeInQuart:function(t){return t*t*t*t},easeOutQuart:function(t){return 1- --t*t*t*t},easeInOutQuart:function(t){return.5>t?8*t*t*t*t:1-8*--t*t*t*t},easeInQuint:function(t){return t*t*t*t*t},easeOutQuint:function(t){return 1+--t*t*t*t*t},easeInOutQuint:function(t){return.5>t?16*t*t*t*t*t:1+16*--t*t*t*t*t}};n.prototype.error=function(t){console.log("Walkway error: "+t)},n.prototype.getPaths=function(){var t=this,n=e(this.selector),r=document.querySelectorAll(n);return r=Array.prototype.slice.call(r),r.map(function(e){return"path"===e.tagName?new i(e,t.duration,t.easing):"line"===e.tagName?new a(e,t.duration,t.easing):void 0})},n.prototype.setInitialStyles=function(){this.paths.forEach(function(t){t.el.style.strokeDasharray=t.length+" "+t.length,t.el.style.strokeDashoffset=t.length})},n.prototype.draw=function(e){var n,i=this.paths.length;if(0===i)return e&&"function"==typeof e&&e(),t.cancelAnimationFrame(this.id);for(;i--;){n=this.paths[i];var a=n.update();a&&this.paths.splice(i,1)}this.id=t.requestAnimationFrame(this.draw.bind(this,e))},i.prototype.update=function(){this.animationStarted||(this.animationStart=Date.now(),this.animationStarted=!0);var t=this.easing((Date.now()-this.animationStart)/this.duration);return t>=1?!0:(this.el.style.strokeDashoffset=Math.floor(this.length*(1-t)),!1)},a.prototype.update=function(){this.animationStarted||(this.animationStart=Date.now(),this.animationStarted=!0);var t=this.easing((Date.now()-this.animationStart)/this.duration);return t>=1?!0:(this.el.style.strokeDashoffset=Math.floor(this.length*(1-t)),!1)},t.Walkway=n}(this);