Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › After Effects Expression Needed: I am trying to build an Average Speed Generator

  • After Effects Expression Needed: I am trying to build an Average Speed Generator

    Posted by Christopher Johnstone on January 24, 2013 at 5:46 pm

    Hi, I need a little help getting my average speed gauge to work.

    I have a slider generating “MPH” values: essentially a stream of numbers fluctuating over time.
    What I am after is a single number for the the average speed accumulating from the start.
    I have had a hack at it using bit’s of code from the forums and I am getting a accumulative stream of figures.

    I’d be grateful for any help – Many thanks – Chris

    accum = "";
    a=inPoint;

    while (a < time) {
       v = thisComp.layer("MPH value").text.sourceText.valueAtTime(a);
       accum += v + "" + " " ;
       a += thisComp.frameDuration;
    }

    accum

    Christopher Johnstone

    Christopher Johnstone replied 13 years, 8 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    January 24, 2013 at 7:05 pm

    You mention a slider, but your expression looks like it gets the speed values from a text layer, is that correct? If so, I think you need to convert the strings to numbers via parseInt() or parseFloat(), and total them into your accum variable. Then divide accum by the number of frames that have elapsed to get the average. I haven’t tested this, but something like this should be close:


    accum = 0;
    f = Math.max(timeToFrames(time-inPoint),0);
    for (i = 0; i <= f; i++) {
    v = thisComp.layer("MPH value").text.sourceText.valueAtTime(framesToTime(i));
    accum += parseFloat(v);
    }
    avg = accum/(f+1);

    If your input really is a slider, you would replace:

    accum += parseFloat(v);

    with:

    accum += v;

    Dan

  • Christopher Johnstone

    January 25, 2013 at 10:30 am

    – Yes you are right, apologies Dan:

    I was using a keyframed slider to drive a Math.round expression in Text Layer which I was using as a source to generate my average.

    You solution was bang on an sorted me right out – Many thanks

    As a after note:

    Being an After Effects user who dabbles and loves Java Script but who has got in via the back door I am always looking for great resources to help me out. I rely heavily on your online presence of Mr Dan Ebberts, via his blogs and his various web sites as he put this stuff into a language that I understand. I would love to know if there is a set of comprehensive exercises out there that I can pick up on that will take me that little bit further into the world of Java Script. As I am using and pushing it more and more I am finding more and more walls to hit from a lack of my knowledge. I am sure, like many others, would love a one stop, easy to read, not too techy but covering everything, set of exercises. Is there anything out there? Indeed do you have a book coming out Dan?

    Many thanks for your ongoing help – Chris

    Christopher Johnstone

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