Animate typewriter text on keyframed source text?

Scripting for After Effects

Animate typewriter text on keyframed source text?

Postby Ampix0 on 04/4/2016, 2:48 pm

If the source text changes over time, how might you animate it to appear typed? The animation preset can not handle the dynamic source text.
Ampix0
 
Posts: 109
Joined: 05/13/2009, 6:23 pm

Re: Animate typewriter text on keyframed source text?

Postby Guitarboy2828 on 04/4/2016, 11:54 pm

How are you animating the source text to change?
Guitarboy2828
 
Posts: 357
Joined: 04/1/2012, 12:05 pm

Re: Animate typewriter text on keyframed source text?

Postby Ampix0 on 04/5/2016, 7:06 pm

Im using the source text attribute and keyframing it.

I want this to type itself.

<>

<p>

<p></p>

<p> Hello </p>
Ampix0
 
Posts: 109
Joined: 05/13/2009, 6:23 pm

Re: Animate typewriter text on keyframed source text?

Postby star+circle on 04/5/2016, 10:52 pm

For a short piece of text the easy answer is to change the source text letter by letter.

Not that useful if you have a lot of code to animate in this fashion...

If you do, I would think you'd have to create an expression that looks at what the text was in the previous source text keyframe and the next source text keyframe, find the string piece that is different and add a part of that missing text in based on the progress between these two keyframes. So if you're 30% between the keyframes, you'd add 30% of the missing string in, etc.

As long as you add one contiguous piece of text at each keyframe, it will be a relatively easy to find the (one or) two places where the string changes.
star+circle
 
Posts: 729
Joined: 12/6/2014, 3:07 pm
Location: Finland

Re: Animate typewriter text on keyframed source text?

Postby Guitarboy2828 on 04/6/2016, 1:01 am

Unless I'm still not fully understanding, why can't you use the "Typewriter" animation preset? You can have multiple animators.

Is this what you're trying to do?

Image
Attachments
Untitled.gif
Guitarboy2828
 
Posts: 357
Joined: 04/1/2012, 12:05 pm

Re: Animate typewriter text on keyframed source text?

Postby Ampix0 on 04/6/2016, 1:41 pm

What this is missing is the ability to go back and add text afterward. So your animation, but then add the word "Hello" in between the '<p>' and '</p>'.
Ampix0
 
Posts: 109
Joined: 05/13/2009, 6:23 pm

Re: Animate typewriter text on keyframed source text?

Postby Guitarboy2828 on 04/6/2016, 11:48 pm

AHHHHH.. Ok, I get it now.. You could half fake it and use the range selectors "offet" to make hello type in the middle of the <p>'s but, you'd still start it with a gap. Otherwise, you'd best ask the expression experts.
Guitarboy2828
 
Posts: 357
Joined: 04/1/2012, 12:05 pm

Re: Animate typewriter text on keyframed source text?

Postby star+circle on 04/8/2016, 12:40 am

Here's my attempt at it. As long as you only add one contiguous piece of text between keyframes, it should work. Also, you have to have the first keyframe in the first frame of the layer.

Code: Select all
k = nearestKey(time);
if (k.time>time) {
k_index = k.index-1;
} else {
k_index = k.index;
}
prev_text = text.sourceText.valueAtTime(key(k_index).time)
if (k_index < numKeys) {
new_text = text.sourceText.valueAtTime(key(k_index+1).time)
}else{
new_text = prev_text;
}

done = 0;
index1 = -1;
for (n = 0; n < prev_text.length; n++){
if (prev_text[n]==new_text[n]){
if (done == 0){
index1++}
}else{
done = 1;
n = prev_text.length}
}

done = 0;
index2 = new_text.length;
for (n = 0; n < prev_text.length; n++){
if (prev_text[prev_text.length-1-n]==new_text[new_text.length-1-n]){
if (done == 0){
index2--}
}else{
done = 1;
n = prev_text.length}
}

if (index2<index1){index2 = index1};
if (index1 == prev_text.length-1){index2 = new_text.length}
textlenght = index1 + new_text.length-index2-1;
if (textlenght > prev_text.length-1){index2 = new_text.length - (textlenght-index1-1)}

key1_time = key(k_index).time;
if (k_index < numKeys) {
key2_time = key(k_index+1).time;
num_letters = index2-index1-1;
keyprogress = Math.floor((time - key1_time)/(key2_time-key1_time)*num_letters);
}else{
key2_time = key1_time;
num_letters = 0;
keyprogress = 0;
}

string1 = new_text.substring(0, index1+1+keyprogress);

if (index2<new_text.length){
string2 = new_text.substring(index2, new_text.length);
}else{
string2 = ""
}

my_text = string1+string2;
my_text
star+circle
 
Posts: 729
Joined: 12/6/2014, 3:07 pm
Location: Finland

Re: Animate typewriter text on keyframed source text?

Postby star+circle on 04/9/2016, 3:33 am

Test animation.
Attachments

[ Play Quicktime file ] typing code.mp4 [ 278.46 KiB | Viewed 2903 times ]

star+circle
 
Posts: 729
Joined: 12/6/2014, 3:07 pm
Location: Finland


Return to After Effects Expressions