Skip to content

Instantly share code, notes, and snippets.

@zimonitrome
Last active October 17, 2023 16:40
Show Gist options
  • Save zimonitrome/5f2a3387fd35c65505f5ce832419e277 to your computer and use it in GitHub Desktop.
Save zimonitrome/5f2a3387fd35c65505f5ce832419e277 to your computer and use it in GitHub Desktop.
After Effects text expression selector for bouncy text where you can control how many characters are shown at a given time.
var sliderProperty = effect("Slider Control")("Slider");
var myDelay = 0; // Default delay
var isCharacterShown = false;
// Loop through each keyframe to find when slider value crosses the textIndex
for (var i = 1; i < sliderProperty.numKeys; i++) {
var keyValueBefore = sliderProperty.key(i).value;
var keyValueAfter = sliderProperty.key(i+1).value;
if (keyValueBefore < textIndex && keyValueAfter >= textIndex) {
// Calculate the linear interpolation to find the exact time when the value crosses textIndex
var timeBefore = sliderProperty.key(i).time;
var timeAfter = sliderProperty.key(i+1).time;
var ratio = (textIndex - keyValueBefore) / (keyValueAfter - keyValueBefore);
myDelay = timeBefore + ratio * (timeAfter - timeBefore) - inPoint;
isCharacterShown = true;
break;
}
}
if (!isCharacterShown && sliderProperty.valueAtTime(0) >= textIndex) {
// If it hasn't found a delay yet and the initial value is greater than the textIndex
isCharacterShown = true;
myDelay = 0;
}
// If the character isn't meant to be shown based on slider values, keep it at startVal
if (!isCharacterShown) {
result = [100, 100];
} else {
// Parameters
var freq = 3; // Oscillation frequency.
var decay = 5; // Damping factor for the oscillation.
// Define initial and final values.
var startVal = [100, 100];
var endVal = [0, 0];
var result; // Variable to store the final result
// Calculate time offset from layer in-point, adjusted for the slider-derived delay.
var t = time - (thisLayer.inPoint + myDelay);
// Duration of the initial linear transition.
var dur = 0.1;
// Compute property value based on time.
if (t < dur) {
result = linear(t, 0, dur, startVal, endVal); // Linear transition until duration.
} else {
var amp = (endVal[0] - startVal[0]) / dur; // Amplitude of oscillation. Assuming both components have same amplitude.
var w = freq * Math.PI * 2; // Angular frequency.
// Damped oscillation after the duration.
result = endVal.map(function(val, index) {
return val + amp * (Math.sin((t - dur) * w) / Math.exp(decay * (t - dur)) / w);
});
}
}
result; // Return the final value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment