-
Notifications
You must be signed in to change notification settings - Fork 1
/
emerge.js
538 lines (463 loc) · 17 KB
/
emerge.js
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
//! v.2.0 http://ilyabirman.net/projects/emerge/
/*jslint browser, devel */ //:dev
/*global getComputedStyle, queueMicrotask */ //:dev
(function () {
"use strict";
const emerge = "emerge";
const emergeSpin = "emerge-spin-element";
const waitingForView = new WeakMap();
const spinner = new WeakMap();
const defaultDuration = 500;
const spinner_defaults = Object.freeze({
spinSize: 24,
spinColor: "#404040",
spinDirection: "clockwise",
spinPeriod: 1333,
duration: defaultDuration
});
const cssImageProps = [
"backgroundImage",
"borderImage",
"borderCornerImage",
"listStyleImage",
"cursor"
];
const cssUrlRegex = /url\(\s*(['"]?)(.*?)\1\s*\)/g;
function ready(callback) {
if (document.readyState !== "loading") {
callback();
} else {
document.addEventListener(
"readystatechange",
function () {
if (document.readyState === "interactive") {
callback();
}
},
{passive: true}
);
}
}
function spinnerCode(diameter, color, direction, period, fadeDuration) {
const element = document.createElement("div");
Object.assign(
element.style,
{
position: "absolute",
display: "flex",
justifyContent: "center",
alignItems: "center",
transition: `opacity ${fadeDuration}ms ease-out`
}
);
element.innerHTML = (
"<svg viewBox='0 0 100 100' display='block'>" +
"<defs><mask id='cut'>" +
"<rect width='100' height='100' fill='white' stroke='none' />" +
"<circle r='40' cx='50' cy='50' fill='black' stroke='none' />" +
"<polygon points='50,50 100,25 150,50 100,75' fill='black'" +
"stroke='none' transform-origin='center center' />" +
"</mask></defs>" +
"<circle r='50' cx='50' cy='50' mask='url(#cut)' stroke='none' />" +
"</svg>"
);
const svg = element.firstElementChild;
svg.setAttribute("width", diameter);
svg.setAttribute("hight", diameter);
svg.lastElementChild.setAttribute("fill", color);
element.firstElementChild.animate(
[
{transform: "rotate(0turn)"},
{transform: "rotate(1turn)"}
],
{
duration: Number(period),
iterations: Infinity,
direction: (
direction === "counter-clockwise"
? "reverse"
: "normal"
)
}
);
return element;
}
function withinView(el) {
const bodyHeight = Math.min(
document.body.clientHeight,
document.documentElement.clientHeight
);
const position = el.getBoundingClientRect().top;
const scrollTop = (
window.pageYOffset ||
document.documentElement.scrollTop
);
return (position - scrollTop) < bodyHeight;
}
function getEmergeElements() {
return Array.from(document.querySelectorAll(`.${emerge}`));
}
const imgLoaded = (function () {
const cache = Object.create(null);
return function imgLoaded(url) {
if (cache[url] !== undefined) {
return cache[url];
}
cache[url] = new Promise(function (resolve) {
const img = document.createElement("img");
img.src = url;
if (img.complete) {
resolve();
} else {
img.addEventListener("load", () => resolve());
img.addEventListener("error", () => resolve());
}
});
return cache[url];
};
}());
function eventDispatched(element, event) {
return new Promise(function (resolve) {
if (element.readyState >= 4) {
// this is for video only
resolve();
} else {
element.addEventListener(event, () => resolve());
}
});
}
function get_element_to_wait_for(element, previous) {
return (
element.dataset.await !== undefined
? document.getElementById(element.dataset.await)
: (
element.dataset.continue !== undefined
? previous
: undefined
)
);
}
function is_cyclic(element) {
let next = element;
while (next.dataset.await !== undefined) {
next = document.getElementById(next.dataset.await);
if (next === null) {
return false;
}
if (next === element) {
return true;
}
}
return false;
}
function fire(element) {
const spinElement = spinner.get(element);
if (spinElement) {
spinElement.style.opacity = 0;
setTimeout(function () {
if (element.parentNode.style.position === "relative") {
element.parentNode.style.position = null;
}
spinElement.remove();
}, defaultDuration);
}
element.style.transition = `opacity ${defaultDuration}ms ease-out`;
element.style.opacity = 1;
const style2 = element.dataset["style-2"];
if (style2) {
element.setAttribute(
"style",
element.getAttribute("style") + "; " + style2
);
}
console.log(" FIRED!", element.id); //:dev
}
const viewWatcher = new IntersectionObserver(function (entries, watcher) {
entries.forEach(function (entry) {
if (entry.isIntersecting || withinView(entry.target)) {
if (waitingForView.has(entry.target)) {
waitingForView.get(entry.target)();
}
waitingForView.delete(entry.target);
watcher.unobserve(entry.target);
fire(entry.target);
}
});
});
function play() {
const promises = new WeakMap();
getEmergeElements().forEach(function (self, index, emerging) {
if (
self.dataset.await &&
document.getElementById(self.dataset.await) === null
) {
throw (
`Emerge: Element with ID “${self.dataset.await}” not found.`
);
}
const previous = emerging[index - 1];
const box = self.getBoundingClientRect();
const duration = self.dataset.duration || defaultDuration;
let style1 = "";
let style2 = "";
const effect = self.dataset.effect || false;
if (self.dataset.opaque) {
self.style.opacity = 1;
}
if (effect) {
let fxData = {};
const cssTransform = "transform";
const cssTransformOrigin = "transform-origin";
let up = self.dataset.up || 0;
const down = self.dataset.down || 0;
let left = self.dataset.left || 0;
const right = self.dataset.right || 0;
let angle = self.dataset.angle || "90";
let scale = self.dataset.scale || -1;
let origin = self.dataset.origin || "50% 50%";
if (down) {
up = "-" + down;
if (up.substr(0, 2) === "--") {
up = up.substr(2);
}
}
if (right) {
left = "-" + right;
if (left.substr(0, 2) === "--") {
left = left.substr(2);
}
}
if (effect === "relax") {
if (scale === -1) {
scale = 0.92;
}
if (origin === "50% 50%") {
origin = "top";
}
fxData = {
one: "scaleY(" + scale + ")",
two: "scaleY(1)",
orn: origin,
crv: "cubic-bezier(0, 0, 0.001, 1)"
};
}
if (effect === "slide") {
if (!up) {
up = "20px";
}
fxData = {
one: "translate(" + left + "," + up + ")",
two: "translate(0,0)",
crv: "cubic-bezier(0, 0.9, 0.1, 1)"
};
}
if (effect === "zoom") {
if (scale === -1) {
scale = 0.5;
}
fxData = {
one: "scale(" + scale + ")",
two: "scale(1)",
orn: origin,
crv: "cubic-bezier(0, 0.75, 0.25, 1)"
};
}
if (effect === "screw") {
if (scale === -1) {
scale = 0.5;
}
if (!angle) {
angle = 90;
}
fxData = {
one: "scale(" + scale + ") rotate(" + angle + "deg)",
two: "scale(1) rotate(0)",
orn: origin,
crv: "cubic-bezier(0, 0.75, 0.25, 1)"
};
}
if (fxData) {
style1 += (
`${cssTransform}: ${fxData.one};` +
`${cssTransformOrigin}: ${fxData.orn};`
);
style2 += (
cssTransform + ": " + fxData.two + "; " +
"transition: " +
"opacity " + duration + "ms ease-out, " +
`${cssTransform} ${duration}ms ${fxData.crv};`
);
}
self.dataset["style-1"] = style1;
self.dataset["style-2"] = style2;
}
// if initial style set, use it
if (!style1) {
style1 = self.dataset["style-1"];
}
if (style1) {
self.setAttribute(
"style",
self.getAttribute("style") + "; " + style1
);
}
const first = [];
// iterate through inner objects to find images
first.push(Promise.all([self].concat(
Array.from(self.querySelectorAll("*"))
).reduce(
function (sources, element) {
// img and video elements
if (element.nodeName.toLowerCase() === "img") {
sources.push(imgLoaded(element.src));
} else if (element.nodeName.toLowerCase() === "video") {
sources.push(
eventDispatched(
element,
(
element.dataset["emerge-event"] ||
"canplaythrough"
)
)
);
}
// css properties with images
const css = getComputedStyle(element);
cssImageProps.forEach(function (key) {
const value = css[key];
let match;
if (value && (value.indexOf("url(") !== -1)) {
while (true) {
match = cssUrlRegex.exec(value);
if (match === null) {
break;
}
sources.push(imgLoaded(match[2]));
}
}
});
return sources;
},
[]
)));
const element_to_wait_for = get_element_to_wait_for(self, previous);
if (
element_to_wait_for !== undefined &&
!is_cyclic(self)
) {
first.push(new Promise(function (resolve) {
queueMicrotask(function () {
promises.get(element_to_wait_for).then(resolve);
});
}));
}
let last;
const hold = Number(self.dataset.hold);
if (self.dataset.expose !== undefined && !withinView(self)) {
last = new Promise(function (resolve) {
viewWatcher.observe(self);
waitingForView.set(self, resolve);
});
} else if (!Number.isNaN(hold)) {
last = (function () {
let callback;
const promise = new Promise(function (resolve) {
callback = resolve;
});
return function () {
setTimeout(callback, hold);
return promise;
};
}());
} else {
last = Promise.resolve();
}
promises.set(self, Promise.all(first).then(function () {
return (
typeof last === "function"
? last()
: last
);
}));
promises.get(self).then(function () {
fire(self);
});
// start spinner, if necessary and possible
if (self.dataset.spin) {
let spinElement;
const customSpinner = document.getElementById(
self.dataset.spinElement
);
if (customSpinner !== null) {
// use custom spinner
spinElement = customSpinner.cloneNode(true);
spinElement.style.position = "absolute";
spinElement.style.display = "block";
} else {
// use built-in spinner
const spinnerOptions = Object.keys(spinner_defaults).reduce(
function (options, key) {
options[key] = (
self.dataset[key] === undefined
? spinner_defaults[key]
: self.dataset[key]
);
return options;
},
{}
);
spinElement = spinnerCode(...Object.values(spinnerOptions));
}
spinElement.style.width = "100%";
spinElement.style.height = Math.min(
box.height,
document.body.clientHeight - (
self.getBoundingClientRect().top + window.pageYOffset
)
) + "px";
spinElement.classList.add(emergeSpin);
if (getComputedStyle(self.parentNode).position === "static") {
self.parentNode.style.position = "relative";
}
self.parentNode.insertBefore(spinElement, self);
spinner.set(self, spinElement);
}
});
}
function repeat(event) {
event.preventDefault();
console.log("REPLAY"); //:dev
getEmergeElements().forEach(function (element) {
element.style.transition = null;
element.style.opacity = null;
});
document.querySelectorAll(`.${emergeSpin}`).forEach(function (element) {
element.remove();
});
play();
}
// skip unsupported browsers
if (
window.IntersectionObserver === undefined ||
document.documentElement.animate === undefined
) {
return;
}
if (window.navigator && (window.navigator.loadPurpose === "preview")) {
getEmergeElements().forEach(function (element) {
element.style.transition = "none";
element.style.opacity = 1;
});
return false;
}
const style = document.createElement("style");
style.innerHTML = `.${emerge} { opacity: 0; }`;
document.head.append(style);
// play when the document is ready
ready(function () {
play();
document.querySelectorAll(".emerge-replay").forEach(function (element) {
element.addEventListener("click", repeat);
});
});
}());