@@ -97,46 +97,60 @@ Object.defineProperty(exports, "__esModule", { value: true });
97
97
const graphics_1 = __webpack_require__ ( 2 ) ;
98
98
const things_1 = __webpack_require__ ( 3 ) ;
99
99
class Fireworks {
100
- constructor ( container , { rocketSpawnInterval = 150 , maxRockets = 3 , numParticles = 100 , explosionHeight = 0.2 , explosionChance = 0.08 } = { } ) {
100
+ constructor ( container , { rocketSpawnInterval = 150 , maxRockets = 3 , numParticles = 100 , explosionMinHeight = 0.2 , explosionMaxHeight = 0.9 , explosionChance = 0.08 } = { } ) {
101
101
this . rocketSpawnInterval = rocketSpawnInterval ;
102
102
this . maxRockets = maxRockets ;
103
- this . numParticles = numParticles ;
104
- this . explosionHeight = explosionHeight ;
105
- this . explosionChance = explosionChance ;
106
103
this . cw = container . clientWidth ;
107
104
this . ch = container . clientHeight ;
108
105
this . graphics = new graphics_1 . default ( container ) ;
109
- this . things = new things_1 . default ( { maxRockets : this . maxRockets , cw : this . cw , ch : this . ch } ) ;
106
+ this . things = new things_1 . default ( {
107
+ maxRockets : this . maxRockets ,
108
+ numParticles,
109
+ explosionMinHeight,
110
+ explosionMaxHeight,
111
+ explosionChance,
112
+ cw : this . cw ,
113
+ ch : this . ch
114
+ } ) ;
110
115
container . appendChild ( this . graphics . canvas ) ;
116
+ console . log ( this . ch * ( 1 - explosionMaxHeight ) ) ;
111
117
}
112
118
start ( ) {
113
- window . requestAnimationFrame ( ( ) => this . update ( ) ) ;
114
- this . interval = setInterval ( ( ) => this . things . spawnRockets ( ) , this . rocketSpawnInterval ) ;
119
+ if ( this . maxRockets > 0 ) {
120
+ this . interval = setInterval ( ( ) => this . things . spawnRockets ( ) , this . rocketSpawnInterval ) ;
121
+ this . rafInterval = window . requestAnimationFrame ( ( ) => this . update ( ) ) ;
122
+ }
115
123
return ( ) => this . stop ( ) ;
116
124
}
117
125
stop ( ) {
118
126
window . clearInterval ( this . interval ) ;
119
127
this . interval = null ;
120
128
}
129
+ fire ( ) {
130
+ this . things . spawnRocket ( ) ;
131
+ if ( ! this . rafInterval ) {
132
+ this . rafInterval = window . requestAnimationFrame ( ( ) => this . update ( ) ) ;
133
+ }
134
+ }
121
135
update ( ) {
122
136
this . graphics . clear ( ) ;
123
137
let x = null ;
124
138
for ( const particle of this . things ) {
125
- if ( ! this . graphics . drawParticle ( particle ) ) {
126
- this . things . delete ( particle ) ;
127
- continue ;
128
- }
139
+ this . graphics . drawParticle ( particle ) ;
129
140
particle . update ( ) ;
130
- if ( particle . shouldExplode ( this . ch , this . explosionHeight , this . explosionChance ) ) {
131
- particle . explode ( this . numParticles ) . forEach ( this . things . add , this . things ) ;
141
+ if ( this . things . shouldRemove ( particle ) ) {
132
142
this . things . delete ( particle ) ;
133
143
}
144
+ else if ( this . things . shouldExplode ( particle ) ) {
145
+ this . things . explode ( particle ) ;
146
+ }
134
147
}
135
148
if ( this . interval || this . things . size > 0 ) {
136
- window . requestAnimationFrame ( ( ) => this . update ( ) ) ;
149
+ this . rafInterval = window . requestAnimationFrame ( ( ) => this . update ( ) ) ;
137
150
}
138
151
else {
139
152
this . graphics . clear ( true ) ;
153
+ this . rafInterval = null ;
140
154
}
141
155
}
142
156
}
@@ -179,16 +193,6 @@ class Graphics {
179
193
this . ctx . lineWidth = particle . size ;
180
194
this . ctx . strokeStyle = `hsla(${ particle . hue } , 100%, ${ particle . brightness } %, ${ particle . alpha } )` ;
181
195
this . ctx . stroke ( ) ;
182
- if ( particle . alpha <= 0.1 || particle . size <= 1 ) {
183
- return false ;
184
- }
185
- if ( particle . position . x > this . cw || particle . position . x < 0 ) {
186
- return false ;
187
- }
188
- if ( particle . position . y > this . ch || particle . position . y < 0 ) {
189
- return false ;
190
- }
191
- return true ;
192
196
}
193
197
}
194
198
exports . default = Graphics ;
@@ -204,23 +208,67 @@ Object.defineProperty(exports, "__esModule", { value: true });
204
208
const particle_1 = __webpack_require__ ( 4 ) ;
205
209
const util_1 = __webpack_require__ ( 0 ) ;
206
210
class Things extends Set {
207
- constructor ( { maxRockets, cw, ch } ) {
211
+ constructor ( { maxRockets, numParticles , explosionMinHeight , explosionMaxHeight , explosionChance , cw, ch } ) {
208
212
super ( ) ;
209
213
this . maxRockets = maxRockets ;
214
+ this . numParticles = numParticles ;
215
+ this . explosionMaxHeight = explosionMaxHeight ,
216
+ this . explosionMinHeight = explosionMinHeight ,
217
+ this . explosionChance = explosionChance ;
210
218
this . cw = cw ;
211
219
this . ch = ch ;
212
220
}
213
- spawnRockets ( ) {
214
- const rockets = this . rockets ;
215
- if ( rockets < this . maxRockets ) {
221
+ shouldRemove ( particle ) {
222
+ if ( particle . alpha <= 0.1 || particle . size <= 1 ) {
223
+ return true ;
224
+ }
225
+ if ( particle . position . x > this . cw || particle . position . x < 0 ) {
226
+ return true ;
227
+ }
228
+ if ( particle . position . y > this . ch || particle . position . y < 0 ) {
229
+ return true ;
230
+ }
231
+ return false ;
232
+ }
233
+ shouldExplode ( particle ) {
234
+ if ( ! particle . isRocket ) {
235
+ return false ;
236
+ }
237
+ if ( particle . position . y <= this . ch * ( 1 - this . explosionMaxHeight ) ) {
238
+ return true ;
239
+ }
240
+ if ( particle . position . y >= this . ch * ( 1 - this . explosionMinHeight ) ) {
241
+ return false ;
242
+ }
243
+ return util_1 . random ( 0 , 1 ) <= this . explosionChance ;
244
+ }
245
+ explode ( particle ) {
246
+ for ( let i = 0 ; i < this . numParticles ; i += 1 ) {
216
247
this . add ( new particle_1 . default ( {
217
- isRocket : true ,
218
248
position : {
219
- x : util_1 . random ( 0 , this . cw ) ,
220
- y : this . ch
221
- }
249
+ x : particle . position . x ,
250
+ y : particle . position . y
251
+ } ,
252
+ hue : particle . hue ,
253
+ brightness : particle . brightness
222
254
} ) ) ;
223
255
}
256
+ this . delete ( particle ) ;
257
+ }
258
+ spawnRocket ( ) {
259
+ this . add ( new particle_1 . default ( {
260
+ isRocket : true ,
261
+ position : {
262
+ x : util_1 . random ( 0 , this . cw ) ,
263
+ y : this . ch
264
+ }
265
+ } ) ) ;
266
+ }
267
+ spawnRockets ( ) {
268
+ const rockets = this . rockets ;
269
+ if ( rockets < this . maxRockets ) {
270
+ this . spawnRocket ( ) ;
271
+ }
224
272
}
225
273
get rockets ( ) {
226
274
return [ ...this ] . filter ( x => x . isRocket ) . length ;
@@ -246,11 +294,10 @@ class Particle {
246
294
this . position ,
247
295
this . position
248
296
] ;
249
- this . velocity = { x : null , y : null } ;
250
297
if ( this . isRocket ) {
251
298
this . velocity = {
252
- x : util_1 . random ( 0 , 6 ) - 3 ,
253
- y : util_1 . random ( - 3 , 0 ) - 4
299
+ x : util_1 . random ( - 3 , 3 ) ,
300
+ y : util_1 . random ( - 7 , - 3 )
254
301
} ;
255
302
this . shrink = 0.999 ;
256
303
this . resistance = 1 ;
@@ -272,31 +319,6 @@ class Particle {
272
319
this . hue = hue ;
273
320
this . brightness = brightness ;
274
321
}
275
- shouldExplode ( ch , explosionHeight , explosionChance ) {
276
- if ( ! this . isRocket ) {
277
- return false ;
278
- }
279
- const inRange = this . position . y <= ch * ( 1 - explosionHeight ) ;
280
- if ( ! inRange ) {
281
- return false ;
282
- }
283
- const shouldExplode = util_1 . random ( 0 , 1 ) <= explosionChance ;
284
- return shouldExplode ;
285
- }
286
- explode ( count ) {
287
- const newParticles = new Set ( ) ;
288
- for ( let i = 0 ; i < count ; i += 1 ) {
289
- newParticles . add ( new Particle ( {
290
- position : {
291
- x : this . position . x ,
292
- y : this . position . y
293
- } ,
294
- hue : this . hue ,
295
- brightness : this . brightness
296
- } ) ) ;
297
- }
298
- return newParticles ;
299
- }
300
322
update ( ) {
301
323
this . positions . pop ( ) ;
302
324
this . positions . unshift ( { x : this . position . x , y : this . position . y } ) ;
0 commit comments