Skip to main content
Participant
October 11, 2021
Answered

Link a keyframe to a slider control and animate with whole numbers.

  • October 11, 2021
  • 2 replies
  • 2482 views

I want to have a slider control for an animation couting up a percentage, so from 0% through to whatever value set via the slider control. I'd like the text to display as:

 

Rating = X% (X being the value)

 

I've found some useful posts on here that have helped me get some of the way there, currently I have the expression:

 

if (numKeys > 1){

t1 = key(1).time;

t2 = key(2).time;

v1 = 0;

v2 = effect("Slider Control")("Slider");

linear(time,t1,t2,v1,v2);

}else value

 

For linking the second keyframe on my "Source Text" value to my slider control. This works but you get all kinds of decimal places and I'd like whole numbers only, so I then added the following to get rid of the decimal places and add a "%" to the end.

 

text:x=Math.floor(effect("Slider Control")("Slider"));x+" %"

 

But it removes the animation, the text now only displays the amount set on the slider control and not the animaion from 0 to it.

 

Also, adding:

 

"Rating = " +

 

at the front breaks the whole expression.

 

Any help with these issues would be much appreciated!

This topic has been closed for replies.
Correct answer Mylenium

You need to clean up your syntax and create proper variables to hold the value. You are also introducing useless code elements that throw off the engine. This will be better:

 

if (numKeys > 1){
t1 = key(1).time;
t2 = key(2).time;
v1 = 0;
v2 = effect("Slider Control")("Slider");
mPer = linear(time,t1,t2,v1,v2);
}
else
{mPer = value}

"Rating = " + Math.floor(mPer) + "%"

 

Mylenium

2 replies

Mylenium
Legend
October 11, 2021

mPer is just a variable that holds the value so it can carried more easily instead of always writing out the full code. Other than that I think you are mixing up values and strings. Anything within a " " is a string and they are concatenated in the order they are typed out. In this case this also goes for the mPer value, because JS automatically converts certain data types to strings. If you wanted to be academic about it, of course you could explicitly convert it with mPer.toString(), but it's not necessary and will only confuse you further.

 

Mylenium

Participant
October 11, 2021

Thanks for the clarification!

Mylenium
MyleniumCorrect answer
Legend
October 11, 2021

You need to clean up your syntax and create proper variables to hold the value. You are also introducing useless code elements that throw off the engine. This will be better:

 

if (numKeys > 1){
t1 = key(1).time;
t2 = key(2).time;
v1 = 0;
v2 = effect("Slider Control")("Slider");
mPer = linear(time,t1,t2,v1,v2);
}
else
{mPer = value}

"Rating = " + Math.floor(mPer) + "%"

 

Mylenium

Participant
October 11, 2021

That has worked perfectly, thank you.

 

Could you explain how the last line shows the expression where to add the text? It looks to me like that would give the result:

 

X% Rating = %

 

but it does put it in the correct order (Rating = X%).

 

Also, I'd like to understand the mPer part of the expression. Can you explain or point in the right direction to find out more?

 

Thank you again!

 

 

Also