Public Solving: Let it snow

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today the elves asked us to make some snow animations! This is a pretty exciting task as we have to get out our creative hats.
You can find the complete puzzle here.
So far I've done some confetti in CSS, and an animated snake. Today we can add animated snow to the list.
We are free to make it in any way we want.
My results looks like this:
I'm right away thinking in the lines of the CSS confetti I made, where we repeat 50 divs and use CSS to randomize some elements of every snowflake.
The things I want to randomize:
This would be the easiest to use SASS, which isn't mentioned as a no-go, so we'll be implementing that.
For the creation of 50 divs, we could use pug, but I inject them through JavaScript.
Let me guide you through this process step by step.
First, we need to add our 50 divs into the main container. This main container already exists and looks like this:
<main class="snow"></main>
We can fetch this element in our provided JavaScript file by using the following code.
const snowContainer = document.querySelector('.snow');
Then we'll need to create a loop that runs 50 times and adds a new element into this one.
[...Array(50)].forEach((_, i) => {
const snowFlake = document.createElement('div');
snowFlake.classList.add('snowflake');
snowContainer.append(snowFlake);
});
This forEach hack is a simple way to generate x looped lines.
We then use the createElement function to create a new div and add the snowflake class.
After which, we add out to our container element.
Now that we have these 50 divs in the viewport, we need to change the default CSS import to work with SASS files.
Luckily for us, Vite already supports this out of the box. We just need to install the preprocessor.
npm install -D sass
Then we can change our file from style.css to style.scss.
And modify the import in the main.js to look like this:
import './style.scss';
Right, we can now leverage the massive powers of SCSS.
There are some elements to our snowflake that never really change. We can style those in a general fashion.
.snowflake {
--size: 1vw;
background: #fff;
border-radius: 50%;
position: absolute;
width: var(--size);
height: var(--size);
top: -5vh;
}
This sets a basic viewport-based snowflake. It will start outside of the viewport on the negative top side.
Then we want to create a loop to add our differences to each individual snowflake.
@for $i from 1 through 50 {
.snowflake:nth-child(#{$i}) {
--size: #{random(10) * 0.1}vw;
left: #{random(100)}vw;
animation: snowfall #{10 + random(10)}s linear infinite;
animation-delay: -#{random(15)}s;
}
}
Here we loop 50 times, and for each of the snowflake, we set the following:
0.1vw and 1vw.The animation looks like this:
@keyframes snowfall {
0% {
transform: translate3d(0, 0, 0);
}
100% {
transform: translate3d(0, 110vh, 0);
}
}
At this point, we get the random flakes falling down, but they fall straight down, so maybe we should add a slight offset to mix things up.
To achieve this, we need a horizontal start and endpoint. This should be a random number based on a percentage of the viewport's width. As we don't want the snowflakes to fall across the whole screen.
--horizontal-start: #{random(20) - 10}vw;
--horizontal-end: #{random(20) - 10}vw;
And then, we can modify our animation to start and end on these values.
@keyframes snowfall {
0% {
transform: translate3d(var(--horizontal-start), 0, 0);
}
100% {
transform: translate3d(var(--horizontal-end), 110vh, 0);
}
}
That's it, my version of CSS-based animated snow ❄️.
I would be delighted to see other people's snow animations, as some are wizards with CSS 👀.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter