返回 html-ppt-skill
counter-explosion.js
根目录 / assets / animations / fx / counter-explosion.js
1 (function(){
2 window.HPX = window.HPX || {};
3 window.HPX['counter-explosion'] = function(el){
4 const U = window.HPX._u;
5 if (getComputedStyle(el).position === 'static') el.style.position = 'relative';
6 const target = parseInt(el.getAttribute('data-fx-to') || '2400', 10);
7 const k = U.canvas(el), ctx = k.ctx;
8 const pal = U.palette(el);
9 // number overlay
10 const num = document.createElement('div');
11 num.style.cssText = 'position:absolute;inset:0;display:flex;align-items:center;justify-content:center;font:900 120px system-ui,sans-serif;color:var(--text-1,#fff);pointer-events:none;text-shadow:0 4px 40px rgba(124,92,255,0.5);';
12 num.textContent = '0';
13 el.appendChild(num);
14 let parts = [];
15 let state = 'count'; // count | burst | hold
16 let stateT = 0;
17 let value = 0;
18 let cycle = 0;
19 const burst = () => {
20 const cx = k.w/2, cy = k.h/2;
21 for (let i=0;i<120;i++){
22 const a = Math.random()*Math.PI*2;
23 const s = U.rand(120, 400);
24 parts.push({x:cx,y:cy,vx:Math.cos(a)*s,vy:Math.sin(a)*s,life:1,r:U.rand(2,5),c:pal[(Math.random()*pal.length)|0]});
25 }
26 };
27 const stop = U.loop(() => {
28 ctx.clearRect(0,0,k.w,k.h);
29 const dt = 1/60;
30 stateT += dt;
31 if (state === 'count'){
32 const dur = 2.2;
33 const p = Math.min(1, stateT/dur);
34 const eased = 1 - Math.pow(1-p,3);
35 value = Math.round(target*eased);
36 num.textContent = value.toLocaleString();
37 if (p >= 1){ state='burst'; stateT=0; burst(); }
38 } else if (state === 'burst'){
39 if (stateT > 0.05 && stateT < 0.3 && parts.length < 200) {}
40 if (stateT > 2.5){ state='hold'; stateT=0; }
41 } else if (state === 'hold'){
42 if (stateT > 1.5){
43 state='count'; stateT=0; value=0; num.textContent='0'; cycle++;
44 }
45 }
46 parts = parts.filter(p => p.life > 0);
47 for (const p of parts){
48 p.vy += 260*dt; p.vx *= 0.985; p.vy *= 0.985;
49 p.x += p.vx*dt; p.y += p.vy*dt; p.life -= 0.01;
50 ctx.globalAlpha = Math.max(0,p.life);
51 ctx.fillStyle = p.c;
52 ctx.beginPath(); ctx.arc(p.x,p.y,p.r,0,Math.PI*2); ctx.fill();
53 }
54 ctx.globalAlpha = 1;
55 });
56 return { stop(){ stop(); k.destroy(); if (num.parentNode) num.parentNode.removeChild(num); } };
57 };
58 })();
59
59 lines JAVASCRIPT