Skip to content

Instantly share code, notes, and snippets.

@Inventsable
Created January 10, 2020 05:24
Show Gist options
  • Save Inventsable/8ed9042b04469d729c052717c985c8e1 to your computer and use it in GitHub Desktop.
Save Inventsable/8ed9042b04469d729c052717c985c8e1 to your computer and use it in GitHub Desktop.
Word jumble expression for After Effects
posterizeTime(thisComp.layer("controller").effect("posterizer")("Slider")/2);
// The Array of words to jumble
let words = ['My', 'name', 'is', 'Blonky'];
// the digits parameter below is an Array. It contains the result of all rolling, and determines which word above to choose
function roll(digits) {
// attempt to roll a number between the first and last word
let dice = randomInteger(0, words.length - 1);
// if the number was already rolled, roll again
if (digits.includes(dice)) return roll(digits);
else {
// but if a new number, add to Array and return the value
digits.push(dice);
return digits;
}
}
// Pure random number generator. This is just an example of how AE's random() function actually works in vanilla Javascript
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Create an empty array for the result
let dialogue = [];
// Do something per word Array item
words.forEach((word) => {
// We pass in the initially empty Array, but it will populate itself with our roll function since that uses .push()
roll(dialogue);
})
// Now we need to change our roll Array from containing only digits to the actual word equivalents, like word[1], word[3], etc.
// We can easily modify it using Array.prototype.map, which alters every entry of an Array and returns the result
let speak = dialogue.map(item => {
// expression ? if true do this : else false do this -- this is one-line shorthand for if/else:
return randomInteger(0,1) ? words[item].toUpperCase() : words[item].toLowerCase();
// The new Array entry returns words[randomNumber], jumbling our initial array.
// If rolls a second time between 0 and 1, if 0 it returns all uppercase, else returns lowercase
})
// Finally join the newest array with a delimiter, a space character. This automatically converts it from an Array to a String
speak.join(' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment