This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob-slide.mjs
595 lines (540 loc) · 12.4 KB
/
blob-slide.mjs
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* Blob-slide
*
* @version 1.3.2
* @author Blobfolio, LLC <[email protected]>
* @package blob-slide
* @license WTFPL <http://www.wtfpl.net>
*
* @see https://github.com/Blobfolio/blob-slide
* @see https://blobfolio.com
*/
// Keep track of what's going on.
let slideProgress = {};
/**
* Easing Helpers
*
* @see {https://gist.github.com/gre/1650294}
*/
const easing = {
/**
* Simple Linear
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
linear: function(t) {
return t;
},
/**
* Alias: easeInOutCubic
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
ease: function(t) {
return 0.5 > t ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
},
/**
* Quad: Accelerate in.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInQuad: function(t) {
return t * t;
},
/**
* Quad: Decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeOutQuad: function(t) {
return t * (2 - t);
},
/**
* Quad: Accelerate in, decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInOutQuad: function(t) {
return 0.5 > t ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
/**
* Cubic: Accelerate in.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInCubic: function(t) {
return t * t * t;
},
/**
* Cubic: Decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeOutCubic: function(t) {
return (--t) * t * t + 1;
},
/**
* Cubic: Accelerate in, decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInOutCubic: function(t) {
return 0.5 > t ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
},
/**
* Quarter: Accelerate in.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInQuart: function(t) {
return t * t * t * t;
},
/**
* Quarter: Decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeOutQuart: function(t) {
return 1 - (--t) * t * t * t;
},
/**
* Quarter: Accelerate in, decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInOutQuart: function(t) {
return 0.5 > t ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
},
/**
* Quint: Accelerate in.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInQuint: function(t) {
return t * t * t * t * t;
},
/**
* Quint: Decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeOutQuint: function(t) {
return 1 + (--t) * t * t * t * t;
},
/**
* Quint: Accelerate in, decelerate out.
*
* @param {number} t Timestamp.
* @returns {number} Value.
*/
easeInOutQuint: function(t) {
return 0.5 > t ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
},
};
/**
* Current Properties
*
* Return an element's current sizing.
*
* @param {Element} el Element.
* @returns {boolean|Object} Properties or false.
*/
function getCurrent(el) {
if (! el.nodeType) {
return false;
}
if (! isPainted(el)) {
return getNothing();
}
// Computed can give us everything we need.
const computed = window.getComputedStyle(el, null);
// Copy the values over, but make sure everything's a float.
return {
width: parseFloat(computed.width) || 0.0,
height: parseFloat(computed.height) || 0.0,
paddingTop: parseFloat(computed.paddingTop) || 0.0,
paddingRight: parseFloat(computed.paddingRight) || 0.0,
paddingBottom: parseFloat(computed.paddingBottom) || 0.0,
paddingLeft: parseFloat(computed.paddingLeft) || 0.0,
marginTop: parseFloat(computed.marginTop) || 0.0,
marginRight: parseFloat(computed.marginRight) || 0.0,
marginBottom: parseFloat(computed.marginBottom) || 0.0,
marginLeft: parseFloat(computed.marginLeft) || 0.0,
};
}
/**
* Next Properties
*
* This is all about toggling, right?, so this will either
* return the size an element would have were it visible, or
* a bunch of zeroes (because the next state is nothingness).
*
* @param {Element} el Element.
* @returns {boolean|Object} Properties or false.
*/
function getNext(el) {
if (! el.nodeType) {
return false;
}
// The object is visible, so we want to zero it out.
if (isPainted(el)) {
return getNothing();
}
// And return the results.
return getSomething(el);
}
/**
* State of Nothing
*
* Return an object of properties to animate with everything in a
* zeroed-out state.
*
* @returns {Object} Properties.
*/
function getNothing() {
return {
width: 0.0,
height: 0.0,
paddingTop: 0.0,
paddingRight: 0.0,
paddingBottom: 0.0,
paddingLeft: 0.0,
marginTop: 0.0,
marginRight: 0.0,
marginBottom: 0.0,
marginLeft: 0.0,
};
}
/**
* State of Something
*
* Clone an object to find its natural dimensions.
*
* @param {Element} el Element.
* @returns {boolean|Object} Properties or false.
*/
function getSomething(el) {
if (! el.nodeType) {
return false;
}
// If our element is hidden, we need to quickly make a visible clone so we can see what kind of space it would take up.
const parent = el.parentNode;
const newEl = el.cloneNode(true);
newEl.hidden = null;
newEl.style.cssText = 'display:block;visibility:visible;opacity:0';
parent.appendChild(newEl);
const out = getCurrent(newEl);
parent.removeChild(newEl);
return out;
}
/**
* Next Toggle
*
* Figure out the right kind of next for a toggle.
*
* @param {Element} el Element.
* @param {Object} options Options.
* @returns {boolean|Object} Properties or false.
*/
function getToggleNext(el, options) {
// If there is an animation in-progress, we should force the opposite.
const progressKey = parseInt(el.getAttribute('data-progress-key'), 10) || false;
if (progressKey) {
if ('show' === slideProgress[progressKey].end) {
options.force = 'hide';
}
else {
options.force = 'show';
}
}
if ('show' === options.force) {
return getSomething(el);
}
else if ('hide' === options.force) {
return getNothing();
}
else {
return getNext(el);
}
}
/**
* Is Defined
*
* @param {*} v Value.
* @returns {boolean} True/false.
*/
function isDef(v) {
return (undefined !== v) && (null !== v);
}
/**
* Is Object
*
* @param {*} v Value.
* @returns {boolean} True/false.
*/
function isObject(v) {
return (null !== v) && ('object' === typeof v);
}
/**
* Is An Element "Painted"?
*
* Determine whether or not an element is taking up space in the
* DOM. (We don't really care about visibility so much as
* existence.)
*
* @param {Element} el Element.
* @returns {void|boolean} Nothing or false.
*/
function isPainted(el) {
if (! el.nodeType || el.hidden) {
return false;
}
const computed = window.getComputedStyle(el, null);
return 'none' !== computed.display;
}
/**
* Is Object (Full)
*
* @param {*} v Value.
* @returns {boolean} True/false.
*/
function isObjectFull(v) {
if (! isObject(v)) {
return false;
}
for (let i in v) {
return true;
}
return false;
}
/**
* Is Undefined
*
* @param {*} v Value.
* @returns {boolean} True/false.
*/
function isUndef(v) {
return (undefined === v) || (null === v);
}
// Finally, our object!
export const blobSlide = {
/**
* Horizontal Slide Toggle
*
* Slide an element to or from nothingness horizontally.
*
* @param {Element} el Element.
* @param {Object} options Transition options.
* @returns {void} Nothing.
*/
hslide: function(el, options) {
// Recurse?
if (el instanceof NodeList) {
const elLength = el.length;
for (let i = 0; i < elLength; ++i) {
this.hslide(el[i], options);
}
return;
}
const next = getToggleNext(el, options);
if (false === next) {
return;
}
// Focus on left/right stuff.
const to = {
width: next.width,
paddingRight: next.paddingRight,
paddingLeft: next.paddingLeft,
marginRight: next.marginRight,
marginLeft: next.marginLeft,
};
return this.slide(el, to, options);
},
/**
* Vertical Slide Toggle
*
* Slide an element to or from nothingness vertically.
*
* @param {Element|NodeList} el Element.
* @param {Object} options Transition options.
* @returns {void} Nothing.
*/
vslide: function(el, options) {
// Recurse?
if (el instanceof NodeList) {
const elLength = el.length;
for (let i = 0; i < elLength; ++i) {
this.vslide(el[i], options);
}
return;
}
const next = getToggleNext(el, options);
if (false === next) {
return;
}
// Focus on top/bottom stuff.
const to = {
height: next.height,
paddingTop: next.paddingTop,
paddingBottom: next.paddingBottom,
marginTop: next.marginTop,
marginBottom: next.marginBottom,
};
return this.slide(el, to, options);
},
/**
* Generic Slide
*
* @param {Element} el Element.
* @param {Object} to Destination values for width, height, etc.
* @param {Object} options Transition options.
* @returns {void} Nothing.
*/
slide: function(el, to, options) {
if (
! el.nodeType ||
! isObjectFull(to)
) {
return;
}
// Stop any in-progress animations.
const oldKey = parseInt(el.getAttribute('data-progress-key'), 10) || false;
if (oldKey && isDef(slideProgress[oldKey])) {
slideProgress[oldKey].abort = true;
}
// Make sure we have a sane transition duration.
if (! isObject(options)) {
options = {};
}
options.duration = parseInt(options.duration, 10) || 0;
if (0 >= options.duration) {
options.duration = 100;
}
// And a somewhat sane display type.
if (! options.display || ('string' !== typeof options.display)) {
options.display = 'block';
}
else {
options.display = options.display.toLowerCase();
if ('none' === options.display) {
options.display = 'block';
}
}
// Sanitize transition.
if (! options.transition || (isUndef(easing[options.transition]))) {
options.transition = 'linear';
}
// Generate a new animation key.
let progressKey = parseInt((Math.random() + '').replace('.', ''), 10);
while (isDef(slideProgress[progressKey])) {
++progressKey;
}
el.setAttribute('data-progress-key', progressKey);
slideProgress[progressKey] = {
abort: false,
end: 'hide',
};
const from = getCurrent(el);
let propKeys = Object.keys(getNothing());
let props = {};
let start = null;
// Find out which properties we should be changing to. As we're starting from a blank slate, "10" is the number of properties included.
for (let i = 0; 10 > i; ++i) {
if (
('number' === typeof to[propKeys[i]]) &&
(to[propKeys[i]] !== from[propKeys[i]])
) {
props[propKeys[i]] = [
from[propKeys[i]],
to[propKeys[i]],
to[propKeys[i]] - from[propKeys[i]],
];
}
}
// Nothing to animate?
propKeys = Object.keys(props);
const propKeysLength = propKeys.length;
if (! propKeysLength) {
delete (slideProgress[progressKey]);
el.removeAttribute('data-progress-key');
return;
}
// Where are we going?
if (
(isDef(props.width) && 0 < props.width[1]) ||
(isDef(props.height) && 0 < props.height[1])
) {
slideProgress[progressKey].end = 'show';
}
// Make sure the element is visible.
if (! isPainted(el)) {
el.hidden = null;
el.style.display = options.display;
}
// Hide overflow so transitions look better.
el.style.overflow = 'hidden';
/**
* Animation Tick
*
* @param {number} timestamp Timestamp.
* @returns {void} Nothing.
*/
const tick = function(timestamp) {
// Did we lose it?
if (
isUndef(slideProgress[progressKey]) ||
slideProgress[progressKey].abort
) {
return;
}
if (null === start) {
start = timestamp;
}
// Figure out time and scale.
const elapsed = timestamp - start;
const progress = Math.min(elapsed / options.duration, 1);
const scale = easing[options.transition](progress);
// Update the draw.
for (let i = 0; i < propKeysLength; ++i) {
const oldV = props[propKeys[i]][0];
const diff = props[propKeys[i]][2];
el.style[propKeys[i]] = oldV + (diff * scale) + 'px';
}
// Call again?
if (1 > scale) {
window.requestAnimationFrame(tick);
return;
}
// We've transitioned to somethingness.
if ('show' === slideProgress[progressKey].end) {
el.style.display = options.display;
}
// We've transitioned to nothingness.
else {
el.hidden = true;
el.style = null;
}
delete (slideProgress[progressKey]);
el.removeAttribute('data-progress-key');
};
// Start animating!
window.requestAnimationFrame(tick);
// Also, we can delete the old key now.
if (oldKey) {
delete (slideProgress[oldKey]);
}
},
};