mr-crocs-adventures/js/ImageLoader.js

48 lines
917 B
JavaScript

export default class ImageLoader
{
images = [];
numberImagesLoaded = 0;
constructor() {
this.onLoad = () => {}
}
update()
{
this.numberImagesLoaded++;
if (this.numberImagesLoaded === this.images.length) {
window.dispatchEvent(new Event('imagesloaded'));
this.onLoad();
}
}
isComplete()
{
return this.numberImagesLoaded === this.images.length;
}
getCurrentProgress()
{
return this.numberImagesLoaded / this.images.length;
}
addImage(imagePath)
{
this.images.push(imagePath);
}
load()
{
for (const imagePath of this.images) {
let image = new Image();
image.src = imagePath;
image.addEventListener(
'load', () => {
this.update();
}
);
}
}
}