3

Is there a way to convert jQuery-compatible easing functions so that they only need a single parameter? (e.g. to use them with something like skrollr).

For example easeOutBack takes six parameters.

The original function:

easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}

What I want:

easeOutBack: function (x) {
    // same return
}
8
  • Why would you need something like that? Looks like a XY problem meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Mar 20, 2014 at 11:23
  • I am trying to generate a custom easing-function to be used with skrollr. Therefore it should have that format. See this issue: github.com/Prinzhorn/skrollr/issues/341#issuecomment-34225683 Commented Mar 20, 2014 at 11:30
  • This comment still holds true github.com/Prinzhorn/skrollr/issues/341#issuecomment-25972719 Commented Mar 20, 2014 at 11:55
  • @A.Wolff Because they are over-engineered, slow, and buggy (they generate NaN's when d == 0, don't handle the edge cases when t < 0, nor t > d), inefficient by declaring but not using the param x. Commented Apr 11, 2016 at 21:13
  • @Michaelangel007 If duration is zero, then velocity is infinite, so NaN is the correct result. Similarly, if the time is not between 0 and duration, all bets are off and the easing function result cannot be trusted. An easing function could be coded to swallow a time-out-of-bounds error, but I'd say it's better not to sweep it under the rug, as it signals something is fundamentally wrong in the application's time tracking. As for the x parameter, that was added in the jQuery code and I have no idea why. Commented Mar 15, 2017 at 20:19

2 Answers 2

4

There you go:

easeOutBack: function (p, s) {
    s = 1.70158;
    p = p - 1;
    return (p * p * ((s + 1) * p + s) + 1);
}

I will document the process here once and for all. First read this to understand what the variables mean jQuery easing function — variables' comprehension.

Now it's pretty easy.

Since c is 1 and b is 0, we can just remove them (there's a multiplication with c and b is added, which are both noops).

if (s == undefined) s = 1.70158;
return ((t=t/d-1)*t*((s+1)*t + s) + 1);

Now we just set s to the magic number 1.70158, because I have no idea if something else will ever be passed to the easing function by jQuery (skrollr won't for sure).

s = 1.70158;
return ((t=t/d-1)*t*((s+1)*t + s) + 1);

Now jQuery is highly optimized, let's move this weird assignment inside the expression to the line before

s = 1.70158;
t = t/d - 1;
return (t*t*((s+1)*t + s) + 1);

Almost there! If you read about t and d you will notice that t/d is the percentage of how much the animation has progressed so far. Guess what, that's what skrollr calls p (see, for percentage). Let's just replace t/d with p

s = 1.70158;
t = p - 1;
return (t*t*((s+1)*t + s) + 1);

And the last step is to rename t to p, because we don't need a third variable.

s = 1.70158;
p = p - 1;
return (p*p*((s+1)*p + s) + 1);
Sign up to request clarification or add additional context in comments.

Comments

-1

Prinzhorn's answer is correct - I just wanted to point to the fact that you can easily create your own functions with this generator and convert them using the above shown math to use it with Skrollr.

Example

function(t, b, c, d) {
  var ts=(t/=d)*t;
  var tc=ts*t;
  return b+c*(1*tc*ts + -2.5*ts*ts + -1*tc + 4*ts + -0.5*t);
}

can be used like this

<div data-0="opacity[easeCustomAwesomeSuper]:0"></div>
<script>
  $(function() { var s = skrollr.init({ 
    easing: {
      easeCustomAwesomeSuper: function(p) {
        return p*p*p*p*p + -2.5*(p*p*p*p) + -1*(p*p*p) + 4*(p*p) + -0.5*p;
      }
    }
  });});
</script>

I've created this gist to show the details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.