Glitch Text In After Effects by KYLE MARTINEZ

Это копия страницы, на тот случай, если автор вдруг пропадет или его страница будет удалена. Ссылка на оригинал:

Glitch Text In After Effects

I recently put together some fun glitch text for a client project. It’s created with an expression and is completely procedural. The studio I was working with sent over a really neat website as reference. Using it as inspiration I wrote my own After Effects friendly version.

If you’re curious about how the expression works keep reading. If you just want to copy and paste the code scroll to the bottom of this page. Enjoy!

There are two sliders controlling the animation. The Character Offset Percentage slider can animate from 0% to 100% and controls how many characters are randomized. The Posterize Time slider controls how fast the expression updates because I found randomizing the text every frame was too quick.

Code Breakdown

Take the Posterize Time slider value and use it as the frame rate. I found that 12 fps looked best.

posterizeTime(effect("Posterize Time")("Slider").value);

Take the Character Offset Percentage slider value and converts it to a decimal. I found that animating between 0% and 80% looked best.

var probability = effect("Character Offset Percentage")("Slider").value / 100;

All the characters used for the glitch. These choices were directly inspired by the example website. I simply did a Google search for “unicode characters” and copy/pasted the characters directly into the expression.

var characters = ["░","▒","▓","<",">","/"];

Split the original text into an array of lines and get how many lines there are.

var textLines = value.split("\r");
var numTextLines = textLines.length;

Loop through all of the lines. First take the old line, create a new line, and then replace the old line.

for (var l = 0; l < numTextLines; l++) {
    var oldLine = textLines[l];
    var newLine = getNewLine(oldLine);
    textLines[l] = newLine;
}

This is where the magic happens.

Create a new empty line then take the old line and go through character by character. For each character check to see if a randomly generated number (between 0 and 1) is greater than or equal to the value from the Character Offset Percentage. If it is greater than or equal and add a random character to the new line. If is is less, then just add the original character to the new line.

function getNewLine (oldLine) {
    var newLine = "";
    for (var i = 0; i < oldLine.length; i++) {
        if (random() <= probability) {
            newLine += getRandomCharacter(characters);
        } else {
            newLine += oldLine.charAt(i);
        }
    }
    return newLine;
}

Get a random character from the list of allowed characters.

function getRandomCharacter (a) {
    return a[Math.floor(random() * a.length)];
}

Join all of the newly created lines back together again. This is the line that actually puts the text back into the text layer.

textLines.join("\r");

Complete Code

Download the entire expression and try it out in your own project.

posterizeTime(effect("Posterize Time")("Slider").value);
var probability = effect("Character Offset Percentage")("Slider").value / 100;
var characters = ["░","▒","▓","<",">","/"];

var textLines = value.split("\r");
var numTextLines = textLines.length;
for (var l = 0; l < numTextLines; l++) {
    var oldLine = textLines[l];
    var newLine = getNewLine(oldLine);
    textLines[l] = newLine;
}

function getNewLine (oldLine) {
    var newLine = "";
    for (var i = 0; i < oldLine.length; i++) {
        if (random() <= probability) {
            newLine += getRandomCharacter(characters);
        } else {
            newLine += oldLine.charAt(i);
        }
    }
    return newLine;
}

function getRandomCharacter (a) {
    return a[Math.floor(random() * a.length)];
}

textLines.join("\r");