Skip to content

Instantly share code, notes, and snippets.

@thehack
Created December 8, 2019 03:47
Show Gist options
  • Save thehack/fee9afe7a8b5ded10d0780c7c25663a1 to your computer and use it in GitHub Desktop.
Save thehack/fee9afe7a8b5ded10d0780c7c25663a1 to your computer and use it in GitHub Desktop.
Javscript for snow
<h1>JavaScript Snow</h1>
(function() {
var quantity = 100;
var snowflakes = [];
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createSnowflake() {
var el = document.createElement("div");
el.style.position = "fixed";
el.style.zIndex = "999999";
el.style.color = "white";
el.style.fontSize = getRandomNumber(10, 20) + "px";
el.style.top = getRandomNumber(0, window.innerHeight) + "px";
el.style.left = getRandomNumber(0, window.innerWidth) + "px";
el.innerHTML = "&#10052";
el.speed = 33 / parseInt(el.style.fontSize);
return el;
}
function moveSnowflakes() {
for (var i = 0; i < quantity; i++) {
moveSnowflake(snowflakes[i]);
}
}
function moveSnowflake(el) {
var height = window.innerHeight;
var top = parseInt(el.style.top);
//top += speed;
if (top > height) {
resetSnowflake(el);
} else {
el.style.top = top + el.speed + "px";
}
}
function resetSnowflake(el) {
el.style.top = "0px";
el.style.left = getRandomNumber(0, window.innerWidth) + "px";
}
for (var i = 0; i < quantity; i++) {
particle = snowflakes[i] = createSnowflake();
document.body.appendChild(particle);
}
setInterval(moveSnowflakes, 33);
})();
body{background-color:#37474F}
h1 {
margin-top:42vh;
color:#E1F5FE;
text-align:center;
font-size:500%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment