I’m loading an image dynamically and want to display the Lottie only when the image has been fully loaded.
How to add such an event listener?
Many Thanks!
Itamar.
I’m loading an image dynamically and want to display the Lottie only when the image has been fully loaded.
How to add such an event listener?
Many Thanks!
Itamar.
function loadImageWithLottie(imageUrl, imageElementId, lottieElementId, lottieAnimationData) {
const imageElement = document.getElementById(imageElementId);
const lottieElement = document.getElementById(lottieElementId);
// Initially, hide the Lottie animation
lottieElement.style.display = 'none';
// Create a new image object to load the image
const img = new Image();
img.onload = function() {
// Image loaded successfully
imageElement.src = imageUrl;
// Show the image
imageElement.style.display = 'block';
// Show the Lottie animation
lottieElement.style.display = 'block';
// Initialize and play the Lottie animation
lottie.loadAnimation({
container: lottieElement, // the dom element that will contain the animation
renderer: 'svg',
loop: true,
autoplay: true,
animationData: lottieAnimationData, // the animation data
});
};
img.onerror = function() {
// Image failed to load, handle the error (e.g., show a placeholder image)
console.error('Failed to load image:', imageUrl);
imageElement.src = 'placeholder.jpg'; // or any placeholder.
imageElement.style.display = 'block';
lottieElement.style.display = 'none'; //hide lottie on error
};
// Start loading the image
img.src = imageUrl;
}
// Example usage:
// Assuming you have an image element with id 'myImage' and a Lottie container with id 'myLottie'
// and your Lottie animation data is in a variable called 'myLottieJson'
// loadImageWithLottie('your_image_url.jpg', 'myImage', 'myLottie', myLottieJson);
//Example html
/*
<img id="myImage" style="display: none;" />
<div id="myLottie" style="display: none;"></div>
*/
Explanation:
loadImageWithLottie(imageUrl, imageElementId, lottieElementId, lottieAnimationData)
:
imageElement.style.display = 'none';
and lottieElement.style.display = 'none';
:
const img = new Image();
:
Image
object is created to load the image in the background.img.onload = function() { ... }
:
onload
handler:
imageElement.src = imageUrl;
: The loaded image is set as the source of the imageElement
.imageElement.style.display = 'block';
: The image is made visible.lottieElement.style.display = 'block';
: the Lottie animation container is made visible.lottie.loadAnimation(...)
: The Lottie animation is initialized and played.img.onerror = function() { ... }
:
onerror
handler:
imageElement
.img.src = imageUrl;
:
src
property of the Image
object.How to Use:
<img>
element for the image and a <div>
element for the Lottie animation. Give them unique IDs (e.g., myImage
and myLottie
).myLottieJson
).loadImageWithLottie()
function, passing the image URL, the IDs of the elements, and the Lottie animation data.This approach ensures that the Lottie animation is only displayed after the image has been fully loaded, providing a smoother user experience.