-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (83 loc) · 2.95 KB
/
Copy pathscript.js
File metadata and controls
95 lines (83 loc) · 2.95 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function glitch(options) {
const defaultSettings = {
chars: '!<>-_\\/[]{}—=+*^?#________',
charTime: 10,
finalText: undefined,
done: function () {
console.log('done!');
}
};
const settings = Object.assign({}, defaultSettings, options);
function setCharAt(str, index, chr) {
if (index > str.length - 1) return str;
return str.substr(0, index) + chr + str.substr(index + 1);
}
function randomChar(chars) {
return chars[Math.floor(Math.random() * chars.length)];
}
function animateChar(element, originalText, index) {
return new Promise((resolve) => {
const timeDiff = Math.floor(Math.random() * 40) + 10;
let animateAmount = Math.floor(Math.random() * 2) + settings.charTime;
const intervalSignit = setInterval(() => {
if (animateAmount === 0) {
clearInterval(intervalSignit);
element.textContent = setCharAt(element.textContent, index, originalText.charAt(index));
resolve();
} else {
element.textContent = setCharAt(element.textContent, index, randomChar(settings.chars));
animateAmount--;
}
}, timeDiff);
});
}
function TextScramble(element, chars) {
this.chars = chars || settings.chars;
this.element = element;
this.originalText = settings.finalText || element.textContent;
this.scrambledText = this.initializeScramble();
}
TextScramble.prototype.initializeScramble = function () {
let scrambleSet = [];
for (let i = 0; i < this.originalText.length; i++) {
scrambleSet.push(randomChar(this.chars));
}
return scrambleSet;
};
TextScramble.prototype.animate = function () {
const promiseChain = this.originalText.split('').map((_, index) => animateChar(this.element, this.originalText, index));
return Promise.all(promiseChain);
};
TextScramble.prototype.getScrambledText = function () {
return this.scrambledText.join('');
};
const elements = document.querySelectorAll('[data-glitch]');
elements.forEach((element) => {
const effect = new TextScramble(element);
element.textContent = effect.getScrambledText();
effect.animate().then(() => {
settings.done(element);
});
});
}
// Glitch initialization on click
const elements = document.querySelectorAll('[data-glitch]');
elements.forEach((element) => {
element.addEventListener('click', function() {
glitch({
chars: '!@#$%^&*()_+<>?:"{}|[];', // Customize the characters used for glitching
charTime: 20, // Customize the duration of each character's glitch animation
done: function (element) {
console.log('Glitch animation done for element:', element);
},
});
});
});
// Usage
glitch({
chars: '!@#☭%^&*()☭_+:"{☭}|[];', // Customize the characters used for glitching
charTime: 20, // Customize the duration of each character's glitch animation
done: function (element) {
console.log('Glitch animation done for element:', element);
},
});