Oh fiddlesticks
Something's gone wrong here. Try again and see if it works, if it doesn't you're allowed to shout at me.

HTML5 Snowfall Effect (“Mimas”)

Posted: March 9th, 2015

After the madness of Christmas 2014 (trust me, this industry is madness) we settle in to the quiet period… Lots of general concepting and out-of-the-box thinking, and other manager buzzwords. I generally get a bit of free time here to play with some things on my own – usually working on website designs.

One of the projects I toyed with this year was an element for a future design rather than the design its self. I tend to name everything according to some system when I’m sprouting concepts like this, and for this year I settled on moons of Saturn (essential trivia: in the past I’ve used Shakespearean characters and bars in Sydney), so this project is known to me as Mimas.

When I interviewed for my current job I had hacked together a prototype based on a brief sent to me, in the header was a simple snowfall effect, with the snow settling on the bottom of the frame. I decided to revisit that idea, but go upscale, and borrow some code from my work wrapping text in php with imagettftext etc.

mimas

 

So what we end up with is a falling snow animation in <canvas>, where the snow settles across the complex boundary of the foreground objects. In fact, we have two distinct collision zones.

 

mimas html5 canvas snow fall debug

 

The red line is a hard stop, but the green line is probabilistic. Once a flake crosses it it has a chance of stopping each frame, so it appears the snow is falling around the objects in 3D and makes the whole effect more realistic. To cap the whole thing off, we have animated smoke (a lot of groups of slightly different coloured circles) and subtly flickering glows in all the windows. Oooh, cosy!

Over time the effect blankets the objects, which you can see toward the end of the video above (total runtime on that is about 30 minutes, by the way).

So, how do we achieve this? Pretty easily really.

I’ll say first off you can view a live demo here – for all you eager code junkies who’ll understand the rawness of a messy prototype.

Now, on to the gubbins. All our data defining the lines is stored as percentages of the width and height of the images used in the construction to allow for resizing of the animation, the same as generating image previews, and it’s scaled at the start of the animation. Our collision testing also re-uses code from that project, a simple point-line intersection test which is all nice fast basic maths – I will confess to nabbing a well optimized version of this from the web, rather than going to work on my own!

 

 

Note: The comment regarding “skipping minY as the last element” is related to some other logic. When the vertices are scaled the minimum Y value is also stored in the outputted array so we can easily exclude flakes that haven’t reached that point yet.

Besides that the animation aspects are fairly boilerplate. Because we have to clear and refresh the falling snow canvas each frame there’s actually a second canvas there too which holds the fallen snow, flakes are drawn┬áthere when they collide so their Javascript objects can be removed.

I won’t go in to too much detail because you can go and disect the code yourself, but it’s also worth mentioning I built a non-HTML5 version without the settling or collision detection, just falling snow. The loader stub uses jQuery to load either the posh version or boring version based on browser <canvas> support.

 

Thanks all,

— Ben