@@ -21,12 +21,14 @@ const Fireworks = () => {
21
21
'https://cdn.bootcdn.net/ajax/libs/animejs/3.2.1/anime.min.js' ,
22
22
'js'
23
23
) . then ( ( ) => {
24
- if ( window . anime ) {
25
- createFireworks ( {
26
- config : { colors : fireworksColor } ,
27
- anime : window . anime
28
- } )
29
- }
24
+ loadExternalResource ( '/js/fireworks.js' , 'js' ) . then ( ( ) => {
25
+ if ( window . anime && window . createFireworks ) {
26
+ window . createFireworks ( {
27
+ config : { colors : fireworksColor } ,
28
+ anime : window . anime
29
+ } )
30
+ }
31
+ } )
30
32
} )
31
33
}
32
34
@@ -37,204 +39,7 @@ const Fireworks = () => {
37
39
}
38
40
} , [ ] )
39
41
40
- return < canvas id = 'fireworks' className = 'fireworks' > </ canvas >
42
+ return < > </ >
41
43
}
42
- export default Fireworks
43
-
44
- /**
45
- * 创建烟花
46
- * @param config
47
- */
48
- function createFireworks ( { config, anime } ) {
49
- const defaultConfig = {
50
- colors : config ?. colors ,
51
- numberOfParticules : 20 ,
52
- orbitRadius : {
53
- min : 50 ,
54
- max : 100
55
- } ,
56
- circleRadius : {
57
- min : 10 ,
58
- max : 20
59
- } ,
60
- diffuseRadius : {
61
- min : 50 ,
62
- max : 100
63
- } ,
64
- animeDuration : {
65
- min : 900 ,
66
- max : 1500
67
- }
68
- }
69
- config = Object . assign ( defaultConfig , config )
70
-
71
- let pointerX = 0
72
- let pointerY = 0
73
-
74
- // sky blue
75
- const colors = config . colors
76
-
77
- const canvasEl = document . querySelector ( '.fireworks' )
78
- const ctx = canvasEl . getContext ( '2d' )
79
-
80
- /**
81
- * 设置画布尺寸
82
- */
83
- function setCanvasSize ( canvasEl ) {
84
- canvasEl . width = window . innerWidth
85
- canvasEl . height = window . innerHeight
86
- canvasEl . style . width = `${ window . innerWidth } px`
87
- canvasEl . style . height = `${ window . innerHeight } px`
88
- }
89
-
90
- /**
91
- * update pointer
92
- * @param {TouchEvent } e
93
- */
94
- function updateCoords ( e ) {
95
- pointerX =
96
- e . clientX ||
97
- ( e . touches [ 0 ] ? e . touches [ 0 ] . clientX : e . changedTouches [ 0 ] . clientX )
98
- pointerY =
99
- e . clientY ||
100
- ( e . touches [ 0 ] ? e . touches [ 0 ] . clientY : e . changedTouches [ 0 ] . clientY )
101
- }
102
-
103
- function setParticuleDirection ( p ) {
104
- const angle = ( anime . random ( 0 , 360 ) * Math . PI ) / 180
105
- const value = anime . random (
106
- config . diffuseRadius . min ,
107
- config . diffuseRadius . max
108
- )
109
- const radius = [ - 1 , 1 ] [ anime . random ( 0 , 1 ) ] * value
110
- return {
111
- x : p . x + radius * Math . cos ( angle ) ,
112
- y : p . y + radius * Math . sin ( angle )
113
- }
114
- }
115
-
116
- /**
117
- * 在指定位置创建粒子
118
- * @param {number } x
119
- * @param {number } y
120
- * @returns
121
- */
122
- function createParticule ( x , y ) {
123
- const p = {
124
- x,
125
- y,
126
- color : `rgba(${ colors [ anime . random ( 0 , colors . length - 1 ) ] } ,${ anime . random (
127
- 0.2 ,
128
- 0.8
129
- ) } )`,
130
- radius : anime . random ( config . circleRadius . min , config . circleRadius . max ) ,
131
- endPos : null ,
132
- draw ( ) { }
133
- }
134
- p . endPos = setParticuleDirection ( p )
135
- p . draw = function ( ) {
136
- ctx . beginPath ( )
137
- ctx . arc ( p . x , p . y , p . radius , 0 , 2 * Math . PI , true )
138
- ctx . fillStyle = p . color
139
- ctx . fill ( )
140
- }
141
- return p
142
- }
143
44
144
- function createCircle ( x , y ) {
145
- const p = {
146
- x,
147
- y,
148
- color : '#000' ,
149
- radius : 0.1 ,
150
- alpha : 0.5 ,
151
- lineWidth : 6 ,
152
- draw ( ) { }
153
- }
154
- p . draw = function ( ) {
155
- ctx . globalAlpha = p . alpha
156
- ctx . beginPath ( )
157
- ctx . arc ( p . x , p . y , p . radius , 0 , 2 * Math . PI , true )
158
- ctx . lineWidth = p . lineWidth
159
- ctx . strokeStyle = p . color
160
- ctx . stroke ( )
161
- ctx . globalAlpha = 1
162
- }
163
- return p
164
- }
165
-
166
- function renderParticule ( anim ) {
167
- for ( let i = 0 ; i < anim . animatables . length ; i ++ ) {
168
- anim . animatables [ i ] . target . draw ( )
169
- }
170
- }
171
-
172
- function animateParticules ( x , y ) {
173
- const circle = createCircle ( x , y )
174
- const particules = [ ]
175
- for ( let i = 0 ; i < config . numberOfParticules ; i ++ ) {
176
- particules . push ( createParticule ( x , y ) )
177
- }
178
-
179
- anime
180
- . timeline ( )
181
- . add ( {
182
- targets : particules ,
183
- x ( p ) {
184
- return p . endPos . x
185
- } ,
186
- y ( p ) {
187
- return p . endPos . y
188
- } ,
189
- radius : 0.1 ,
190
- duration : anime . random (
191
- config . animeDuration . min ,
192
- config . animeDuration . max
193
- ) ,
194
- easing : 'easeOutExpo' ,
195
- update : renderParticule
196
- } )
197
- . add (
198
- {
199
- targets : circle ,
200
- radius : anime . random ( config . orbitRadius . min , config . orbitRadius . max ) ,
201
- lineWidth : 0 ,
202
- alpha : {
203
- value : 0 ,
204
- easing : 'linear' ,
205
- duration : anime . random ( 600 , 800 )
206
- } ,
207
- duration : anime . random ( 1200 , 1800 ) ,
208
- easing : 'easeOutExpo' ,
209
- update : renderParticule
210
- } ,
211
- 0
212
- )
213
- }
214
-
215
- const render = anime ( {
216
- duration : Infinity ,
217
- update : ( ) => {
218
- ctx . clearRect ( 0 , 0 , canvasEl . width , canvasEl . height )
219
- }
220
- } )
221
-
222
- document . addEventListener (
223
- 'mousedown' ,
224
- e => {
225
- render . play ( )
226
- updateCoords ( e )
227
- animateParticules ( pointerX , pointerY )
228
- } ,
229
- false
230
- )
231
-
232
- setCanvasSize ( canvasEl )
233
- window . addEventListener (
234
- 'resize' ,
235
- ( ) => {
236
- setCanvasSize ( canvasEl )
237
- } ,
238
- false
239
- )
240
- }
45
+ export default Fireworks
0 commit comments