jparticles
Version:
A lightweight, efficient and easy-to-use Canvas library for building some cool particle effects.
2 lines (1 loc) • 3.15 kB
JavaScript
this.JParticles=this.JParticles||{},this.JParticles.Snow=function(t,e){"use strict";function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i,o,a,s=n(t),r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function p(t,e){for(var n=0,i=e.length,o=t.length;n<i;n++,o++)t[o]=e[n];return t}return Function.prototype.toString.call(Object),/msie\s8.0/i.test(navigator.userAgent),function(t){t.DESTROY="DESTROY",t.RESIZE="RESIZE"}(i||(i={})),function(t){t.PROGRESS="PROGRESS",t.FINISHED="FINISHED"}(o||(o={})),function(t){t.FINISHED="FINISHED"}(a||(a={})),function(t){function n(e,i){var o=t.call(this,n.defaultConfig,e,i)||this;return o.startTime=Date.now(),o.isFinished=!1,o.bootstrap(),o}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 n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(n,t),n.prototype.init=function(){this.createSnowflakes()},n.prototype.createSnowflake=function(){var t=this.options,n=t.maxR,i=t.minR,o=t.maxSpeed,a=t.minSpeed,s=t.spin,r=t.spinMaxSpeed,p=t.spinMinSpeed,h=e.randomInRange(n,i);return{r:h,x:Math.random()*this.canvasWidth,y:-h,vx:e.randomSpeed(o,a),vy:Math.abs(h*e.randomSpeed(o,a)),color:this.getColor(),swingAt:Date.now(),shape:this.getShapeData(),rotate:s?e.randomInRange(0,360):0,rotateSpeed:e.randomSpeed(r,p)}},n.prototype.createSnowflakes=function(){for(var t=Math.max(0,Math.ceil(Math.random()*this.options.num));t--;)this.elements.push(this.createSnowflake())},n.prototype.draw=function(){var t=this,e=this,n=e.canvasWidth,i=e.canvasHeight,o=e.isPaused,s=this.options,r=s.maxR,p=s.swing,h=s.swingInterval,c=s.swingProbability,l=s.duration;(this.clearCanvasAndSetGlobalAttrs(),this.elements.forEach((function(e,a,s){var u=e.x,f=e.y,d=e.r;t.updateElementRotate(e),t.drawShape(e),o||(e.x+=e.vx,e.y+=e.vy,p&&Date.now()-e.swingAt>h&&Math.random()<d/r*c&&(e.swingAt=Date.now(),e.vx*=-1),u+d<0||u-d>n?l?s.splice(a,1):s.splice(a,1,t.createSnowflake()):f-d>i&&s.splice(a,1))})),o)||(!(!!l&&Date.now()-this.startTime>l)&&Math.random()>.9&&this.createSnowflakes(),this.elements.length?this.requestAnimationFrame():(this.isFinished=!0,this.eventEmitter.trigger(a.FINISHED)))},n.prototype.updateElementRotate=function(t){this.options.spin&&!this.isPaused&&(t.rotate+=t.rotateSpeed,t.rotate>=360&&(t.rotate=t.rotate-360))},n.prototype.fallAgain=function(){this.isRunningSupported&&!this.isCanvasRemoved&&!this.isPaused&&this.isFinished&&(this.isFinished=!1,this.startTime=Date.now(),this.createSnowflakes(),this.draw())},n.prototype.onFinished=function(){for(var t,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return(t=this.eventEmitter).on.apply(t,p([a.FINISHED],e)),this},n.defaultConfig={num:6,color:"#fff",maxR:6.5,minR:.5,maxSpeed:.6,minSpeed:.1,swing:!0,swingInterval:2e3,swingProbability:.06,spin:!1,spinMaxSpeed:5,spinMinSpeed:1},n}(s.default)}(JParticles.classes.Shape,JParticles.utils);