-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathae-textanimation-aecran
75 lines (61 loc) · 2.8 KB
/
ae-textanimation-aecran
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var charDelay = 0.05; // ~0.05 Time offset before the next character also starts to animate
var charDur = 0.1; // ~0.1-0.2 How long each character animates
var fps = 5; // ~5 Speed of random characters
var baseHoldTime = 0.8; // Default for in case the hold time is not mentioned in the 2D array
var adjustedHoldTime = 0; // Initializing
var startText = "xxx"; // Initializing
var endText = "yyy"; // Initializing
var largerCharCount = 0; // Initializing
// Array of words/phrases to animate through [Word, HoldTimeAdjustment]
var wordArray = [
["EAT", 0],
["SLEEP", 0],
["LIVE", 0],
["LAUGH", -0.2],
["LOVE", 1],
["GETREADYTO", 0],
["LOOP IT", 0]
];
var totalWords = wordArray.length;
var wordsTimeTotal = 0; // Cumulative time of words so far
var w = 0; // Counter for current word we are on
// Determine current word [w] based on time
for (w = 0; w < totalWords; w++) {
startText = wordArray[w][0];
endText = (w + 1 < totalWords) ? wordArray[w + 1][0] : wordArray[0][0]; // Next word (w+1) or go to word 0 if end of array
// Applying word-specific hold time adjustment from 2D array
adjustedHoldTime = baseHoldTime + wordArray[w][1];
if (adjustedHoldTime < 0) { adjustedHoldTime = 0; } // Preventing negative holdtime
largerCharCount = Math.max(startText.length, endText.length);
var currentWordTime = adjustedHoldTime + (largerCharCount * charDelay) + charDur; // use the larger one because even a shorter start/end word will have to animate ALL characters of the larger word
if (time <= (wordsTimeTotal + currentWordTime)) {
break; // Stop once we've passed the current time
}
wordsTimeTotal += currentWordTime;
}
var t = time - adjustedHoldTime;
var getRandomCharacter = function(seed) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
seedRandom(Math.floor(time * fps) + seed, true); // Use time for consistency across frames
return chars.charAt(random(0, chars.length));
};
var output = [];
for (var i = 0; i < largerCharCount; i++) {
var myCharDelay = wordsTimeTotal + (charDelay * i);
if (t < myCharDelay) {
// Output char from original word
output.push(startText.charAt(i) || ''); // Add '' if no char at index i to avoid out of bounds
} else if (t < myCharDelay + charDur) {
// Output random character (junk)
output.push(getRandomCharacter(i));
} else {
// If startText is longer than endText, remove characters from the LEFT of startText
if (startText.length > endText.length) {
output.push(endText.charAt(i - (startText.length - endText.length)) || ''); // Start from the end of startText
} else {
output.push(endText.charAt(i) || ''); // Output char from the original word & Add '' if no char
}
}
}
// Return the final result (joined string)
output.join('');