feat(StarryBackground): 为星星添加闪烁效果并减少默认数量
为星星背景添加动态闪烁效果,通过正弦波函数叠加变化透明度。同时将默认星星数量从800减少到500以优化性能
Showing
1 changed file
with
8 additions
and
2 deletions
| ... | @@ -28,7 +28,7 @@ const props = defineProps({ | ... | @@ -28,7 +28,7 @@ const props = defineProps({ |
| 28 | // 星星数量 | 28 | // 星星数量 |
| 29 | starCount: { | 29 | starCount: { |
| 30 | type: Number, | 30 | type: Number, |
| 31 | - default: isWxMobile ? 800 : 800 | 31 | + default: isWxMobile ? 500 : 500 |
| 32 | }, | 32 | }, |
| 33 | // 星星颜色 (RGBA 前缀,例如 '255,255,255') | 33 | // 星星颜色 (RGBA 前缀,例如 '255,255,255') |
| 34 | starColor: { | 34 | starColor: { |
| ... | @@ -169,8 +169,14 @@ const render = () => { | ... | @@ -169,8 +169,14 @@ const render = () => { |
| 169 | 169 | ||
| 170 | // 绘制星星 | 170 | // 绘制星星 |
| 171 | context.beginPath(); | 171 | context.beginPath(); |
| 172 | + | ||
| 173 | + // 叠加闪烁效果:使用正弦波在基础 alpha 上叠加快速变化 | ||
| 174 | + // Date.now() 产生时间变化,i 作为相位偏移,* 0.005 控制闪烁频率 | ||
| 175 | + const twinkle = Math.abs(Math.sin(Date.now() * 0.006 + i)); | ||
| 176 | + const finalAlpha = Math.min(1, Math.max(0, star.alpha * (0.5 + 0.5 * twinkle))); | ||
| 177 | + | ||
| 172 | const bg = context.createRadialGradient(star.x, star.y, 0, star.x, star.y, star.r); | 178 | const bg = context.createRadialGradient(star.x, star.y, 0, star.x, star.y, star.r); |
| 173 | - bg.addColorStop(0, `rgba(${props.starColor}, ${star.alpha})`); | 179 | + bg.addColorStop(0, `rgba(${props.starColor}, ${finalAlpha})`); |
| 174 | bg.addColorStop(1, `rgba(${props.starColor}, 0)`); | 180 | bg.addColorStop(1, `rgba(${props.starColor}, 0)`); |
| 175 | context.fillStyle = bg; | 181 | context.fillStyle = bg; |
| 176 | context.arc(star.x, star.y, star.r, 0, Math.PI * 2, true); | 182 | context.arc(star.x, star.y, star.r, 0, Math.PI * 2, true); | ... | ... |
-
Please register or login to post a comment