By Luis Manuel
This tutorial is out of date and no longer maintained.
In recent times, we have seen that the web is no longer a page in a browser full of squares. Every day there are more designs that incorporate irregular shapes.
In this tutorial, we want to teach you how to create and animate shapes with a jelly effect. In the end, you will be able to create the jelly shape you want and animate it according to your needs without too much effort.
Specifically, in this first part we will see how to achieve the following result:
The maths behind an effect like this can be very difficult to achieve. That’s why we have tried to group the code needed to create and animate jelly shapes in a library that is easy to use for developers.
At the same time, we have been inspired by this pen by Thom Chiovoloni, inspired as well in the game The Floor is Jelly. And we have started specifically from this implementation of the jelly physics.
So, we’ve packaged this implementation into a library we’ve just called jelly.js, to which we’ve added everything we need to get jelly shapes easily. Then let’s see how to use it!
We have chosen SVG paths to create the shapes because we believe it is the easiest and most customizable way we have to do it. In this way, we can create the shapes that we want in a vector editor (like Inkscape or Illustrator), and insert them directly into our HTML document, or even import them from JavaScript.
For example, we can draw a simple shape like this:
Then we can include the relevant SVG code directly in the HTML:
We also need a canvas
to draw the shape, and some other elements with the aim of moving a letter (S, from Scotch) coordinately with the centroid (average point) of the shape.
For this example, we need very few styles. Let’s see the code:
Finally, we have arrived at the most fun part! Let’s draw our pentagon on the canvas
and see how we can animate it in a jelly way. But don’t worry, it will be very easy using our library.
We just need a few lines of code to draw our pentagon, and to get it reacting in a jelly way if we move the mouse to the edges of the shape:
Note that the constructor of our library (Jelly
) receives a canvas
element and a set of options. We can also provide an array of options, a set of options for each shape we want to draw. For a detailed description of the available options, you can check the GitHub repository.
We could leave it there, but our library has much more to offer. So let’s look at some other things we can do.
To illustrate a little more the options we have (and for fun), let’s see how we can shake our pentagon as we drag it across the screen.
First, we need to know when the mouse is inside the shape, to allow dragging it only when that happens. Check the following code, doing just that:
And let’s see how we can implement a basic dragging logic. Please look at the comments to understand what is happening there, and pay special attention to the shake
function:
The browser support is really good because all modern browsers have support for canvas
. But jelly.js
uses Promises, so we need a polyfill for any browser that does not support native promises.
On the other hand, the performance is very variable according to the browsers and the operating systems. This is due to the CPU-intensive work in each animation frame. So, don’t abuse the use of these effects, because they can kill website performance.
And we are done! So far we have created a truly jelly pentagon without too much effort. Our shape also change the cursor on hover event, and respond to dragging in a jelly way as well :)
You can see the final demo here, and you can get the full code on GitHub too.
These are not the only things we can do. In fact, in the second part of this tutorial, we will build an awesome slider, where everything will be jelly. In broad outline, we will learn how to:
We really hope you enjoyed it and found it useful! See you in the second part.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!