{"id":280294,"date":"2018-12-18T12:28:24","date_gmt":"2018-12-18T19:28:24","guid":{"rendered":"http:\/\/css-tricks.com\/?p=280294"},"modified":"2018-12-18T12:28:24","modified_gmt":"2018-12-18T19:28:24","slug":"ease-y-breezy-a-primer-on-easing-functions","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/ease-y-breezy-a-primer-on-easing-functions\/","title":{"rendered":"Ease-y Breezy: A Primer on Easing Functions"},"content":{"rendered":"
During the past few months, I\u2019ve been actively teaching myself how to draw and animate SVG shapes. I\u2019ve been using CSS transitions, as well as tools like D3.js<\/a>, react-motion<\/a> and GSAP<\/a>, to create my animations.<\/p>\n One thing about animations in general and the documentation these and other animation tools recommend is using easing functions<\/strong>. I\u2019ve been working with them in some capacity over the years, but to be honest, I would never know which function to choose for which kind of animation. Moreover, I did not know about the magic that goes into each of these functions, the notable differences between them, and how to use them effectively. But I was fine with that because I knew that easing functions somehow \u201csmoothed\u201d things out and mostly made my work look realistic.<\/p>\n Here, I present to you what I learned about easing functions in the form of a primer that I hope gives you a good understanding as you dig into animations.<\/p>\n <\/p>\n I tried to re-create a pattern called rotating snakes<\/a>, an optical illusion that tricks the brains into thinking that circles rotate and \u201cdance\u201d when they are not.<\/p>\n I quickly found a gap in my knowledge when trying to build this out. It\u2019s hard! But in the process, I discovered that easing functions play a big role in it.<\/p>\n I turned to JavaScript to draw a bunch of concentric circles in SVG using a library:<\/p>\n This was the result:<\/p>\n But that clearly does not look anything like the picture.<\/p>\n As I thought things through, I realized that I was looking for a certain property. I wanted the change in radius of the concentric circles to be small at the beginning and then become larger as the radius increases.<\/p>\n This means that the linear increase in radius using …which got me this:<\/p>\n Hmm, still not what I wanted. In fact, this deviates even further from the pattern. Plus, this code is hardly customizable unwieldy to maintain.<\/p>\n So, I turned to math for one last attempt.<\/p>\n What we need is a function that changes the radius organically and exponentially. I had an \u201cAha!\u201d moment and maybe you already see it, too. Easing functions will do this!<\/p>\n The radius of each circle should increase slowly at first, then quickly as the circles go outward. With easing, we can make move things along a curve that can slow and speed up at certain points.<\/p>\n A quick Google search landed me at this gist<\/a> which is a well-documents list of easing functions and really saved my day. Each function takes one input value, runs formulae. and provides an output value. The input value has to be between 0 and 1. (We will dig into this reasoning later.)<\/p>\n A quadratic easing function looked promising because all it does is square the value it receives:<\/p>\n Here\u2019s the code I wound up using:<\/p>\n And we have a winner!<\/p>\n The difference between this pattern and my first two attempts was night and day. Yay for easing functions!<\/p>\n This little experience got me really interested in what else easing functions could do. I scoured the internet for cool information. I found old articles, mostly related to Flash and ActionScript which had demos showing different line graphs.<\/p>\n That\u2019s all pretty outdated, so here\u2019s my little primer on easing functions.<\/p>\n They\u2019re a type of function that takes a numeric input between 0 and 1. That number runs through the specified function and returns another number between 0 and 1. A value between 0-1 multiplied by another value between 0-1 always results in a value between 0-1.<\/strong> This special property helps us make any computation we want while remaining within specific bounds.<\/p>\n The purpose of an easing function is to get non-linear values<\/strong> from linear value inputs<\/strong>.<\/p>\n This is the crux of what we need to know about easing functions. The explanations and demos here on out are all geared towards driving home this concept.<\/p>\n Easing functions are a manifestation of the interpolation concept in mathematics<\/a>. Interpolation is the process of finding the set of points that lie on a curve. Easing functions are essentially drawing a curve from point 0 to point 1 by interpolating (computing) different sets of points along the way.<\/p>\nHow I got into easing functions<\/h3>\n
for (i = 1; i <= 10; i++) {\r\n drawCircle({radius: i * 10});\r\n}<\/code><\/pre>\n
i++<\/code> won\u2019t do the trick. We need a better formula to derive the radius. So, my next attempt looked something like this:<\/p>\n
let i = 1;\r\nlet radiusList = [];\r\nlet radius = 0;\r\nwhile (i <= 10) {\r\n drawCircle({radius: i * 10});\r\n if(i < 4) { i = i + 0.5 } else { i = i + 1 } \r\n}<\/code><\/pre>\n
function (t) { return t*t }<\/code><\/pre>\n
const easing = (t) => {\r\n return t*t\r\n}\r\nfor(i = 0; i<=1; i=i+0.05) {\r\n const r = easing(i) * 40;\r\n drawCircle(r);\r\n}<\/code><\/pre>\n
What are easing functions?<\/h3>\n