1+ ! function ( a ) {
2+ var b = "object" == typeof self && self . self === self && self || "object" == typeof global && global . global === global && global ;
3+ "function" == typeof define && define . amd ? define ( [ "exports" ] , function ( c ) {
4+ b . ParticleNetwork = a ( b , c )
5+ } ) : "object" == typeof module && module . exports ? module . exports = a ( b , { } ) : b . ParticleNetwork = a ( b , { } )
6+ } ( function ( a , b ) {
7+ var c = function ( a ) {
8+ this . canvas = a . canvas , this . g = a . g , this . particleColor = a . options . particleColor , this . x = Math . random ( ) * this . canvas . width , this . y = Math . random ( ) * this . canvas . height , this . velocity = {
9+ x : ( Math . random ( ) - .5 ) * a . options . velocity ,
10+ y : ( Math . random ( ) - .5 ) * a . options . velocity
11+ }
12+ } ;
13+ return c . prototype . update = function ( ) {
14+ ( this . x > this . canvas . width + 20 || this . x < - 20 ) && ( this . velocity . x = - this . velocity . x ) , ( this . y > this . canvas . height + 20 || this . y < - 20 ) && ( this . velocity . y = - this . velocity . y ) , this . x += this . velocity . x , this . y += this . velocity . y
15+ } , c . prototype . h = function ( ) {
16+ this . g . beginPath ( ) , this . g . fillStyle = this . particleColor , this . g . globalAlpha = .7 , this . g . arc ( this . x , this . y , 1.5 , 0 , 2 * Math . PI ) , this . g . fill ( )
17+ } , b = function ( a , b ) {
18+ this . i = a , this . i . size = {
19+ width : this . i . offsetWidth ,
20+ height : this . i . offsetHeight
21+ } , b = void 0 !== b ? b : { } , this . options = {
22+ particleColor : void 0 !== b . particleColor ? b . particleColor : "#fff" ,
23+ background : void 0 !== b . background ? b . background : "#1a252f" ,
24+ interactive : void 0 !== b . interactive ? b . interactive : ! 0 ,
25+ velocity : this . setVelocity ( b . speed ) ,
26+ density : this . j ( b . density )
27+ } , this . init ( )
28+ } , b . prototype . init = function ( ) {
29+ if ( this . k = document . createElement ( "div" ) , this . i . appendChild ( this . k ) , this . l ( this . k , {
30+ position : "absolute" ,
31+ top : 0 ,
32+ left : 0 ,
33+ bottom : 0 ,
34+ right : 0 ,
35+ "z-index" : 1
36+ } ) , / ( ^ # [ 0 - 9 A - F ] { 6 } $ ) | ( ^ # [ 0 - 9 A - F ] { 3 } $ ) / i. test ( this . options . background ) ) this . l ( this . k , {
37+ background : this . options . background
38+ } ) ;
39+ else {
40+ if ( ! / \. ( g i f | j p g | j p e g | t i f f | p n g ) $ / i. test ( this . options . background ) ) return console . error ( "Please specify a valid background image or hexadecimal color" ) , ! 1 ;
41+ this . l ( this . k , {
42+ background : 'url("' + this . options . background + '") no-repeat center' ,
43+ "background-size" : "cover"
44+ } )
45+ }
46+ if ( ! / ( ^ # [ 0 - 9 A - F ] { 6 } $ ) | ( ^ # [ 0 - 9 A - F ] { 3 } $ ) / i. test ( this . options . particleColor ) ) return console . error ( "Please specify a valid particleColor hexadecimal color" ) , ! 1 ;
47+ this . canvas = document . createElement ( "canvas" ) , this . i . appendChild ( this . canvas ) , this . g = this . canvas . getContext ( "2d" ) , this . canvas . width = this . i . size . width , this . canvas . height = this . i . size . height , this . l ( this . i , {
48+ position : "relative"
49+ } ) , this . l ( this . canvas , {
50+ "z-index" : "20" ,
51+ position : "relative"
52+ } ) , window . addEventListener ( "resize" , function ( ) {
53+ return this . i . offsetWidth === this . i . size . width && this . i . offsetHeight === this . i . size . height ? ! 1 : ( this . canvas . width = this . i . size . width = this . i . offsetWidth , this . canvas . height = this . i . size . height = this . i . offsetHeight , clearTimeout ( this . m ) , void ( this . m = setTimeout ( function ( ) {
54+ this . o = [ ] ;
55+ for ( var a = 0 ; a < this . canvas . width * this . canvas . height / this . options . density ; a ++ ) this . o . push ( new c ( this ) ) ;
56+ this . options . interactive && this . o . push ( this . p ) , requestAnimationFrame ( this . update . bind ( this ) )
57+ } . bind ( this ) , 500 ) ) )
58+ } . bind ( this ) ) , this . o = [ ] ;
59+ for ( var a = 0 ; a < this . canvas . width * this . canvas . height / this . options . density ; a ++ ) this . o . push ( new c ( this ) ) ;
60+ this . options . interactive && ( this . p = new c ( this ) , this . p . velocity = {
61+ x : 0 ,
62+ y : 0
63+ } , this . o . push ( this . p ) , this . canvas . addEventListener ( "mousemove" , function ( a ) {
64+ this . p . x = a . clientX - this . canvas . offsetLeft , this . p . y = a . clientY - this . canvas . offsetTop
65+ } . bind ( this ) ) , this . canvas . addEventListener ( "mouseup" , function ( a ) {
66+ this . p . velocity = {
67+ x : ( Math . random ( ) - .5 ) * this . options . velocity ,
68+ y : ( Math . random ( ) - .5 ) * this . options . velocity
69+ } , this . p = new c ( this ) , this . p . velocity = {
70+ x : 0 ,
71+ y : 0
72+ } , this . o . push ( this . p )
73+ } . bind ( this ) ) ) , requestAnimationFrame ( this . update . bind ( this ) )
74+ } , b . prototype . update = function ( ) {
75+ this . g . clearRect ( 0 , 0 , this . canvas . width , this . canvas . height ) , this . g . globalAlpha = 1 ;
76+ for ( var a = 0 ; a < this . o . length ; a ++ ) {
77+ this . o [ a ] . update ( ) , this . o [ a ] . h ( ) ;
78+ for ( var b = this . o . length - 1 ; b > a ; b -- ) {
79+ var c = Math . sqrt ( Math . pow ( this . o [ a ] . x - this . o [ b ] . x , 2 ) + Math . pow ( this . o [ a ] . y - this . o [ b ] . y , 2 ) ) ;
80+ c > 120 || ( this . g . beginPath ( ) , this . g . strokeStyle = this . options . particleColor , this . g . globalAlpha = ( 120 - c ) / 120 , this . g . lineWidth = .7 , this . g . moveTo ( this . o [ a ] . x , this . o [ a ] . y ) , this . g . lineTo ( this . o [ b ] . x , this . o [ b ] . y ) , this . g . stroke ( ) )
81+ }
82+ }
83+ 0 !== this . options . velocity && requestAnimationFrame ( this . update . bind ( this ) )
84+ } , b . prototype . setVelocity = function ( a ) {
85+ return "fast" === a ? 1 : "slow" === a ? .33 : "none" === a ? 0 : .66
86+ } , b . prototype . j = function ( a ) {
87+ return "high" === a ? 5e3 : "low" === a ? 2e4 : isNaN ( parseInt ( a , 10 ) ) ? 1e4 : a
88+ } , b . prototype . l = function ( a , b ) {
89+ for ( var c in b ) a . style [ c ] = b [ c ]
90+ } , b
91+ } ) ;
92+
93+ // Initialisation
94+
95+ var canvasDiv = document . getElementById ( 'particle-canvas' ) ;
96+ var options = {
97+ particleColor : '#000' ,
98+ background : '#fff' ,
99+ interactive : true ,
100+ speed : 'medium' ,
101+ density : 'high'
102+ } ;
103+ var particleCanvas = new ParticleNetwork ( canvasDiv , options ) ;
0 commit comments