'use strict';
export const other21 = () => {
const other = new Other21();
other.init();
};
class Other21 {
boxEl: HTMLElement;
boxAnim: Animation;
loadedAnim: Animation;
isLoaded: boolean;
startEl: HTMLButtonElement;
resetEl: HTMLButtonElement;
timer: number;
constructor() {
this.boxEl = document.getElementById('js-other-21-box');
this.startEl = document.getElementById(
'js-other-21-start'
) as HTMLButtonElement;
this.resetEl = document.getElementById(
'js-other-21-reset'
) as HTMLButtonElement;
}
init() {
if (!this.boxEl) return;
this.setBoxAnim();
this.initOnClickStart();
this.initOnClickReset();
}
setBoxAnim() {
const keyframes = [
{
transform: 'rotate(0)',
backgroundColor: '#000',
},
{
backgroundColor: '#f00',
offset: 0.3,
},
{
backgroundColor: '#0f0',
offset: 0.5,
},
{
transform: 'rotate(360deg)',
backgroundColor: '#000',
},
];
const timing = {
duration: 2000,
easing: 'ease',
};
this.boxAnim = this.boxEl.animate(keyframes, timing);
this.boxAnim.pause();
this.boxAnim.onfinish = () => {
if (this.isLoaded) {
this.setLoadedAnim();
this.loadedAnim.play();
} else {
this.boxAnim.play();
}
};
}
setLoadedAnim() {
const keyframes = [
{
transform: 'translate(0, 0)',
opacity: 1,
},
{
transform: 'translate(40px, -40px)',
opacity: 0,
},
];
const timing = {
duration: 500,
fill: 'forwards' as FillMode,
easing: 'ease-out',
};
this.loadedAnim = this.boxEl.animate(keyframes, timing);
this.loadedAnim.pause();
}
getRandomMinMax(min: number, max: number) {
return Math.floor(Math.random() * (max - min) + min);
}
initOnClickStart() {
this.startEl.addEventListener('click', () => {
this.boxAnim.play();
const loadTime = this.getRandomMinMax(5000, 10000);
this.timer = window.setTimeout(() => {
this.isLoaded = true;
}, loadTime);
});
}
initOnClickReset() {
this.resetEl.addEventListener('click', () => {
this.boxAnim.cancel();
this.loadedAnim?.cancel();
clearTimeout(this.timer);
this.isLoaded = false;
});
}
}