-2

I am trying to understand how the math unfolds through this code, easingDoc

function easeInOutQuad (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
  
console.log(easeInOutQuad(0.3,50,150,1));
console.log(easeInOutQuad(0.95,50,150,1));

I am trying to decipher the equation for both the conditions. While I understand, the condition for if ((t/=d/2) from this post describes what t/=d means and how it affects the calculation.

For example, (t/=d/2)<1 the code can be rewritten as following and it would produce the identical result. Such as,

function easeInOutQuad (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
  

function alternate (t,b,c,d){
    if (2*t/d < 1) return c/2*(2*t/d)*(2*t/d)+b;
    return 999999;};

console.log(easeInOutQuad(0.3,50,150,1));
console.log(alternate(0.3,50,150,1)); //(150/2)*(2*0.3/1)*(2*0.3/1)+50
console.log(easeInOutQuad(0.45,50,150,1));
console.log(alternate(0.45,50,150,1));//(150/2)*(2*0.45/1)*(2*0.45/1)+50

I was wondering how can I express return -c/2 * ((--t)*(t-2) - 1) + b explicitly in equation like console.log(alternate(0.3,50,150,1)); //(150/2)*(2*0.3/1)*(2*0.3/1)+50 so that I can undersatnd what the math is for that part.

smpa01
  • 4,149
  • 2
  • 12
  • 23
  • 3
    That’s ridiculous code. `(t-1)*(t-3)` is a much more readable equivalent. – Ry- Jul 12 '22 at 21:49
  • Some visual examples on https://easings.net/ – IT goldman Jul 12 '22 at 21:50
  • Assigment expressions are equal to the resulting value. A lot of that code looks awful anyways. – don_aman Jul 12 '22 at 21:54
  • @ Ry- thanks. `function alternate (t,b,c,d) {if (2*t/d < 1) return c/2*(2*t/d)*(2*t/d)+b; return -c/2 * ((2*t/d-1)*(2*t/d-3) - 1) + b;};}` is doing the job. Can you please explain how `(--t)*(t-2)` is equivalent to `(t-1)*(t-3)`. Thanks. – smpa01 Jul 12 '22 at 21:58
  • 1
    [MDN decrement operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Decrement) – Thomas Jul 12 '22 at 22:04
  • @dippas I don't see that operator combination anywhere in the code shown, and that question looks to be the result of a simple misunderstanding. Seems unlikely to help the OP. – miken32 Jul 12 '22 at 22:32
  • @miken32 its in the answer `It is the post decrement operator -- followed by the greater than operator >. Just the spacing is weird.` – dippas Jul 12 '22 at 22:33
  • @dippas I understand that. I just don't see how it answers the OP's rather unfocused (why I voted to close) question. – miken32 Jul 12 '22 at 22:35

1 Answers1

1

Probably the following 2 expressions could look tricky, here are the alternatives:

  c = -1 * c; // -c
  t = t - 1;  // --t

And an example for -c/2 * ((--t)*(t-2) - 1) + b;

// -c/2 * ((--t)*(t-2) - 1) + b;
function alternate(t, b, c, d) {
  c = -1 * c; // -c
  t = t - 1;  // --t
  return (c / 2) * (t * (t - 2) - 1) + b;
};

console.log(alternate(0.3, 50, 150, 1));
console.log(((-1 * 150) / 2) * ((0.3 - 1) * ((0.3 - 1) - 2) - 1) + 50);
Telman
  • 1,444
  • 1
  • 9
  • 12