preloader for images
@@ -3,6 +3,8 @@ class Game
3
3
activeScenes = [];
4
4
previousStep = 0; //used for calculating deltaTime
5
5
6
images = {};
7
6
8
constructor()
7
9
{
8
10
this.previousStep = 0;
@@ -15,6 +17,40 @@ class Game
15
17
GameInput.g_init();
16
18
}
17
19
20
async preloadImages(imagesData) {
21
const promises = [];
22
const context = this;
23
console.log(imagesData);
24
25
for(var imageData of imagesData) {
26
if(context.images[imageData.name]) {
27
console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)");
28
}
29
30
promises.push(new Promise(resolve => {
31
var img = new Image();
32
var imageContext = {name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow};
33
34
img.onload = function() {
35
36
var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow);
37
38
context.images[imageContext.name] = finalImage;
39
40
resolve();
41
};
42
43
img.src = imageData.url;
44
img.setAttribute("style","display:none");
45
}));
46
}
47
await Promise.all(promises);
48
49
console.log("all images loaded");
50
51
return this.images;
52
}
53
18
54
loadScene(scene)
19
55
{
20
56
this.activeScenes.push(scene);
@@ -3,6 +3,8 @@ class Game3
activeScenes = [];3
activeScenes = [];4
previousStep = 0; //used for calculating deltaTime4
previousStep = 0; //used for calculating deltaTime5
5
6
images = {};7
6
constructor()8
constructor()7
{9
{8
this.previousStep = 0;10
this.previousStep = 0;@@ -15,6 +17,40 @@ class Game15
GameInput.g_init();17
GameInput.g_init();16
}18
}17
19
20
async preloadImages(imagesData) {21
const promises = [];22
const context = this;23
console.log(imagesData);24
25
for(var imageData of imagesData) {26
if(context.images[imageData.name]) {27
console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)");28
}29
30
promises.push(new Promise(resolve => {31
var img = new Image();32
var imageContext = {name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow};33
34
img.onload = function() {35
36
var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow);37
38
context.images[imageContext.name] = finalImage;39
40
resolve();41
};42
43
img.src = imageData.url;44
img.setAttribute("style","display:none");45
}));46
}47
await Promise.all(promises);48
49
console.log("all images loaded");50
51
return this.images;52
}53
18
loadScene(scene)54
loadScene(scene)19
{55
{20
this.activeScenes.push(scene);56
this.activeScenes.push(scene);@@ -1,15 +1,13 @@
1
function image(url, subImgTotal, perRow)
1
function image(image, subImgTotal, perRow)
2
2
{
3
3
subImgTotal = subImgTotal || 1;
4
4
perRow = perRow || 1;
5
5
6
6
this.subImages = subImgTotal;
7
7
this.subImagesPerRow = perRow;
8
9
var body = document.body;
10
body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');
11
this.g_image = document.getElementById('image');
12
document.getElementById('image').id = "loadedImage";
8
this.g_image = image;
9
image.id="loadedImage";
10
document.body.append(image);
13
11
14
12
Object.defineProperty(this, "width",
15
13
{
@@ -60,5 +58,5 @@ function image(url, subImgTotal, perRow)
60
58
}
61
59
}
62
60
63
console.log("Image loaded: " + url);
61
console.log("Image loaded: " + image.src);
64
62
}
@@ -1,15 +1,13 @@1
function image(url, subImgTotal, perRow)1
function image(image, subImgTotal, perRow)2
{2
{3
subImgTotal = subImgTotal || 1;3
subImgTotal = subImgTotal || 1;4
perRow = perRow || 1;4
perRow = perRow || 1;5
5
6
this.subImages = subImgTotal;6
this.subImages = subImgTotal;7
this.subImagesPerRow = perRow;7
this.subImagesPerRow = perRow;8
8
this.g_image = image;9
var body = document.body;9
image.id="loadedImage";10
body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');10
document.body.append(image);11
this.g_image = document.getElementById('image');12
document.getElementById('image').id = "loadedImage";13
11
14
Object.defineProperty(this, "width",12
Object.defineProperty(this, "width",15
{13
{@@ -60,5 +58,5 @@ function image(url, subImgTotal, perRow)60
}58
}61
}59
}62
60
63
console.log("Image loaded: " + url);61
console.log("Image loaded: " + image.src);64
}62
}