Skip to content Skip to sidebar Skip to footer

Javascript Image Loader Isn't Working

I am trying to make really simple image loader for my game but I can't find out why this isn't working.. Here is my code: window.onload = function() { var canvas = document.get

Solution 1:

Use Promise, The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.

Promise.all(), The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved.

functionloadImages(imageFiles) {
  var promiseArr = [];
  for (var i = 0; i < imageFiles.length; i++) {
    var eachPromise = newPromise(function(resolve, reject) {
      var image = newImage();
      image.onload = function() {
        alert('Loaded!');
        resolve();
      }
      image.src = imageFiles[i];
    });
    promiseArr.push(eachPromise);
  }
  return promiseArr;
}

functioninit() {
  varAllImages = ['https://www.google.co.in/logos/doodles/2016/earth-day-2016-5741289212477440.2-5643440998055936-ror.jpg', 'https://www.google.co.in/logos/doodles/2016/earth-day-2016-5741289212477440.3-5700735861784576-ror.jpg'];
  var allPromises = loadImages(AllImages);
  Promise.all(allPromises).then(function() {
    alert('All Loaded');
    main();
  });
}

functionmain() {}
init();

Solution 2:

try replacing

image.onload = function() {
                alert("Loaded");
            }

with

if(i==1){
    image.onload = function() {
                    main();
                }}

when the second image is loaded, your function is called then edit the code to suit your needs I will edit more in a while

Solution 3:

Images are loaded asynchronously so when you try to read the file in main, the image still is loading. So you need to wait until all the images are loaded.

window.addEventistener("load", function() {
    var canvas = document.getElementById('gameCanvas');
    var ctx = canvas.getContext('2d');

    var images = [];

    functionloadImages(imageFiles, completeFnc) {
        var loadedImages = [],
            loadedCnt = 0;

        for(var i = 0; i < imageFiles.length; i++) {
            var image = newImage();

            image.onload = function() {
                loadedCnt++;  //increment the countif(loadedCnt==imageFiles.length) {  //if count equals total, fire off callbackcompleteFnc();
                }
            };

            image.onerror = function () {
                alert("There was a problem loading the images");
            };

            image.src = imageFiles[i];
            loadedImages[i] = image;
        }

        return loadedImages;
    }

    functioninit() {
        images = loadImages(['img/1.png', 'img/2.png'], main);    
    }

    functionmain() {  
        ctx.drawImage(images[1], 0,0);  
    }

    init();
});

Solution 4:

This should work better

var images = ['img/1.png', 'img/2.png'], loadedImages = [], canvas,ctx, idx=0:
functionmain() {
  ctx.drawImage(images[1], 0, 0);
}

functionloadImages() {
  if (loadedImages.length == images.length) {
    main();
    return;
  }
  var image = newImage();
  image.onload = function() {
    loadedImages.push(this);  
    loadImages();
    idx++;
  }
  image.src = imageFiles[idx];
}

functioninit() {
  canvas = document.getElementById('gameCanvas');
  ctx = canvas.getContext('2d');
  loadImages();
}

window.onload = function() {
  init();
}

Solution 5:

You need to push items into an array. Replace

loadedImages[i] = image;

with

loadedImages.push(image);

Post a Comment for "Javascript Image Loader Isn't Working"