in ✏️ Articles · javascript art

The Magic of Fractal Snowflakes

This post has dynamic interactions. If you are reading this through a feed reader, it probably won't work as expected.

The winter ❄️ has finally come. I have been wishing for snow for a long time. Coming from a warm country such as Portugal, snow is not the most common thing to see. In fact, I only saw snow in Portugal once when I was a kid when I went to Serra da Estrela, the highest mountain in continental Portugal.

Since then, I’ve only seen snow a few other times when traveling, but never that experience of being outside while it is actually snowing. That has changed! The Netherlands has been suffering from low temperatures, reaching even -15Β°C where I’m living. It’s been over a week without positive temperatures!

However, it is not the weather of the Netherlands that brought you here. Due to the snowy situation, I had the idea of searching how to recursively generate snowflakes. The first method that I found, and probably the most known one, is the Koch snowflake. Even so, I am not going to talk about it today. Maybe some other day!

After a bit of digging, I found quite a nice video where it was explained how to create a fractal snowflake in JavaScript. I’m basing this post on that same code, with some slight changes and the possibility for you to try it out, live!

First and foremost: what is a fractal? I found this definition online that I think explains pretty well what a fractal is:

A Fractal is a type of mathematical shape that are infinitely complex. In essence, a Fractal is a pattern that repeats forever, and every part of the Fractal, regardless of how zoomed in, or zoomed out you are, it looks very similar to the whole image.

Usually, when we think about fractals, we think about infinitely repeating shapes. Nonetheless, that does not need to happen. There is a fantastic video by 3Blue1Brown that I would highly recommend to you if you’re interested in self-similarity and fractals.

Let’s get started then! But first, here’s the demo of the final “product” so you can see what we are trying to build. This demo is interactive, so you can change the parameters. I chose some default parameters that make the snowflake look really nice in my opinion!

Tip: you can right click on the image above and save it. Did you get something unique? Share it with the rest of us!

You will be amazed by how little code this requires. Seriously, it’s not even 50 lines of code and that includes empty lines! We will be using the canvas API so let’s start by assuming you have an HTML document with a canvas element on it, just like the following:

1<canvas id="painting" />

The first step is already concluded! You can now add some fancy CSS to add a background for it to look better and some dimensions. In this case I’m creating a radial gradient with two shades of blue, as well as setting the height and width to the same as of the screen.

