|
| 1 | +<script setup lang="ts"> |
| 2 | +interface Star { |
| 3 | + x: number |
| 4 | + y: number |
| 5 | + size: number |
| 6 | +} |
| 7 | +
|
| 8 | +const props = withDefaults(defineProps<{ |
| 9 | + starCount?: number |
| 10 | + color?: string |
| 11 | + speed?: 'slow' | 'normal' | 'fast' |
| 12 | + size?: { min: number, max: number } |
| 13 | +}>(), { |
| 14 | + starCount: 300, |
| 15 | + color: 'var(--ui-primary)', |
| 16 | + speed: 'normal', |
| 17 | + size: () => ({ |
| 18 | + min: 1, |
| 19 | + max: 2, |
| 20 | + }), |
| 21 | +}) |
| 22 | +
|
| 23 | +// Generate random star positions and sizes |
| 24 | +function generateStars(count: number): Star[] { |
| 25 | + return Array.from({ length: count }, () => ({ |
| 26 | + x: Math.floor(Math.random() * 2000), |
| 27 | + y: Math.floor(Math.random() * 2000), |
| 28 | + size: typeof props.size === 'number' |
| 29 | + ? props.size |
| 30 | + : Math.random() * (props.size.max - props.size.min) + props.size.min, |
| 31 | + })) |
| 32 | +} |
| 33 | +
|
| 34 | +// Define speed configurations once |
| 35 | +const speedMap = { |
| 36 | + slow: { duration: 200, opacity: 0.5, ratio: 0.3 }, |
| 37 | + normal: { duration: 150, opacity: 0.75, ratio: 0.3 }, |
| 38 | + fast: { duration: 100, opacity: 1, ratio: 0.4 }, |
| 39 | +} |
| 40 | +
|
| 41 | +// Use a more efficient approach to generate and store stars |
| 42 | +const stars = ref<{ slow: Star[], normal: Star[], fast: Star[] }>({ |
| 43 | + slow: generateStars(Math.floor(props.starCount * speedMap.slow.ratio)), |
| 44 | + normal: generateStars(Math.floor(props.starCount * speedMap.normal.ratio)), |
| 45 | + fast: generateStars(Math.floor(props.starCount * speedMap.fast.ratio)), |
| 46 | +}) |
| 47 | +
|
| 48 | +// Compute star layers with different speeds and opacities |
| 49 | +const starLayers = computed(() => [ |
| 50 | + { stars: stars.value.fast, ...speedMap.fast }, |
| 51 | + { stars: stars.value.normal, ...speedMap.normal }, |
| 52 | + { stars: stars.value.slow, ...speedMap.slow }, |
| 53 | +]) |
| 54 | +</script> |
| 55 | + |
| 56 | +<template> |
| 57 | + <div class="absolute pointer-events-none z-[-1] inset-y-0 inset-x-5 sm:inset-x-7 lg:inset-x-9 overflow-hidden"> |
| 58 | + <div class="stars size-full absolute inset-x-0 top-0"> |
| 59 | + <div |
| 60 | + v-for="(layer, index) in starLayers" |
| 61 | + :key="index" |
| 62 | + class="star-layer" |
| 63 | + :style="{ |
| 64 | + '--star-duration': `${layer.duration}s`, |
| 65 | + '--star-opacity': layer.opacity, |
| 66 | + '--star-color': color, |
| 67 | + }" |
| 68 | + > |
| 69 | + <div |
| 70 | + v-for="(star, starIndex) in layer.stars" |
| 71 | + :key="starIndex" |
| 72 | + class="star absolute rounded-full" |
| 73 | + :style="{ |
| 74 | + left: `${star.x}px`, |
| 75 | + top: `${star.y}px`, |
| 76 | + width: `${star.size}px`, |
| 77 | + height: `${star.size}px`, |
| 78 | + backgroundColor: 'var(--star-color)', |
| 79 | + opacity: 'var(--star-opacity)', |
| 80 | + }" |
| 81 | + /> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | +</template> |
| 86 | + |
| 87 | +<style scoped> |
| 88 | +.stars { |
| 89 | + left: 50%; |
| 90 | + transform: translate(-50%); |
| 91 | + -webkit-mask-image: linear-gradient(180deg, |
| 92 | + rgba(217, 217, 217, 0) 0%, |
| 93 | + rgba(217, 217, 217, 0.8) 25%, |
| 94 | + #d9d9d9 50%, |
| 95 | + rgba(217, 217, 217, 0.8) 75%, |
| 96 | + rgba(217, 217, 217, 0) 100%); |
| 97 | + mask-image: linear-gradient(180deg, |
| 98 | + rgba(217, 217, 217, 0) 0%, |
| 99 | + rgba(217, 217, 217, 0.8) 25%, |
| 100 | + #d9d9d9 50%, |
| 101 | + rgba(217, 217, 217, 0.8) 75%, |
| 102 | + rgba(217, 217, 217, 0) 100%); |
| 103 | + -webkit-mask-size: cover; |
| 104 | + mask-size: cover; |
| 105 | +} |
| 106 | +
|
| 107 | +.star-layer { |
| 108 | + animation: risingStarsAnimation linear infinite; |
| 109 | + animation-duration: var(--star-duration); |
| 110 | + will-change: transform; |
| 111 | +} |
| 112 | +
|
| 113 | +@keyframes risingStarsAnimation { |
| 114 | + 0% { |
| 115 | + transform: translateY(0); |
| 116 | + } |
| 117 | +
|
| 118 | + 100% { |
| 119 | + transform: translateY(-2000px); |
| 120 | + } |
| 121 | +} |
| 122 | +</style> |
0 commit comments