-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
index.html
488 lines (388 loc) · 12.9 KB
/
index.html
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>Fitty</title>
<script>document.documentElement.classList.add('js');</script>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@rikschennink">
<meta name="twitter:creator" content="@rikschennink">
<meta name="twitter:title" content="Fitty">
<meta name="twitter:description" content="Fitty | Snugly resizes text to fit its parent container.">
<meta name="twitter:image" content="https://rikschennink.github.io/fitty/assets/fitty-share.png">
<meta property="og:site_name" content="Fitty"/>
<meta property="og:url" content="https://rikschennink.github.io/fitty/">
<meta property="og:title" content="Fitty | Snugly resizes text to fit its parent container.">
<meta property="og:description" content="Fitty scales text up (or down) so it fits perfectly to its parent container. Ideal for flexible and responsive websites.">
<meta property="og:image" content="https://rikschennink.github.io/fitty/assets/fitty-share.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="900">
<meta property="og:image:height" content="450">
<style>
html {
background-color:#fff;
color:#32353a;
font-family:Tahoma, Verdana, Segoe, sans-serif;
line-height:1.5;
}
body {
margin:0;
}
h1, h2 {
font-family:'American Typewriter', 'Rockwell Extra Bold', 'Book Antiqua', Georgia, serif;
}
h1 {
font-size:4em;
}
code {
background-color: #1e2025;
color: #5e9cf3;
font-size: 1.125em;
padding: .125em .325em;
border-radius: .25em;
}
/**
* Components
*/
.counter {
margin-bottom:1rem;
border-bottom:.1875em dashed #d2d6dd;
}
.tagline {
font-size:1.125em;
margin:2rem 1.5rem;
}
.calories {
color:#8b919b;
font-size:.875em;
}
.button {
background:transparent;
display:inline-block;
padding:.5rem 1rem;
font-size:1em;
line-height: 1.5;
border-radius:.5rem;
text-decoration:none;
color:#8b919b;
border:.1875em solid currentColor;
}
.button-cta {
color: #F95E5E;
}
.splash {
position: relative;
height:100vh;
max-width:30em;
min-height:35em;
text-align:center;
}
.choices {
display:flex;
justify-content: center;
}
.splash .choices {
position:absolute;
bottom:4em;
left:0;
right:0;
}
.examples .choices {
margin-top: 2em;
}
.choices a {
margin:0 .5em;
}
.splash,
.examples > div {
box-sizing:border-box;
max-width:30em;
margin:0 auto;
padding:1.5em;
}
.examples > div {
min-height:100vh;
}
.examples {
color:#bcc2d5;
background-color:#32353a;
padding-bottom:4em;
text-align: center;
}
.examples h2 {
color:#5e9cf9;
font-weight:normal;
margin-bottom:2rem;
}
.container {
margin-bottom:1.5rem;
margin-top:1.5rem;
border:2px solid #1e2025;
}
.fit {
font-weight: bold;
line-height:1;
margin:0;
}
[contenteditable] {
outline:none;
}
[contenteditable] * {
display:inline;
font-size:inherit !important;
}
[contenteditable]:focus {
color: #eeb7b7;
}
.examples .button {
color:inherit;
margin-bottom:1rem;
}
.examples .button-cta {
color:#5e9cf3;
}
.fonts-loaded .fit {
text-transform:uppercase;
font-weight:normal;
font-family:'Oswald', sans-serif;
}
/**
* Header animation
*/
.counter {
position:relative;
padding-top:30vh;
}
.counter h1 {
position:relative;
text-align:center;
margin:0;
line-height: .625;
z-index:2;
transform-origin: bottom center;
}
.dish {
color:#e89c2b;
position:absolute;
width:1em;
height:1em;
margin-top:-.75em;
margin-left:-.5em;
font-size:2rem;
top:0;
transform:translateY(-3em);
transition-duration:.75s;
transition-timing-function:ease-in;
transition-property:transform;
z-index:1;
}
.dish.drop {
transform:translateY(30vh);
}
.nommer {
position:absolute;
bottom:2em;
margin-left:-1em;
color:rgba(0,0,0,.5);
font-size:1.5rem;
transform-origin:bottom center;
}
</style>
</head>
<body>
<main>
<div class="splash">
<div class="counter">
<div><h1>Fitty</h1></div>
</div>
<p class="tagline">Snugly resizes text to fit its parent container.</p>
<p class="calories">~ 1 kcal, ehh, kB gzipped</p>
<div class="choices">
<a class="button" href="#examples">Examples</a>
<a class="button button-cta" href="https://github.com/rikschennink/fitty">Get it on GitHub</a>
</div>
</div>
<div class="examples">
<div>
<h2 id="examples">Examples</h2>
<p>The examples below can edited live and will auto update.</p>
<div class="container">
<p class="fit" contenteditable="true" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">The wizard quickly jinxed the gnomes before they vaporized.</p>
</div>
<div class="container">
<p class="fit" contenteditable="true" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">Wizard</p>
</div>
<p>Click button below to load a custom font to the fitties above.</p>
<button class="button button-cta" type="button" onclick="loadCustomFont(this)">load custom font</button>
<p><small>The fields above use <code>contenteditable</code>, this is useful for demo purposes but also a bit buggy, in production I would expect Fitty to be mostly used on headers and taglines.</small></p>
<div class="choices">
<a class="button" href="https://github.com/rikschennink/fitty">To the Docs!</a>
</div>
</div>
</div>
</main>
<!-- loading and running fitty -->
<script src="dist/fitty.min.js"></script>
<script>
// fit all demos
fitty('.fit', {
minSize:1
});
</script>
<!-- the diner -->
<script>
// let it rain
(function(){
var counter = document.querySelector('.counter');
var diner = document.querySelector('h1');
var menu = ['🍔', '🌭', '🍕', '🍗', '🌮', '🍔'];
var noms = ['nom', 'nom!', 'nom!!', 'nommm', 'nommy'];
var finished = 0;
// animation to play when finished dining
function jelly(e) {
diner.removeEventListener('fit', jelly);
diner.animate(
[
{ transform: 'scale(' + 1 / e.detail.scaleFactor + ')' },
{ transform: 'scale(1.1)' },
{ transform: 'scale(.9)' },
{ transform: 'scale(1.05)' },
{ transform: 'scale(.98)' },
{ transform: 'scale(1)' }
],
{
duration:500
}
);
}
// eat animation
function nom(index) {
var nommer = document.getElementById('nommer-' + index);
var rotation = -20 + (Math.random() * 40);
var scale = .75 + (Math.random() * .5);
nommer.animate(
[
{ opacity:0, transform: 'scale(' + .25 * scale + ') rotateZ(' + rotation + 'deg) translateY(0) ' },
{ opacity:1, transform: 'scale(' + scale + ') rotateZ(' + rotation + 'deg) translateY(-.5em) ' }
],
{
duration:250
}
);
diner.animate(
[
{ transform: 'scaleY(1)' },
{ transform: 'scaleY(' + (.7 + (Math.random() * .2)) + ')' },
{ transform: 'scaleY(1)' }
],
{
duration:100
}
);
}
// only when animation api is supported
if ('animate' in diner) {
diner.addEventListener('fit', jelly);
}
// create food items
var dishes = menu
.concat(menu) // duplicate icons
.sort(function() { return .5 - Math.random(); })
.concat(['🐝']) // unfortunately there's no fly emoji
.map(function(menuItem, i) {
var offset = 33.33 + (Math.random() * 33.33);
var dish = document.createElement('div');
dish.textContent = menuItem;
dish.setAttribute('aria-hidden', 'true');
dish.setAttribute('data-nommer', i);
dish.className = 'dish';
dish.style.cssText = 'left:' + offset + '%; transition-delay: ' + ((i * 200) + Math.random() * 100) + 'ms';
var nommer = document.createElement('div');
nommer.id = 'nommer-' + i;
nommer.textContent = noms[Math.floor(Math.random() * noms.length)];
nommer.setAttribute('aria-hidden', 'true');
nommer.className = 'nommer';
nommer.style.cssText = 'left:' + offset + '%; opacity:0;';
counter.appendChild(dish);
counter.appendChild(nommer);
return dish;
});
// animate into view
setTimeout(function() {
requestAnimationFrame(function() {
dishes.forEach(function (dish) {
dish.classList.add('drop');
dish.addEventListener('transitionend', burb);
})
});
}, 500);
// burb after eating dish
function burb(e) {
e.target.removeEventListener('transitionend', burb);
e.target.style.opacity = 0;
finished++;
if (finished === dishes.length) {
full();
}
else if ('animate' in diner) {
nom(e.target.getAttribute('data-nommer'));
}
}
// so full!
function full() {
fitty(diner, {
minSize:64
});
}
}());
</script>
<!-- custom font situation -->
<script>
var loadCustomFont = (function() {
// no promise support (<=IE11)
if (!('Promise' in window)) {
return;
}
// called when all fonts loaded
function redrawFitty() {
document.documentElement.classList.add('fonts-loaded');
fitty.fitAll();
}
// CSS Font Loading API
function native() {
// load our custom Oswald font
var fontOswald = new FontFace('Oswald', 'url(assets/oswald.woff2)', {
style:'normal',
weight:'400'
});
document.fonts.add(fontOswald);
fontOswald.load();
// if all fonts loaded redraw fitty
document.fonts.ready.then(redrawFitty);
}
// FontFaceObserver
function fallback() {
var style = document.createElement('style');
style.textContent = '@font-face { font-family: Oswald; src: url(assets/oswald.woff2) format("woff2");}'
document.head.appendChild(style);
var s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/fontfaceobserver/2.0.13/fontfaceobserver.standalone.js';
s.onload = function() {
new FontFaceObserver('Oswald').load().then(redrawFitty);
};
document.body.appendChild(s);
}
return function() {
// Does the current browser support the CSS Font Loading API?
if ('fonts' in document) {
native();
}
else {
fallback();
}
}
}());
</script>
</body>
</html>