forked from VincentGarreau/particles.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.min.js
1 lines (1 loc) · 9.66 KB
/
particles.min.js
1
function hexToRgb(e){var a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;e=e.replace(a,function(e,a,t,n){return a+a+t+t+n+n});var t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);return t?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null}function clamp(e,a,t){return Math.min(Math.max(e,a),t)}function isInArray(e,a){return a.indexOf(e)>-1}var pJS=function(e,a){var t=document.querySelector("#"+e+" > .particles-js-canvas-el");this.pJS={canvas:{el:t,w:t.offsetWidth,h:t.offsetHeight},particles:{number:{value:400},color:{value:"#fff"},shape:{type:"circle",image:{src:"",width:100,height:100}},opacity:{value:1,random:!1},size:{value:20,random:!1},move:{enable:!0,speed:2,random:!1,straight:!0},array:[]},interactivity:{detect_on:"canvas",events:{resize:!0}},retina_detect:!1,fn:{interact:{},modes:{},vendors:{}},tmp:{}};var n=this.pJS;a&&Object.deepExtend(n,a),n.tmp.obj={size_value:n.particles.size.value,move_speed:n.particles.move.speed},n.fn.retinaInit=function(){n.retina_detect&&window.devicePixelRatio>1?(n.canvas.pxratio=window.devicePixelRatio,n.tmp.retina=!0):(n.canvas.pxratio=1,n.tmp.retina=!1),n.canvas.w=n.canvas.el.offsetWidth*n.canvas.pxratio,n.canvas.h=n.canvas.el.offsetHeight*n.canvas.pxratio,n.particles.size.value=n.tmp.obj.size_value*n.canvas.pxratio,n.particles.move.speed=n.tmp.obj.move_speed*n.canvas.pxratio},n.fn.canvasInit=function(){n.canvas.ctx=n.canvas.el.getContext("2d")},n.fn.canvasSize=function(){n.canvas.el.width=n.canvas.w,n.canvas.el.height=n.canvas.h,n&&n.interactivity.events.resize&&window.addEventListener("resize",function(){n.canvas.w=n.canvas.el.offsetWidth,n.canvas.h=n.canvas.el.offsetHeight,n.tmp.retina&&(n.canvas.w*=n.canvas.pxratio,n.canvas.h*=n.canvas.pxratio),n.canvas.el.width=n.canvas.w,n.canvas.el.height=n.canvas.h,n.particles.move.enable||(n.fn.particlesEmpty(),n.fn.particlesCreate(),n.fn.particlesDraw())})},n.fn.canvasPaint=function(){n.canvas.ctx.fillRect(0,0,n.canvas.w,n.canvas.h)},n.fn.canvasClear=function(){n.canvas.ctx.clearRect(0,0,n.canvas.w,n.canvas.h)},n.fn.particle=function(e,a,t){if(this.radius=(n.particles.size.random?Math.random():1)*n.particles.size.value,this.x=n.canvas.w/2,this.y=n.canvas.h/2,this.x=t?t.x:Math.random()*n.canvas.w,this.y=t?t.y:Math.random()*n.canvas.h,this.x>n.canvas.w-2*this.radius?this.x=this.x-this.radius:this.x<2*this.radius&&(this.x=this.x+this.radius),this.y>n.canvas.h-2*this.radius?this.y=this.y-this.radius:this.y<2*this.radius&&(this.y=this.y+this.radius),this.color={},"object"==typeof e.value)if(e.value instanceof Array){var r=e.value[Math.floor(Math.random()*n.particles.color.value.length)];this.color.rgb=hexToRgb(r)}else void 0!=e.value.r&&void 0!=e.value.g&&void 0!=e.value.b&&(this.color.rgb={r:e.value.r,g:e.value.g,b:e.value.b}),void 0!=e.value.h&&void 0!=e.value.s&&void 0!=e.value.l&&(this.color.hsl={h:e.value.h,s:e.value.s,l:e.value.l});this.opacity=(n.particles.opacity.random?Math.random():1)*n.particles.opacity.value;var i={x:1,y:1};n.particles.move.straight?(this.vx=i.x,this.vy=i.y,n.particles.move.random&&(this.vx=this.vx*Math.random(),this.vy=this.vy*Math.random())):(this.vx=i.x+Math.random()-.5,this.vy=i.y+Math.random()-.5),this.theta=Math.floor(360*Math.random()+1),this.forward_rotation=Math.random()<.5;var s=n.particles.shape.type;if("object"==typeof s){if(s instanceof Array){var o=s[Math.floor(Math.random()*s.length)];this.shape=o}}else this.shape=s;if("image"==this.shape){var c=n.particles.shape;this.img={src:c.image.src,ratio:c.image.width/c.image.height},this.img.ratio||(this.img.ratio=1),"svg"==n.tmp.img_type&&void 0!=n.tmp.source_svg&&(n.fn.vendors.createSvgImg(this),n.tmp.pushing&&(this.img.loaded=!1))}},n.fn.particle.prototype.draw=function(){function e(){n.canvas.ctx.save(),n.canvas.ctx.translate(a.x,a.y),n.canvas.ctx.rotate(a.theta*Math.PI/180),n.canvas.ctx.drawImage(s,0-a.radius,0-a.radius/a.img.ratio,2*t,2*t/a.img.ratio),n.canvas.ctx.restore()}var a=this,t=a.radius,r=a.opacity;if(a.color.rgb)var i="rgba("+a.color.rgb.r+","+a.color.rgb.g+","+a.color.rgb.b+","+r+")";else var i="hsla("+a.color.hsl.h+","+a.color.hsl.s+"%,"+a.color.hsl.l+"%,"+r+")";switch(n.canvas.ctx.fillStyle=i,a.shape){case"circle":n.canvas.ctx.beginPath(),n.canvas.ctx.arc(a.x,a.y,t,0,2*Math.PI,!1);break;case"image":if("svg"==n.tmp.img_type)var s=a.img.obj;else var s=n.tmp.img_obj;s&&e()}n.canvas.ctx.closePath(),n.canvas.ctx.fill()},n.fn.particlesCreate=function(){for(var e=0;e<n.particles.number.value;e++)n.particles.array.push(new n.fn.particle(n.particles.color,n.particles.opacity.value))},n.fn.particlesUpdate=function(){for(var e=0;e<n.particles.array.length;e++){var a=n.particles.array[e],t=1,r=!1;if(a.forward_rotation?(a.theta=a.theta+t,a.theta<360||(a.theta=0)):(a.theta=a.theta-t,a.theta>0||(a.theta=360)),n.particles.move.enable){var i=n.particles.move.speed/2;a.x+=a.vx*i,a.y+=a.vy*i}a.x+a.radius>n.canvas.w?(a.vx=-a.vx,r=!0):a.x-a.radius<0&&(a.vx=-a.vx,r=!0),a.y+a.radius>n.canvas.h?(a.vy=-a.vy,r=!0):a.y-a.radius<0&&(a.vy=-a.vy,r=!0),r&&(a.forward_rotation=!a.forward_rotation)}},n.fn.particlesDraw=function(){n.canvas.ctx.clearRect(0,0,n.canvas.w,n.canvas.h),n.fn.particlesUpdate();for(var e=0;e<n.particles.array.length;e++){var a=n.particles.array[e];a.draw()}},n.fn.particlesEmpty=function(){n.particles.array=[]},n.fn.particlesRefresh=function(){cancelRequestAnimFrame(n.fn.checkAnimFrame),cancelRequestAnimFrame(n.fn.drawAnimFrame),n.tmp.source_svg=void 0,n.tmp.img_obj=void 0,n.tmp.count_svg=0,n.fn.particlesEmpty(),n.fn.canvasClear(),n.fn.vendors.start()},n.fn.vendors.eventsListeners=function(){n.interactivity.el="window"==n.interactivity.detect_on?window:n.canvas.el},n.fn.vendors.createSvgImg=function(e){var a=n.tmp.source_svg,t=/#([0-9A-F]{3,6})/gi,r=a.replace(t,function(){if(e.color.rgb)var a="rgba("+e.color.rgb.r+","+e.color.rgb.g+","+e.color.rgb.b+","+e.opacity+")";else var a="hsla("+e.color.hsl.h+","+e.color.hsl.s+"%,"+e.color.hsl.l+"%,"+e.opacity+")";return a}),i=new Blob([r],{type:"image/svg+xml;charset=utf-8"}),s=window.URL||window.webkitURL||window,o=s.createObjectURL(i),c=new Image;c.addEventListener("load",function(){e.img.obj=c,e.img.loaded=!0,s.revokeObjectURL(o),n.tmp.count_svg++}),c.src=o},n.fn.vendors.destroypJS=function(){cancelAnimationFrame(n.fn.drawAnimFrame),t.remove(),pJSDom=null},n.fn.vendors.exportImg=function(){window.open(n.canvas.el.toDataURL("image/png"),"_blank")},n.fn.vendors.loadImg=function(e){if(n.tmp.img_error=void 0,""!=n.particles.shape.image.src)if("svg"==e){var a=new XMLHttpRequest;a.open("GET",n.particles.shape.image.src),a.onreadystatechange=function(e){4==a.readyState&&(200==a.status?(n.tmp.source_svg=e.currentTarget.response,n.fn.vendors.checkBeforeDraw()):(console.log("Error pJS - Image not found"),n.tmp.img_error=!0))},a.send()}else{var t=new Image;t.addEventListener("load",function(){n.tmp.img_obj=t,n.fn.vendors.checkBeforeDraw()}),t.src=n.particles.shape.image.src}else console.log("Error pJS - No image.src"),n.tmp.img_error=!0},n.fn.vendors.draw=function(){"image"==n.particles.shape.type?"svg"==n.tmp.img_type?n.tmp.count_svg<n.particles.number.value?n.tmp.img_error||(n.fn.drawAnimFrame=requestAnimFrame(n.fn.vendors.draw)):(n.fn.particlesDraw(),n.particles.move.enable?n.fn.drawAnimFrame=requestAnimFrame(n.fn.vendors.draw):cancelRequestAnimFrame(n.fn.drawAnimFrame)):void 0!=n.tmp.img_obj?(n.fn.particlesDraw(),n.particles.move.enable?n.fn.drawAnimFrame=requestAnimFrame(n.fn.vendors.draw):cancelRequestAnimFrame(n.fn.drawAnimFrame)):n.tmp.img_error||(n.fn.drawAnimFrame=requestAnimFrame(n.fn.vendors.draw)):(n.fn.particlesDraw(),n.particles.move.enable?n.fn.drawAnimFrame=requestAnimFrame(n.fn.vendors.draw):cancelRequestAnimFrame(n.fn.drawAnimFrame))},n.fn.vendors.checkBeforeDraw=function(){"image"==n.particles.shape.type?"svg"==n.tmp.img_type&&void 0==n.tmp.source_svg?n.tmp.checkAnimFrame=requestAnimFrame(check):(cancelRequestAnimFrame(n.tmp.checkAnimFrame),n.tmp.img_error||(n.fn.vendors.init(),n.fn.vendors.draw())):(n.fn.vendors.init(),n.fn.vendors.draw())},n.fn.vendors.init=function(){n.fn.retinaInit(),n.fn.canvasInit(),n.fn.canvasSize(),n.fn.canvasPaint(),n.fn.particlesCreate()},n.fn.vendors.start=function(){isInArray("image",n.particles.shape.type)?(n.tmp.img_type=n.particles.shape.image.src.substr(n.particles.shape.image.src.length-3),n.fn.vendors.loadImg(n.tmp.img_type)):n.fn.vendors.checkBeforeDraw(),console.log(n)},n.fn.vendors.eventsListeners(),n.fn.vendors.start()};Object.deepExtend=function(e,a){for(var t in a)a[t]&&a[t].constructor&&a[t].constructor===Object?(e[t]=e[t]||{},arguments.callee(e[t],a[t])):e[t]=a[t];return e},window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}(),window.cancelRequestAnimFrame=function(){return window.cancelAnimationFrame||window.webkitCancelRequestAnimationFrame||window.mozCancelRequestAnimationFrame||window.oCancelRequestAnimationFrame||window.msCancelRequestAnimationFrame||clearTimeout}(),window.pJSDom=[],window.particlesJS=function(e,a){"string"!=typeof e&&(a=e,e="particles-js"),e||(e="particles-js");var t=document.getElementById(e),n="particles-js-canvas-el",r=t.getElementsByClassName(n);if(r.length)for(;r.length>0;)t.removeChild(r[0]);var i=document.createElement("canvas");i.className=n,i.style.width="100%",i.style.height="100%";var s=document.getElementById(e).appendChild(i);null!=s&&pJSDom.push(new pJS(e,a))},window.particlesJS.load=function(e,a,t){var n=new XMLHttpRequest;n.open("GET",a),n.onreadystatechange=function(a){if(4==n.readyState)if(200==n.status){var r=JSON.parse(a.currentTarget.response);window.particlesJS(e,r),t&&t()}else console.log("Error pJS - XMLHttpRequest status: "+n.status),console.log("Error pJS - File config not found")},n.send()};