Counter expression, split digits
by James Armstrong
on
Apr 22, 2009 at 4:42:56 pm
Hi,
I am trying to create a 9 digit counter where the numbers are not displayed altogether, but rather spread across the comp.
What I need is an expression that controls my text layers based on selected digits of a number (generated by slider).
Originally I used
val = effect(”Slider Control”)(”Slider”);
numDec = 0; // digits to right of decimal
numDigit = 2; // digits to left of decimal
if (val < 0) sign = "-" else sign = ""; s = Math.abs(val).toFixed(numDec); while (s.length < numDigit + numDec + 1) s = "0" + s; sign + s
from a post here somewhere, with an aim to simply creating masks around the appropriate digit on each text layer and spreading them about. However the client has chosen a non monospace font so the numbers were all wiggling about in and out of their masks as the numbers around them changed.
To this end, if I could get a single digit per text layer, its position would remain pretty static. Can somebody help (Dan?) thanks.
Re: Counter expression, split digits by Dan Ebberts on Apr 22, 2009 at 5:44:33 pm
Assuming your digit layers are layer 1 thru 9 (with layer 1 being the left-most digit) this mod to your expression should get you close:
val = effect(”Slider Control”)(”Slider”);
numDec = 0; // digits to right of decimal
numDigit = 2; // digits to left of decimal
if (val < 0) sign = "-" else sign = "";
s = Math.abs(val).toFixed(numDec);
while (s.length < numDigit + numDec + 1) s = "0" + s;
s = sign + s;
s.substr(index-1,1)
Re: Counter expression, split digits by James Armstrong on Apr 23, 2009 at 9:37:03 am
Wow! I didn't expect to get a response so fast! Thanks.
I tried Dan's expression out and came very close.
The problem I had was that in figures with less digits than the number of layers, the digits are appearing in the wrong place.
for instance if the slider was '1065' the text layers said '1065_ _ _ _ _ 'rather than' _ _ _ _ _ 1065'
So I just adjusted the digits to the left of the decimal place like so
val = thisComp.layer("Number Cruncher").effect("LAST6 digits")("Slider");
numDec = 0; // digits to right of decimal
numDigit = 8; // digits to left of decimal
if (val < 0) sign = "-" else sign = "";
s = Math.abs(val).toFixed(numDec);
while (s.length < numDigit + numDec + 1) s = "0" + s;
s = sign + s;
s.substr(index-1,1)
...and Robert's your mother's brother!
Now because the slider does not have enough digits to go up to numbers I need to show, I will create 2 pre comps controlled by 2 sliders in the main comp; one for the first 3 digits, one for the last 6.