| 1 | (function(){ |
| 2 | window.HPX = window.HPX || {}; |
| 3 | window.HPX['particle-burst'] = function(el){ |
| 4 | const U = window.HPX._u; |
| 5 | const k = U.canvas(el), ctx = k.ctx; |
| 6 | const pal = U.palette(el); |
| 7 | let parts = []; |
| 8 | const spawn = () => { |
| 9 | const cx = k.w/2, cy = k.h/2; |
| 10 | const n = 90; |
| 11 | for (let i=0;i<n;i++){ |
| 12 | const a = Math.random()*Math.PI*2; |
| 13 | const s = U.rand(80, 260); |
| 14 | parts.push({ |
| 15 | x: cx, y: cy, |
| 16 | vx: Math.cos(a)*s, vy: Math.sin(a)*s, |
| 17 | life: 1, r: U.rand(2,5), |
| 18 | c: pal[(Math.random()*pal.length)|0] |
| 19 | }); |
| 20 | } |
| 21 | }; |
| 22 | spawn(); |
| 23 | let lastSpawn = 0; |
| 24 | const stop = U.loop((t) => { |
| 25 | ctx.clearRect(0,0,k.w,k.h); |
| 26 | if (t - lastSpawn > 2.5) { spawn(); lastSpawn = t; } |
| 27 | const dt = 1/60; |
| 28 | parts = parts.filter(p => p.life > 0); |
| 29 | for (const p of parts){ |
| 30 | p.vy += 220*dt; |
| 31 | p.vx *= 0.985; p.vy *= 0.985; |
| 32 | p.x += p.vx*dt; p.y += p.vy*dt; |
| 33 | p.life -= 0.012; |
| 34 | ctx.globalAlpha = Math.max(0, p.life); |
| 35 | ctx.fillStyle = p.c; |
| 36 | ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI*2); ctx.fill(); |
| 37 | } |
| 38 | ctx.globalAlpha = 1; |
| 39 | }); |
| 40 | return { stop(){ stop(); k.destroy(); } }; |
| 41 | }; |
| 42 | })(); |
| 43 |