|
| 1 | +let currentPhase = 0; // current displayed phase |
| 2 | +let targetPhase = 0; // target phase you want to reach |
| 3 | + |
1 | 4 | function loadingImg(phase) { |
2 | | - const img = document.querySelector('.logo-img'); |
3 | | - if (!img) return; |
| 5 | + targetPhase = Math.max(0, Math.min(phase, 100)); |
| 6 | + animatePhase(); |
| 7 | +} |
| 8 | + |
| 9 | +let animating = false; |
| 10 | + |
| 11 | +function animatePhase() { |
| 12 | + if (animating) return; // prevent multiple loops |
| 13 | + animating = true; |
| 14 | + |
| 15 | + function step() { |
| 16 | + const img = document.querySelector('.logo-img'); |
| 17 | + if (!img) { |
| 18 | + animating = false; |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // smooth approach using linear interpolation |
| 23 | + currentPhase += (targetPhase - currentPhase) * 0.05; // 0.05 = smoothing factor |
| 24 | + if (Math.abs(currentPhase - targetPhase) < 0.1) currentPhase = targetPhase; |
| 25 | + |
| 26 | + const degrees = (currentPhase / 100) * 360; |
| 27 | + |
| 28 | + img.style.webkitMaskImage = `conic-gradient(rgba(255,255,255,1) 0deg ${degrees}deg, rgba(255,255,255,0.7) ${degrees}deg 360deg)`; |
| 29 | + img.style.maskImage = `conic-gradient(rgba(255,255,255,1) 0deg ${degrees}deg, rgba(255,255,255,0.7) ${degrees}deg 360deg)`; |
| 30 | + |
| 31 | + img.style.webkitMaskRepeat = 'no-repeat'; |
| 32 | + img.style.maskRepeat = 'no-repeat'; |
| 33 | + img.style.webkitMaskPosition = 'center'; |
| 34 | + img.style.maskPosition = 'center'; |
| 35 | + img.style.webkitMaskSize = 'contain'; |
| 36 | + img.style.maskSize = 'contain'; |
| 37 | + |
| 38 | + if (currentPhase !== targetPhase) { |
| 39 | + requestAnimationFrame(step); |
| 40 | + } else { |
| 41 | + animating = false; |
| 42 | + } |
| 43 | + } |
4 | 44 |
|
5 | | - phase = Math.max(0, Math.min(phase, 100)); |
6 | | - const degrees = (phase / 100) * 360; |
7 | | - |
8 | | - img.style.webkitMaskImage = `conic-gradient(rgba(255,255,255,1) 0deg ${degrees}deg, rgba(255,255,255,0.7) ${degrees}deg 360deg)`; |
9 | | - img.style.maskImage = `conic-gradient(rgba(255,255,255,1) 0deg ${degrees}deg, rgba(255,255,255,0.7) ${degrees}deg 360deg)`; |
10 | | - img.style.webkitMaskRepeat = 'no-repeat'; |
11 | | - img.style.maskRepeat = 'no-repeat'; |
12 | | - img.style.webkitMaskPosition = 'center'; |
13 | | - img.style.maskPosition = 'center'; |
14 | | - img.style.webkitMaskSize = 'contain'; |
15 | | - img.style.maskSize = 'contain'; |
| 45 | + requestAnimationFrame(step); |
16 | 46 | } |
17 | 47 |
|
18 | 48 | let pulseState = null; |
|
0 commit comments