Skip to content

Instantly share code, notes, and snippets.

@AshMartian
Created July 12, 2023 18:54
Show Gist options
  • Save AshMartian/cb39422e8691619713e6b6ec6edd8684 to your computer and use it in GitHub Desktop.
Save AshMartian/cb39422e8691619713e6b6ec6edd8684 to your computer and use it in GitHub Desktop.
Randomized Star Generator
function addStar() {
var s = document.createElement('div')
s.className = Math.random() > 0.5 ? 'star' : 'star2';
var color = Math.random();
s.style.backgroundColor = color > 0.6 ? '#08c6dc' : color > 0.3 ? '#fd98ab' : 'white';
s.style.setProperty('--size', Math.random()*5 + 1 + 'vmin')
s.style.left = Math.floor(Math.random()*100) + '%'
s.style.top = (window.scrollY + Math.floor(Math.random()*(window.innerHeight-200))) + 'px'
// Make the animation duration random between 1s and 3s
s.style.animationDuration = Math.random() * 2 + 1 + 's';
// Start the animation at a random point between 0s and 1s
// s.style.animationDelay = Math.random() + 's';
s.onanimationend = function() { this.remove() }
document.getElementById("star-bg").appendChild(s)
}
setInterval(addStar,50)
<div id="star-bg"></div>
#star-bg {
max-width: 100vw;
max-height: 100%;
height: 95%;
width: 100%;
overflow: hidden;
display: block;
}
.star, .star2 {
--size: 20vmin;
--holes: calc(var(--size)*.495);
width: var(--size);
aspect-ratio: 1/1;
position: absolute;
display: block !important;
background: white;
--mask: radial-gradient(var(--holes) at var(--holes) var(--holes), #0000 99%,#000) calc(var(--holes)*-1) calc(var(--holes)*-1);
-webkit-mask: var(--mask);
mask: var(--mask);
border-radius: 50%;
animation: sparkle 2s linear forwards;
z-index: -5;
}
.star2 {
--size: 10vmin;
aspect-ratio: 1.2 / 1;
}
@keyframes sparkle {
0% { opacity: 0; transform: scale(0); }
25% { opacity: 1; }
70% { opacity: 0.8; transform: scale(1); }
100% { opacity: 0; transform: scale(0); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment