UNPKG

jparticles

Version:

A lightweight, efficient and easy-to-use Canvas library for building some cool particle effects.

2 lines (1 loc) 4.25 kB
this.JParticles=this.JParticles||{},this.JParticles.Wave=function(t,e){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=o(t),i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])})(t,e)};function s(t,e){for(var o=0,n=e.length,i=t.length;o<n;o++,i++)t[i]=e[o];return t}var r,a,f,c=2*Math.PI;Function.prototype.toString.call(Object),/msie\s8.0/i.test(navigator.userAgent),function(t){t.DESTROY="DESTROY",t.RESIZE="RESIZE"}(r||(r={})),function(t){t.PROGRESS="PROGRESS",t.FINISHED="FINISHED"}(a||(a={})),function(t){t.FINISHED="FINISHED"}(f||(f={}));var l=["fill","fillColor","line","lineColor","lineWidth","offsetLeft","offsetTop","crestHeight","speed"],u=["opacity","mask","maskMode"],h=s(s([],l),["crestCount"]);return function(t){function o(e,n){var i=t.call(this,o.defaultConfig,e,n)||this;return i.waveLength=[],i.bootstrap(),i}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(o,t),o.prototype.init=function(){this.ownResizeEvent(),this.optionsNormalize(),this.loadMaskImage(),this.createDots()},o.prototype.optionsNormalize=function(){var t=this;h.forEach((function(n){for(var i=t.options.num,s=t.options[n],r=[],a="offsetLeft"===n?t.canvasWidth:t.canvasHeight;i--;){var f=Array.isArray(s)?s[i]:s;r[i]=e.isUndefined(f)?t.getOptionDefaultValue(n):o.getOptionProcessedValue(n,f,a),"crestCount"===n&&(t.waveLength[i]=t.canvasWidth/r[i])}t.options[n]=r}))},o.prototype.getOptionDefaultValue=function(t){var o=this.canvasWidth,n=this.canvasHeight;switch(t){case"lineColor":case"fillColor":return e.randomColor();case"lineWidth":return e.randomInRange(2,.2);case"offsetLeft":return Math.random()*o;case"offsetTop":case"crestHeight":return Math.random()*n;case"crestCount":return e.randomInRange(o/2,1);case"speed":return e.randomInRange(.4,.1);case"fill":return!1;case"line":return!0}},o.getOptionProcessedValue=function(t,o,n){return"offsetTop"===t||"offsetLeft"===t||"crestHeight"===t?e.calcQuantity(o,n):o},o.prototype.createDots=function(){for(var t=this.canvasWidth,e=this.waveLength,o=this.options.num;o--;){for(var n=[],i=c/e[o],s=0;s<=t;s++)n.push({x:s,y:s*i});this.elements[o]=n}},o.prototype.draw=function(){var t=this;this.clearCanvasAndSetGlobalAttrs(),this.renderMaskMode((function(){t.drawWaves()})),this.requestAnimationFrame()},o.prototype.drawWaves=function(){var t=this,e=t.ctx,o=t.canvasWidth,n=t.canvasHeight,i=t.isPaused,s=this.options;this.elements.forEach((function(t,r){var a=s.crestHeight[r],f=s.offsetLeft[r],c=s.offsetTop[r],l=s.speed[r];e.save(),e.beginPath(),t.forEach((function(t,o){e[o?"lineTo":"moveTo"](t.x,a*Math.sin(t.y+f)+c),!i&&(t.y-=l)})),s.fill[r]&&(e.lineTo(o,n),e.lineTo(0,n),e.closePath(),e.fillStyle=s.fillColor[r],e.fill()),s.line[r]&&(e.lineWidth=s.lineWidth[r],e.strokeStyle=s.lineColor[r],e.stroke()),e.restore()}))},o.prototype.ownResizeEvent=function(){var t=this,e=["offsetLeft","offsetTop","crestHeight"],o=this.options;this.onResize((function(n,i){e.forEach((function(t){var e="offsetLeft"===t?n:i;o[t].forEach((function(t,o,n){n[o]=t*e}))})),t.elements.forEach((function(t){t.forEach((function(t){t.x*=n,t.y*=i}))}))}))},o.prototype.updateComplexOptions=function(t,n){if(n){var i="offsetLeft"===t?this.canvasWidth:this.canvasHeight,s=this.options,r=Array.isArray(n);s[t].forEach((function(s,a,f){var c=r?n[a]:n;c=o.getOptionProcessedValue(t,c,i),e.isUndefined(c)&&(c=s),f[a]=c}))}},o.prototype.updatePlainOptions=function(t,e){this.options[t]=e,"mask"===t&&this.loadMaskImage()},o.prototype.setOptions=function(t){if(this.isRunningSupported&&e.isPlainObject(t))for(var o in t)Object.hasOwnProperty.call(t,o)&&(-1!==u.indexOf(o)?this.updatePlainOptions(o,t[o]):-1!==l.indexOf(o)&&this.updateComplexOptions(o,t[o]))},o.defaultConfig={num:3,fill:!1,fillColor:[],line:!0,lineColor:[],lineWidth:[],offsetLeft:[],offsetTop:[],crestHeight:[],crestCount:[],speed:[]},o}(n.default)}(JParticles.classes.Mask,JParticles.utils);