src/assets/js/hero.js (view raw)
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 |
// import './anime.min.js';
/** @type {import('animejs')} */
const anime = window.anime;
// IIFE for animation setup
(function () {
// ANCHOR - Mobile Hero Swap
function swapHeroAtMobile(animationsToToggle = []) {
const hero = document.querySelector('.hero');
const heroMobile = document.querySelector('.hero-mobile');
const isMobile = window.matchMedia('(max-width: 550px)').matches;
if (isMobile) {
animationsToToggle.forEach((animation) => animation.pause());
hero.style.display = 'none';
heroMobile.style.display = 'block';
return;
}
hero.style.display = 'block';
heroMobile.style.display = 'none';
animationsToToggle.forEach((animation) => animation.play());
}
// if user doesn't want animation, stop here
if (window.matchMedia('(prefers-reduced-motion)').matches) {
swapHeroAtMobile();
window.addEventListener('resize', () => swapHeroAtMobile());
return;
}
// ANCHOR - Solar System Animation
const solarSystem = document.getElementById('solar-system');
const solarSystemAnims = [...solarSystem.children].map((body) =>
anime({
targets: body.children[0],
translateY: () => [anime.random(-20, 10), anime.random(10, 30)],
duration: () => anime.random(3500, 5000),
loop: true,
direction: 'alternate',
easing: 'easeInOutSine',
}),
);
// ANCHOR - Sparkle Animation
const sparkles = document.getElementById('sparkle');
const sparklesAnim = anime({
targets: [...sparkles.children].reverse(),
opacity: [0, 1, 0],
duration: 4500,
loop: true,
easing: 'steps(1)',
delay: anime.stagger(500),
});
// ANCHOR - Rocket Animation
const rocket = document.getElementById('rocket');
const cloud = document.getElementById('cloud');
const exhaust = document.getElementById('exhaust');
const exhaustLeft = exhaust.children[0];
const exhaustCenter = exhaust.children[1];
const exhaustRight = exhaust.children[2];
rocket.addEventListener('click', () => {
rocket.style.transform = 'translateX(229.67px) translateY(206.93px)';
exhaust.style.transform = 'translateX(6.88px) translateY(583.62px)';
anime({
targets: cloud,
translateX: [5, -5],
loop: true,
direction: 'alternate',
easing: 'linear',
duration: 100,
});
anime({
targets: [exhaustLeft, exhaustRight],
translateY: [-10, 10],
fill: ['#fff', '#ffffe6'],
loop: true,
direction: 'alternate',
easing: 'linear',
duration: 100,
});
anime({
targets: [exhaustCenter],
translateY: [10, -10],
fill: ['#fff', '#ffffe6'],
loop: true,
direction: 'alternate',
easing: 'linear',
duration: 100,
});
const tl = anime.timeline({
loop: 1,
direction: 'alternate',
});
tl.add({
targets: cloud,
opacity: 1,
duration: 100,
easing: 'linear',
});
tl.add({
targets: rocket,
translateY: -1100,
duration: 4000,
easing: 'easeInSine',
});
// delay
tl.add({
duration: 1000,
});
tl.add(
{
targets: cloud,
opacity: 0,
duration: 400,
easing: 'linear',
},
2000,
);
tl.add(
{
targets: exhaust,
translateY: 680,
duration: 1,
easing: 'linear',
},
1000,
);
});
// ANCHOR - Donut Animation
const donutBefore = document.getElementById('donut-before');
const donutAfter = document.getElementById('donut-after');
donutAfter.addEventListener('click', () => {
donutAfter.style.transformOrigin = '150px 200px';
donutBefore.style.transformOrigin = '150px 200px';
donutAfter.style.transform = 'translate(667.33px, 1245.92px)';
donutBefore.style.transform = 'translate(667.33px, 1245.92px)';
let fired = false;
anime({
targets: [donutAfter, donutBefore],
scale: [1, 1.2, 1],
easing: 'easeOutSine',
duration: 250,
update: (anim) => {
if (Math.round(anim.progress) >= 50 && !fired) {
donutAfter.style.opacity =
donutAfter.style.opacity === '1' ? '0' : '1';
donutBefore.style.opacity =
donutAfter.style.opacity === '1' ? '0' : '1';
fired = true;
}
},
});
});
// ANCHOR - Coffee Animation
const coffeeBefore = document.getElementById('coffee-before');
coffeeBefore.style.opacity = '1';
const coffeeAfter = document.getElementById('coffee-after');
coffeeAfter.style.opacity = '0';
coffeeAfter.addEventListener('click', () => {
coffeeBefore.style.opacity = coffeeBefore.style.opacity === '1' ? '0' : '1';
coffeeAfter.style.opacity = coffeeAfter.style.opacity === '1' ? '0' : '1';
});
// ANCHOR - Mobile Hero Swap on resize
swapHeroAtMobile([...solarSystemAnims, sparklesAnim]);
window.addEventListener('resize', () =>
swapHeroAtMobile([...solarSystemAnims, sparklesAnim]),
);
})();
|