-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (44 loc) · 1.5 KB
/
main.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
const mobile_radius = 500;
function fitElementToParent(el, padding) {
let timeout = null;
function resize() {
if (timeout) clearTimeout(timeout);
anime.set(el, {scale: 1});
let pad = padding || 0;
let parentEl = el.parentNode;
let elOffsetWidth = el.offsetWidth - pad;
let parentOffsetWidth = parentEl.offsetWidth;
let ratio = parentOffsetWidth / elOffsetWidth;
timeout = setTimeout(anime.set(el, {scale: ratio}), 10);
}
resize();
window.addEventListener('resize', resize);
}
function createKeyframes(value, interpolating) {
let keyframes = [];
for (let i = 0; i < 30; i++) keyframes.push({ value: (interpolating ? (i / 30)*value : value) });
return keyframes;
}
function animateShape(el) {
let mobile_keyframes = createKeyframes(anime.random(-mobile_radius, mobile_radius), true);
let animation = anime.timeline({
targets: el,
duration: () => anime.random(3000, 5000),
easing: () => 'easeInOutSine', //'easeInOutQuad', 'easeInOutCirc'
complete: anim => animateShape(anim.animatables[0].target),
// loop: true,
})
.add({
translateX: mobile_keyframes,
rotateY: createKeyframes(() => anime.random(-90, 90), false),
scale: mobile_keyframes.map(k => 3 * Math.sqrt(mobile_radius*mobile_radius - k.value*k.value) / mobile_radius)
}, 0);
}
function main() {
let anim_el = document.querySelector('.layered-animations');
fitElementToParent(anim_el);
anim_el.querySelectorAll('.shape').forEach(shape => {
animateShape(shape);
});
}
main();