While working on the True Sparrow and Slackmin we were supposed to create different SVG Shapes to follow a predefined path. After looking at different options to achieve it we found that greensock was our best option to go with. It is fast, robust and compatible with most of the js frameworks, html and css. It is also compatible with most of the modern browsers as well.
In order to achieve our animations, the first task was generating SVG. We tried generating SVG using Figma but the problem was that Figma generated SVG code was too lengthy and the animation paths were not smooth enough. So, we generated our own SVG paths, polygons and circles.
Let's take an example of generating SVG containing a path for a circle and a polygon.
There are three elements in above SVG:
- Circle: Shape that will animate on the path.
- Triangle: Polygon that will animate on the path.
- Path: Which the elements would follow. You can also create your own using, M - Starting coordinates, L - Intermediate coordinates, Q - Intermediate coordinates with one side curve, C - Intermediate coordinates with two side curve.
Once our SVG is ready, the next step is to animate the shapes on the predefined path. As we discussed earlier in this post we will be using greensock (gsap) to achieve it. Post requiring gsap library in your code, following function would animate the circle in the SVG.
Above function would change the origin of the circle at every update. And In the given duration it takes a circle to the end of the path. Value of ‘repeat’ signifies how many trips to be done, -1 means infinite.
Another shape in our SVG is a triangle, let’s animate it now. For that you have to use a motionPath plugin.
autoRotate helps polygon align on the path. alignOrigin helps to align a polygon from a given origin, in the above example its center.
Similarly, you can animate any shape and not just a circle or a triangle using greensock. For more details refer to greensock documentation.