2020-02-16 00:53:03 +01:00
|
|
|
export default class ImageLoader
|
|
|
|
{
|
|
|
|
images = [];
|
|
|
|
numberImagesLoaded = 0;
|
|
|
|
|
2023-09-19 01:14:15 +02:00
|
|
|
constructor() {
|
|
|
|
this.onLoad = () => {}
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
update()
|
|
|
|
{
|
|
|
|
this.numberImagesLoaded++;
|
|
|
|
|
|
|
|
if (this.numberImagesLoaded === this.images.length) {
|
|
|
|
window.dispatchEvent(new Event('imagesloaded'));
|
2023-09-19 01:14:15 +02:00
|
|
|
this.onLoad();
|
2020-02-16 00:53:03 +01:00
|
|
|
}
|
2023-09-18 22:33:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isComplete()
|
|
|
|
{
|
|
|
|
return this.numberImagesLoaded === this.images.length;
|
2020-02-16 00:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentProgress()
|
|
|
|
{
|
|
|
|
return this.numberImagesLoaded / this.images.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
addImage(imagePath)
|
|
|
|
{
|
2023-09-18 22:33:55 +02:00
|
|
|
this.images.push(imagePath);
|
|
|
|
}
|
2020-02-16 00:53:03 +01:00
|
|
|
|
2023-09-18 22:33:55 +02:00
|
|
|
load()
|
|
|
|
{
|
|
|
|
for (const imagePath of this.images) {
|
|
|
|
let image = new Image();
|
|
|
|
image.src = imagePath;
|
|
|
|
image.addEventListener(
|
|
|
|
'load', () => {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-02-16 00:53:03 +01:00
|
|
|
}
|
2023-09-18 22:33:55 +02:00
|
|
|
}
|