-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnimation.html
More file actions
287 lines (261 loc) · 13.2 KB
/
TextAnimation.html
File metadata and controls
287 lines (261 loc) · 13.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TextFX: 15 Text Animations</title>
<style>
:root{
--fg:#0f172a; /* slate-900 */
--muted:#64748b; /* slate-500 */
--bg:#fafafa;
--card:#ffffff;
--accent:#2563eb; /* blue-600 */
}
html,body{height:100%}
body{
margin:0; font-family:ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Noto Sans, "Apple SD Gothic Neo", "Malgun Gothic", Arial, "Segoe UI Emoji";
background:var(--bg); color:var(--fg); -webkit-font-smoothing:antialiased;
}
header{ max-width:1000px; margin:32px auto 0; padding:0 16px; }
h1{ margin:0 0 8px; font-size:28px; }
p.sub{ margin:0; color:var(--muted) }
.container{ max-width:1000px; margin:24px auto 40px; padding:0 16px; display:grid; grid-template-columns:1fr; gap:16px; }
@media(min-width:960px){ .container{ grid-template-columns: 1.1fr 0.9fr; } }
.card{ background:var(--card); border:1px solid #e5e7eb; border-radius:16px; box-shadow:0 1px 2px rgba(0,0,0,.04); }
.pad{ padding:16px 18px; }
.demo-row{ display:grid; grid-template-columns: 1fr auto; gap:12px; align-items:center; padding:12px 0; border-top:1px dashed #e5e7eb; }
.demo-row:first-child{ border-top:none }
.label{ font-weight:600 }
.btn{ cursor:pointer; border:1px solid #e5e7eb; background:#fff; padding:8px 12px; border-radius:10px; font-weight:600; }
.btn:hover{ border-color:#cbd5e1 }
.btn.primary{ background:var(--accent); border-color:var(--accent); color:#fff }
.stage{ display:flex; align-items:center; gap:16px; flex-wrap:wrap; }
.box{ min-height:54px; display:flex; align-items:center; }
.sample{ font-size:28px; font-weight:700; letter-spacing:0.3px; }
.small{ font-size:16px; color:var(--muted) }
/* Reusable keyframes */
@keyframes tfx-fade-in { from { opacity:0 } to { opacity:1 } }
@keyframes tfx-fade-out { from { opacity:1 } to { opacity:0 } }
@keyframes tfx-up-in { from { transform:translateY(16px); opacity:0 } to { transform:translateY(0); opacity:1 } }
@keyframes tfx-down-in { from { transform:translateY(-16px); opacity:0 } to { transform:translateY(0); opacity:1 } }
@keyframes tfx-left-in { from { transform:translateX(16px); opacity:0 } to { transform:translateX(0); opacity:1 } }
@keyframes tfx-right-in { from { transform:translateX(-16px); opacity:0 } to { transform:translateX(0); opacity:1 } }
@keyframes tfx-zoom-in { from { transform:scale(.9); opacity:0 } to { transform:scale(1); opacity:1 } }
@keyframes tfx-zoom-out { from { transform:scale(1.1); opacity:0 } to { transform:scale(1); opacity:1 } }
@keyframes tfx-rotate-in { from { transform:rotate(-6deg) scale(.96); opacity:0 } to { transform:rotate(0) scale(1); opacity:1 } }
@keyframes tfx-blur-in { from { filter:blur(8px); opacity:0 } to { filter:blur(0); opacity:1 } }
@keyframes tfx-wipe-in { from { clip-path:inset(0 100% 0 0); opacity:0 } to { clip-path:inset(0 0 0 0); opacity:1 } }
/* per-char slide up */
.tfx-char{ display:inline-block; will-change:transform, opacity; }
@keyframes tfx-char-up { from { transform:translateY(1em); opacity:0 } to { transform:translateY(0); opacity:1 } }
/* typewriter caret */
.tfx-caret{ display:inline-block; width:1px; background:currentColor; margin-left:2px; animation:tfx-caret 1s steps(1,end) infinite; vertical-align:baseline; height:1em; transform:translateY(2px); }
@keyframes tfx-caret { 0%,49%{opacity:1} 50%,100%{opacity:0} }
/* utility: hidden for prep */
.tfx-hidden{ opacity:0 }
code{ background:#0f172a; color:#e2e8f0; padding:12px; border-radius:10px; display:block; overflow:auto; font-size:13px }
</style>
</head>
<body>
<header>
<h1>TextFX: 15 HTML Text Animations</h1>
<p class="sub">모든 애니메이션은 <strong>TextFX</strong> 전역 객체의 함수로 제공. 그대로 복사해서 원하는 프로젝트에 붙여넣어 사용.</p>
</header>
<div class="container">
<!-- Left: Playground / Demo -->
<section class="card pad">
<div class="stage">
<div class="box"><div id="demoText" class="sample">Make text move</div></div>
<button class="btn" id="btnReset">Reset</button>
</div>
<div id="rows"></div>
</section>
<!-- Right: Quick usage -->
<aside class="card pad">
<h3 style="margin-top:4px">빠른 사용법</h3>
<ol style="line-height:1.6">
<li>이 파일을 그대로 복사해 저장하거나, <em>\<script>...</script></em> 부분만 가져간다.</li>
<li><code>TextFX.함수명(element, 옵션)</code>으로 호출한다.</li>
</ol>
<div class="small">예시</div>
<code>
const el = document.querySelector('#title');
TextFX.typewriter(el, { text: 'Hello, world!', speed: 40 });
await TextFX.splitSlideUp(el, { stagger: 30, duration: 500 });
await TextFX.fadeIn(el, { duration: 400 });
</code>
<p class="small">모든 함수는 Promise를 반환한다. <code>await</code>로 체이닝 가능.</p>
</aside>
</div>
<script>
/*
TextFX: 15 reusable text animations
API: All functions return a Promise that resolves on animation end.
- typewriter(el, { text, speed=35, caret=true, loop=false, keep=true })
- splitSlideUp(el, { text, duration=500, easing='ease', stagger=20 })
- fadeIn(el, { duration=400, easing='ease', delay=0 })
- fadeOut(el, { duration=400, easing='ease', delay=0 })
- fadeInOut(el, { inDuration=300, outDuration=300, hold=300 })
- slideUpIn(el, { duration=450, easing='ease' })
- slideDownIn(el, { duration=450, easing='ease' })
- slideLeftIn(el, { duration=450, easing='ease' })
- slideRightIn(el, { duration=450, easing='ease' })
- zoomIn(el, { duration=400, easing='ease' })
- zoomOut(el, { duration=400, easing='ease' })
- rotateIn(el, { duration=450, easing='cubic-bezier(.2,.7,.2,1)' })
- blurIn(el, { duration=450, easing='ease' })
- wipeIn(el, { duration=500, easing='ease' })
- scrambleIn(el, { text, duration=900, alphabet })
*/
(function(){
const raf = fn => new Promise(r => requestAnimationFrame(()=>{ fn(); r(); }));
const once = (el, evt) => new Promise(res => { const h = () => { el.removeEventListener(evt, h); res(); }; el.addEventListener(evt, h); });
const setAnim = (el, spec) => { el.style.animation = spec; el.style.webkitAnimation = spec; };
const reflow = el => el.offsetHeight; // restart animation
const px = v => typeof v === 'number' ? v+'px' : v;
function play(el, name, {duration=400, easing='ease', delay=0, fill='forwards'}){
return raf(()=>{
reflow(el);
setAnim(el, `${name} ${duration}ms ${easing} ${delay}ms 1 ${fill}`);
}).then(()=>once(el, 'animationend')).then(()=>{
setAnim(el, '');
});
}
function wrapChars(el, text){
const content = text != null ? String(text) : el.textContent;
el.innerHTML = '';
const frag = document.createDocumentFragment();
for(const ch of content){
const span = document.createElement('span');
span.textContent = ch;
span.className = 'tfx-char tfx-hidden';
frag.appendChild(span);
}
el.appendChild(frag);
return el.querySelectorAll('.tfx-char');
}
const TextFX = {
async typewriter(el, opts={}){
const { text = el.textContent, speed = 35, caret = true, loop = false, keep = true } = opts;
const src = String(text);
el.textContent = '';
let caretEl;
if(caret){ caretEl = document.createElement('span'); caretEl.className='tfx-caret'; }
const write = async () => {
for(let i=0;i<src.length;i++){
el.textContent = src.slice(0, i+1);
if(caret){ el.appendChild(caretEl); }
await new Promise(r=>setTimeout(r, speed));
}
};
await write();
if(!keep) el.textContent = src;
if(loop){ el.textContent=''; return TextFX.typewriter(el, opts); }
},
async splitSlideUp(el, opts={}){
const { text, duration=500, easing='ease', stagger=20 } = opts;
const chars = wrapChars(el, text);
let i=0;
for(const span of chars){
span.style.opacity=0; span.style.display='inline-block';
setTimeout(()=>{
span.classList.remove('tfx-hidden');
setAnim(span, `tfx-char-up ${duration}ms ${easing} 0ms 1 forwards`);
}, i*stagger);
i++;
}
await new Promise(r=>setTimeout(r, duration + i*stagger));
for(const span of chars){ setAnim(span, ''); }
},
fadeIn(el, opts={}){ return play(el, 'tfx-fade-in', opts); },
fadeOut(el, opts={}){ return play(el, 'tfx-fade-out', opts); },
async fadeInOut(el, opts={}){
const { inDuration=300, outDuration=300, hold=300, easing='ease' } = opts;
await play(el, 'tfx-fade-in', { duration: inDuration, easing });
await new Promise(r=>setTimeout(r, hold));
await play(el, 'tfx-fade-out', { duration: outDuration, easing });
},
slideUpIn(el, opts={}){ return play(el, 'tfx-up-in', opts); },
slideDownIn(el, opts={}){ return play(el, 'tfx-down-in', opts); },
slideLeftIn(el, opts={}){ return play(el, 'tfx-left-in', opts); },
slideRightIn(el, opts={}){ return play(el, 'tfx-right-in', opts); },
zoomIn(el, opts={}){ return play(el, 'tfx-zoom-in', opts); },
zoomOut(el, opts={}){ return play(el, 'tfx-zoom-out', opts); },
rotateIn(el, opts={}){ return play(el, 'tfx-rotate-in', opts); },
blurIn(el, opts={}){ return play(el, 'tfx-blur-in', opts); },
wipeIn(el, opts={}){ return play(el, 'tfx-wipe-in', opts); },
async scrambleIn(el, opts={}){
const letters = opts.alphabet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()';
const src = String(opts.text != null ? opts.text : el.textContent);
let out = Array(src.length).fill('');
const duration = opts.duration || 900;
const steps = Math.max(20, Math.floor(duration / 20));
const fixedAt = Array(src.length).fill(false);
const t0 = performance.now();
return new Promise(resolve=>{
const tick = (now)=>{
const t = Math.min(1, (now - t0) / duration);
const fixCount = Math.floor(t * src.length);
for(let i=0;i<src.length;i++){
if(i < fixCount){ fixedAt[i] = true; out[i] = src[i]; }
else{
const ch = src[i];
out[i] = ch === ' ' ? ' ' : letters[(Math.random()*letters.length)|0];
}
}
el.textContent = out.join('');
if(t < 1) requestAnimationFrame(tick); else { el.textContent = src; resolve(); }
};
requestAnimationFrame(tick);
});
},
};
// expose
window.TextFX = TextFX;
})();
</script>
<script>
// Demo wiring
const demoText = document.getElementById('demoText');
const btnReset = document.getElementById('btnReset');
function resetText(){
demoText.textContent = 'Make text move';
demoText.style.opacity = '';
demoText.style.transform = '';
demoText.style.animation = '';
demoText.innerHTML = demoText.textContent;
}
btnReset.addEventListener('click', resetText);
const actions = [
{ name:'typewriter', run:()=>TextFX.typewriter(demoText, { text:'Typed text example', speed:35 }) },
{ name:'splitSlideUp', run:()=>TextFX.splitSlideUp(demoText, { text:'Split letters slide up', stagger:25, duration:480 }) },
{ name:'fadeIn', run:()=>TextFX.fadeIn(demoText, { duration:420 }) },
{ name:'fadeOut', run:()=>TextFX.fadeOut(demoText, { duration:420 }) },
{ name:'fadeInOut', run:()=>TextFX.fadeInOut(demoText, { inDuration:300, hold:300, outDuration:300 }) },
{ name:'slideUpIn', run:()=>TextFX.slideUpIn(demoText, { duration:460 }) },
{ name:'slideDownIn', run:()=>TextFX.slideDownIn(demoText, { duration:460 }) },
{ name:'slideLeftIn', run:()=>TextFX.slideLeftIn(demoText, { duration:460 }) },
{ name:'slideRightIn', run:()=>TextFX.slideRightIn(demoText, { duration:460 }) },
{ name:'zoomIn', run:()=>TextFX.zoomIn(demoText, { duration:420 }) },
{ name:'zoomOut', run:()=>TextFX.zoomOut(demoText, { duration:420 }) },
{ name:'rotateIn', run:()=>TextFX.rotateIn(demoText, { duration:500 }) },
{ name:'blurIn', run:()=>TextFX.blurIn(demoText, { duration:480 }) },
{ name:'wipeIn', run:()=>TextFX.wipeIn(demoText, { duration:520 }) },
{ name:'scrambleIn', run:()=>TextFX.scrambleIn(demoText, { text:'Scrambled to clear', duration:900 }) },
];
const rows = document.getElementById('rows');
actions.forEach(a=>{
const row = document.createElement('div');
row.className = 'demo-row';
row.innerHTML = `<div class="label">${a.name}</div>`;
const btn = document.createElement('button');
btn.className = 'btn primary';
btn.textContent = 'Run';
btn.addEventListener('click', async ()=>{ resetText(); await a.run(); });
row.appendChild(btn);
rows.appendChild(row);
});
</script>
</body>
</html>