1
1
import Tree from './tree' ;
2
+ import Random from './random' ;
3
+ import Glow from './glow' ;
2
4
3
5
export default class ParticleAnimation {
4
- constructor ( options ) {
6
+ constructor ( options ) {
5
7
this . running = false ;
6
8
this . parentElement = null ;
7
9
this . canvas = null ;
8
10
this . root = null ;
9
11
this . ctx = null ;
10
12
this . parentElement = document . getElementById ( options . id ) ;
11
13
12
- //User variables
14
+ // Additions
15
+ this . stars = [ ] ;
16
+ this . runningStars = false ;
17
+ this . runningCustomAnimation = false ;
18
+ this . polyTime = 0 ;
19
+ this . customAnimation = null ;
20
+
21
+ // User variables
13
22
this . usePalette = options . usePalette || false ;
14
23
this . drawLines = options . drawLines || false
15
24
this . name = options . name || "" ;
@@ -18,27 +27,35 @@ export default class ParticleAnimation {
18
27
this . color = options . color || "#0059b3" ;
19
28
this . palette = options . palette || [ "#0059b3" , "#4CE038" , "#E32023" , "#ffa500" ] ;
20
29
this . relationColor = options . relationColor || '#F5F9FA' ;
21
-
22
- if ( typeof this . parentElement === 'undefined' ) {
30
+
31
+ if ( typeof this . parentElement === 'undefined' ) {
23
32
console . log ( "Element id doesn't exist" ) ;
24
33
return ;
25
34
}
26
35
27
36
this . canvas = this . parentElement . querySelector ( 'canvas' ) ;
28
- if ( this . canvas === null ) {
29
- this . canvas = document . createElement ( 'canvas' ) ;
30
- this . parentElement . innerHTML = "" ;
37
+ if ( this . canvas === null ) {
38
+ this . canvas = document . createElement ( 'canvas' ) ;
39
+ this . parentElement . innerHTML = "" ;
31
40
this . parentElement . appendChild ( this . canvas ) ;
32
41
this . ctx = this . canvas . getContext ( '2d' ) ;
33
42
this . clear ( ) ;
34
43
this . build ( ) ;
35
44
this . applyResizeRule ( ) ;
36
- } else {
45
+ } else {
37
46
console . log ( "Canvas already exist" ) ;
38
- }
47
+ }
48
+ }
49
+ buildAndRunStars ( ) {
50
+ this . stars = [ ]
51
+ for ( var i = 0 ; i < 20 ; i ++ ) {
52
+ this . stars . push ( new Glow ( Random . range ( window . innerWidth / 2 - 80 , window . innerWidth / 2 + 80 ) , Random . range ( 0 , 10 ) ) ) ;
53
+ }
54
+ this . runningStars = true ;
55
+ this . polyTime = 300 ;
39
56
}
40
- build ( ) {
41
- this . root = Tree . build ( { levels : this . levels , maximumNodes : this . maximumNodes , color : this . color , palette : this . palette , canvasSize :{ x : this . canvas . width , y :this . canvas . height } } ) ;
57
+ build ( ) {
58
+ this . root = Tree . build ( { levels : this . levels , maximumNodes : this . maximumNodes , color : this . color , palette : this . palette , canvasSize : { x : this . canvas . width , y : this . canvas . height } } ) ;
42
59
}
43
60
clear ( ) {
44
61
this . canvas . width = this . parentElement . clientWidth ;
@@ -47,10 +64,10 @@ export default class ParticleAnimation {
47
64
}
48
65
queue ( ) {
49
66
var node = this ;
50
- if ( node . ctx === null ) {
67
+ if ( node . ctx === null ) {
51
68
return ;
52
69
}
53
- window . requestAnimationFrame ( function ( ) {
70
+ window . requestAnimationFrame ( function ( ) {
54
71
if ( node . running ) {
55
72
node . clear ( ) ;
56
73
node . update ( ) ;
@@ -64,18 +81,45 @@ export default class ParticleAnimation {
64
81
}
65
82
draw ( ) {
66
83
var ctx = this . ctx ;
67
- this . ctx . lineWidth = 0.5 ;
84
+ this . ctx . lineWidth = 0.5 ;
68
85
this . drawParticles ( this . root ) ;
86
+
87
+ if ( this . polyTime > 0 && this . polyTime < 1000 ) {
88
+ if ( this . runningStars ) {
89
+ for ( var i = 0 ; i < this . stars . length ; i ++ ) {
90
+ var g = this . stars [ i ] ;
91
+ g . speed = g . speed * g . acceleration ;
92
+ g . y += g . speed ;
93
+ if ( g . y < Random . range ( 140 , 170 ) ) {
94
+ g . x += g . speed * g . direction ;
95
+ } else if ( g . y > Random . range ( 200 , 250 ) ) {
96
+ g . x -= g . speed * g . direction ;
97
+ }
98
+ g . draw ( ctx ) ;
99
+ }
100
+ }
101
+ if ( this . runCustomAnimation && ( typeof this . customAnimation === "function" ) ) {
102
+ this . customAnimation ( ctx , this . polyTime ) ;
103
+ }
104
+ }
105
+
106
+ if ( this . polyTime > - 2 ) {
107
+ this . polyTime -- ;
108
+ } else if ( this . runningStars ) {
109
+ this . runningStars = false ;
110
+ } else if ( this . runningCustomAnimation ) {
111
+ this . stopCustomAnimation ( ) ;
112
+ }
69
113
}
70
114
drawParticles ( node ) {
71
115
var ctx = this . ctx ;
72
-
116
+
73
117
if ( this . usePallette ) {
74
118
ctx . fillStyle = node . color ;
75
- ctx . strokeStyle = node . color ;
76
- } else {
119
+ ctx . strokeStyle = node . color ;
120
+ } else {
77
121
ctx . fillStyle = node . fill ;
78
- ctx . strokeStyle = node . fill ;
122
+ ctx . strokeStyle = node . fill ;
79
123
}
80
124
81
125
ctx . beginPath ( ) ;
@@ -86,7 +130,7 @@ export default class ParticleAnimation {
86
130
87
131
for ( var i = 0 ; i < node . children . length ; i ++ ) {
88
132
var position = node . children [ i ] . position ;
89
- if ( ! node . wait && this . drawLines ) {
133
+ if ( ! node . wait && this . drawLines ) {
90
134
ctx . lineWidth = 0.33 - ( 0.01 * node . level ) ;
91
135
ctx . strokeStyle = this . relationColor ;
92
136
ctx . beginPath ( ) ;
@@ -106,26 +150,44 @@ export default class ParticleAnimation {
106
150
this . queue ( ) ;
107
151
}
108
152
}
109
- applyResizeRule ( ) {
153
+ applyResizeRule ( ) {
110
154
var node = this ;
111
- window . onresize = function ( ) {
155
+ window . onresize = function ( ) {
112
156
node . root . acceleration = 2 ;
113
- node . root . setDestination ( node . parentElement . getBoundingClientRect ( ) . width / 2 , node . parentElement . getBoundingClientRect ( ) . height / 3 )
157
+ node . root . setDestination ( node . parentElement . getBoundingClientRect ( ) . width / 2 , node . parentElement . getBoundingClientRect ( ) . height / 3 )
114
158
}
115
159
}
116
- toggleColors ( ) {
117
- this . usePallette = ! this . usePallette ;
160
+ toggleColors ( ) {
161
+ this . usePallette = ! this . usePallette ;
118
162
}
119
- toggleDrawLines ( ) {
120
- this . drawLines = ! this . drawLines ;
163
+ toggleDrawLines ( ) {
164
+ this . drawLines = ! this . drawLines ;
121
165
}
122
- stop ( ) {
123
- this . running = false ;
166
+ stop ( ) {
167
+ this . running = false ;
124
168
}
125
169
run ( ) {
126
- if ( this . running == false ) {
127
- this . running = true ;
170
+ if ( this . running == false ) {
171
+ this . running = true ;
128
172
this . queue ( ) ;
129
173
}
130
174
}
175
+ drawStars ( ) {
176
+ this . stopCustomAnimation ( ) ;
177
+ this . buildAndRunStars ( ) ;
178
+ }
179
+ runCustomAnimation ( custom , duration , callback ) {
180
+ this . polyTime = duration ;
181
+ this . runningCustomAnimation = true ;
182
+ this . customAnimation = custom ;
183
+ this . customAnimationCallback = callback ;
184
+ }
185
+ stopCustomAnimation ( ) {
186
+ this . runningCustomAnimation = false ;
187
+ this . customAnimation = null ;
188
+ if ( typeof this . customAnimationCallback === 'function' ) {
189
+ this . customAnimationCallback ( ) ;
190
+ }
191
+ this . customAnimationCallback = null ;
192
+ }
131
193
} ;
0 commit comments