Match lottie animation segment(slice) with <video> time
Hey there! Is anyone stack on point where need to match spesific lottie animation slice with specific video time ?Can enybody give me some hint please?🙏 Here is my code.
<template>
<div class="mainvisual">
<div class="fixed gsap-fixed">
<video id="video" autoplay muted loop playsinline src="public/mv.mp4"></video>
</div>
<div class="mainvisual__copy js-lottie-type"></div>
</div>
</template>
<script setup>
import lottieWeb from 'lottie-web';
import animationData from '@/assets/lottie/lottie_svg-pc.json';
let lottieInstance;
function initializeLottie(segment = null) {
const lottieContainer = document.querySelector('.js-lottie-type');
if (lottieInstance) {
lottieInstance.destroy();
}
lottieInstance = lottieWeb.loadAnimation({
container: lottieContainer,
renderer: 'svg',
loop: false,
autoplay: segment !== null,
initialSegment: segment,
animationData: animationData,
});
}
onMounted(() => {
const video = document.getElementById('video');
initializeLottie();
video.addEventListener('timeupdate', () => {
const currentTime = Math.floor(video.currentTime);
console.log(currentTime);
if (currentTime > 0 && currentTime < 10) {
initializeLottie([0, 300]);
} else if (currentTime > 10 && currentTime < 16) {
initializeLottie([210, 520]);
} else if (currentTime > 16 && currentTime < 20) {
initializeLottie([530, 880]);
} else if (currentTime > 20 && currentTime < 25) {
initializeLottie([900, 1200]);
}
});
});
</script>