1#painting {
2  background: radial-gradient(#4b7bec, #3867d6);
3  height: 100vh;
4  width: 100vw;

Now that we have enough HTML and enough CSS, let’s start with the actual code. First of all, we need to get the canvas element, as well as its 2D context in order to work with it. Then, we translate the context to the center of the canvas so we work from the center to the edges.

1const canvas = document.querySelector('#painting')
2const ctx = canvas.getContext('2d')
4canvas.width = window.innerWidth
5canvas.height = window.innerHeight
7// Move the context to the center
8ctx.translate(canvas.width / 2, canvas.height / 2)

Fantastic! Now we can jump into the actual code. As you saw before, there are quite a few options we wil work with. Those are: the number of levels, the number of branches, the number of sides and the spread.

Let’s start by defining the sides variable. This variable determines how many branches out of the center the snowflake will have. I will keep it as 6, but feel free to change it. After that, we call a function draw the same amount of times as the number of sides.

Following each call to draw, we must also rotate the 2D context. Knowing that 2Ο€ is a full circle, we need to rotate it 2Ο€ / sides each time so we can draw each branch.

 1let sides = 6
 2// ... more variables ...
 4function draw (level) {
 5  // ...most magic happens here...
 8for (let i = 0; i < sides; i++) {
 9  draw(0)
10  ctx.rotate(Math.PI * 2 / sides)

Did you notice that we called draw with an argument of 0? That is the current level. The level represents the number of recursive iterations. To ensure that we don’t run into an infinite loop, we must decide on a maximum levl. For now, I will set it as 5. If we reach that level, we stop.

 1// ... more variables ...
 2let maxLevel = 5
 4function draw (level) {
 5  if (level > maxLevel) {
 6    return
 7  }
 9  ctx.strokeStyle = '#d1d8e0'
10  ctx.lineWidth = 4
12  ctx.beginPath()
13  ctx.moveTo(0, 0)
14  ctx.lineTo(125, 0)
15  ctx.stroke()
17  // ... more magic ...

After checking if the current level is the last one, we start working on our snowflake! The first thing we set is the color and the width of the lines of the snowflake. Then, we start drawing! The lines you see above are quite self-explanatory. It starts by beginning a path, then we move the context to the center, and draw a line from (0, 0) to (125, 0).

You can try changing the values to see what you get. For now, you should see something like this:

Partial snowflake with 6 sides

Quite bare, mhmm? Now (almost) the most exciting part: the branches! This number represents the number of branches coming out of each branch. Thus, inside the draw function, we need to add a loop for each branch.

 1// ... more variables ...
 2let branches = 2
 4function draw (level) {
 5  // ... more magic ...
 7  for (let i = 1; i <= branches; i++) {
 8    // ... the magic loop ...
 9  }

We’re almost there! Let’s define the spread value, which will be multiplied by 2Ο€ to get the angle. Hence, this value represents the angle between a branch and its leafs. The largest the spread, the largest the angle. You could set the angle directly. However, for simplicity, it’s easier to define this value between 0 and 1 that will then be multiplied by the 2Ο€.

1// ... more variables ...
2let spread = 0.83
3let angle = Math.PI * 2 * spread

Now let’s fill the loop! In each iteration of the loop, we save the context so we can restore back to that state later. This is important in order to ensure we are drawing on the correct places and that we can go back to the previous spot without transformations applied.

 1for (let i = 1; i <= branches; i++) {
 3  ctx.translate(150 * i / (branches + 1), 0)
 4  ctx.scale(0.5, 0.5)
 7  ctx.rotate(angle)
 8  draw(level + 1, opts)
 9  ctx.restore()
12  ctx.rotate(-angle)
13  draw(level + 1, opts)
14  ctx.restore()
16  ctx.restore()

The first translation and scale define where we start drawing the next branch. This value depends on the number of branches and the dimension of each branch. Here I set it to 150, even though the initial branch size is 125. You can play with this values for nicer results. I simply found out this to work better.

The whole code
 1const canvas = document.querySelector('#painting')
 2const ctx = canvas.getContext('2d')
 3canvas.width = window.innerWidth
 4canvas.height = window.innerHeight
 6const maxLevel = 5
 7const branches = 2
 8const sides = Math.floor((Math.random() * 10) + 3)
 9const spread = (Math.random() * 0.48) + 0.51
10const strokeColor = '#d1d8e0'
11const lineWidth = 4
12const angle = Math.PI * 2 * spread
14function draw (level) {
15  if (level > maxLevel) {
16    return
17  }
19  ctx.strokeStyle = strokeColor
20  ctx.lineWidth = lineWidth
21  ctx.beginPath()
22  ctx.moveTo(0, 0)
23  ctx.lineTo(125, 0)
24  ctx.stroke()
26  for (let i = 1; i <= branches; i++) {
28    ctx.translate(150 * i / (branches + 1), 0)
29    ctx.scale(0.5, 0.5) // smaller from now on
32    ctx.rotate(angle)
33    draw(level + 1, opts)
34    ctx.restore()
37    ctx.rotate(-angle)
38    draw(level + 1, opts)
39    ctx.restore()
41    ctx.restore()
42  }
45for (let i = 0; i < sides; i++) {
46  draw(0)
47  ctx.rotate(Math.PI * 2 / sides)

In order to actually draw the branches, we now rotate the context by the angle and call draw recursively for the next level. Then, we do exactly the same for the symmetric angle so we draw leafs coming out of each side of the branch.

Snowflake: 5 levels, 2 branches, 0.83 spread, 6 sides

Right now, you should be able to see a similar snowflake as the one above. Play with the values, share with me your results. I hope you enjoyed this article as much as I did writing it.

Please enjoy your winter, cause spring is almost coming! For those of you in the southern hemisphere, enjoy the summer and I hope this post gave you some chills of winter. πŸ₯Ά

Interactions (11)


Eric Ronne Eric Ronne liked this post. β€” 16 Feb 2021 02:22
adlrocha adlrocha liked this post. β€” 15 Feb 2021 12:20 [email protected] liked this post. β€” 15 Feb 2021 12:20
Alan Shaw Alan Shaw liked this post. β€” 15 Feb 2021 12:20
Isabel Soares Isabel Soares liked this post. β€” 15 Feb 2021 12:20
Malik Piara Malik Piara liked this post. β€” 15 Feb 2021 12:20
Nimo Nimo liked this post. β€” 15 Feb 2021 12:20
Jan-Lukas Else Jan-Lukas Else said: Henrique from Portugal is currently experiencing real snow for the first time in the Netherlands. Inspired by this he programmed a little tool to generate snowflake fractals using HTML, CSS and JavaScript. Very cool idea! β€” 15 Feb 2021 08:41