Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Need help calculating “linear()” function in JS Math

  • Need help calculating “linear()” function in JS Math

    Posted by Lu Nelson on January 14, 2010 at 3:09 pm

    Hi everyone,

    I’m trying to port some of my AE methods to another program that’s also using Javascript control (Quartz Composer), and need to determine a strictly mathematical equivalent to AE’s “linear()” interpolation method.

    I realize this is therefore not an AE question, but it began with AE so please bear with me 😉

    I found another post here, asking nearly the same question and the answer to “what is the long-form version of the linear() function” was this:

    result = value1 + ((t - t_min)/(t_max - t_min))*(value2 - value1);

    …but in this answer (by Dan Ebberts) it was noted that AE’s linear() function clamps the value, so you’d also have to add a clamp to it. i.e.:

    clampedresult = clamp(result, value1, value2);

    however, in porting to Quartz Composer, I can’t use this either because “clamp()” doesn’t exist in Javascript outside of AE, or at least it doesn’t in QC, where we basically just have javascript math. I looked up Javascript approaches to defining a clamp function and they are all of the following form:

    Math.min(Math.max(result, value1), value2);

    but the problem with that is that it cannot handle descending values. AE’s linear() function doesn’t care whether value1 is less or greater than value2. It will simply interpolate between any two numbers, as t1 goes to t2. In fact you can even reverse the t numbers and the interpolation will go in reverse. So I need a more robust mathematical definition here, something that can handle both ascending and descending interpolations. If it helps, I am only using it in the case where the reference number (t) goes from 0 to 1, so the code could be simpler there; but value1 and value2 could be anything.

    Really hoping someone can crack this one for me….

    Lu Nelson
    Berlin, Germany

    [MacProQ2.66, 8GB, Sys10.5.6, FCP 6.0.5, AE 9.0.2]

    Lu Nelson replied 16 years, 4 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    January 14, 2010 at 5:54 pm

    If t only goes from 0 to 1, then this should work:

    value1 + t*(value2 – value1)

    Dan

  • Lu Nelson

    January 15, 2010 at 6:57 pm

    Thanks Dan,

    that did the trick for the task at hand — indeed, since it’s only between 0 and 1 it’s simpler…

    I’m back to it.

    Lu Nelson
    Berlin, Germany

    [MacProQ2.66, 8GB, Sys10.5.6, FCP 6.0.5, AE 9.0.2]

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy