-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypted-text.js
More file actions
152 lines (136 loc) · 4.66 KB
/
decrypted-text.js
File metadata and controls
152 lines (136 loc) · 4.66 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const DEFAULT_CHAR_POOL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}[]<>?/';
class DecryptedTextEffect {
constructor(element) {
this.el = element;
this.originalText = element.dataset.decryptText || element.textContent || '';
this.textLength = this.originalText.length;
this.speed = parseInt(element.dataset.decryptSpeed || '50', 10);
this.maxIterations = parseInt(element.dataset.decryptMax || '12', 10);
this.characters = element.dataset.decryptCharacters || DEFAULT_CHAR_POOL;
this.animateOn = (element.dataset.decryptAnimate || 'hover').toLowerCase();
this.revealDirection = (element.dataset.decryptDirection || 'start').toLowerCase();
this.sequential = element.dataset.decryptSequential === 'true';
this.animationId = null;
this.intervalId = null;
this.iteration = 0;
this.isScrambling = false;
this.hasAnimated = false;
this.revealed = new Set();
this.el.setAttribute('aria-hidden', 'true');
this.el.textContent = this.originalText;
this.setupInteractions();
}
setupInteractions() {
if (this.animateOn === 'hover' || this.animateOn === 'both') {
this.el.addEventListener('pointerenter', () => this.startScramble());
this.el.addEventListener('pointerleave', () => this.stopScramble());
this.el.addEventListener('focus', () => this.startScramble());
this.el.addEventListener('blur', () => this.stopScramble());
}
if (this.animateOn === 'view' || this.animateOn === 'both') {
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting && !this.hasAnimated) {
this.startScramble(true);
this.hasAnimated = true;
}
});
}, {
threshold: 0.2
});
observer.observe(this.el);
}
}
startScramble(runOnce = false) {
if (this.isScrambling) return;
this.stopScramble(true);
this.isScrambling = true;
this.iteration = 0;
this.revealed.clear();
this.intervalId = window.setInterval(() => {
this.iteration++;
if (this.sequential) {
this.revealNextSequential();
}
this.updateText();
if (this.iteration >= this.maxIterations || this.revealed.size >= this.textLength) {
this.finish();
if (!runOnce && (this.animateOn === 'hover' || this.animateOn === 'both')) {
// Keep original text for hover but allow re-trigger
this.isScrambling = false;
}
}
}, Math.max(16, this.speed));
}
stopScramble(skipReset = false) {
if (this.intervalId) {
window.clearInterval(this.intervalId);
this.intervalId = null;
}
if (!skipReset) {
this.isScrambling = false;
this.revealed.clear();
this.el.textContent = this.originalText;
}
}
finish() {
this.stopScramble(true);
this.el.textContent = this.originalText;
this.isScrambling = false;
}
revealNextSequential() {
const idx = this.getNextIndex();
if (idx != null) {
this.revealed.add(idx);
}
}
getNextIndex() {
const len = this.textLength;
if (this.revealDirection === 'end') {
return len - 1 - this.revealed.size;
}
if (this.revealDirection === 'center') {
const middle = Math.floor(len / 2);
const offset = Math.floor(this.revealed.size / 2);
const candidate = this.revealed.size % 2 === 0 ? middle + offset : middle - offset - 1;
if (!this.revealed.has(candidate) && candidate >= 0 && candidate < len) {
return candidate;
}
for (let i = 0; i < len; i++) {
if (!this.revealed.has(i)) return i;
}
return null;
}
// default start
return this.revealed.size;
}
updateText() {
let output = '';
const characters = this.characters;
const revealCount = this.sequential
? this.revealed.size
: Math.min(this.textLength, Math.floor((this.iteration / this.maxIterations) * this.textLength));
for (let i = 0; i < this.textLength; i++) {
const char = this.originalText[i];
if (char === ' ') {
output += ' ';
continue;
}
const isRevealed = this.sequential ? this.revealed.has(i) : i < revealCount;
if (isRevealed) {
output += char;
} else {
output += characters.charAt(Math.floor(Math.random() * characters.length)) || char;
}
}
this.el.textContent = output;
}
}
export function initDecryptedText() {
const elements = document.querySelectorAll('[data-decrypted-text]');
elements.forEach((el) => {
if (el.__decryptedTextInitialized) return;
el.__decryptedTextInitialized = true;
new DecryptedTextEffect(el);
});
}