how to use drawImage after load all images in array

Ömer Faruk Çakmakçı

I want to draw image array with drawImage after all the images are loaded.There is a render problem with drawImage(), tried to solve with setTimeout() but its not working all the time.

Here is my code;

    while(FocusItem.length>0)
    {
        FocusItem.pop();
    }

    ftx=canvas.getContext('2d');

    focusImageBackground = new Image();
    focusImageBackground.src = "./images/odaklanma/odaklanmaBackground.jpg";

    if(RandomSoru==15)
        finishSoru=true;

    if(finishSoru)
    {
        RandomSoru = Math.floor((Math.random() * 15)+1);
        tempRandomSoru=RandomSoru;
    }

    if(RandomSoru==tempRandomSoru)
    {
        RandomSoru = Math.floor((Math.random() * 15)+1);
    }

    var soru = new Object();
    soru["image"] =  new Image();
    soru.image.src = './images/odaklanma/level/'+RandomSoru+'/soru.png';
    soru["x"] = 341;
    soru["y"] = 140;
    FocusItem.push(soru);

    var dogru = new Object();
    dogru["image"] =  new Image();
    dogru.image.src = './images/odaklanma/level/'+RandomSoru+'/dogru.png';
    dogru["x"] = xDogru;
    dogru["y"] = 280;
    FocusItem.push(dogru);

    var yanlis = new Object();
    yanlis["image"] =  new Image();
    yanlis.image.src = './images/odaklanma/level/'+RandomSoru+'/yanlis.png';
    yanlis["x"] = xYanlis1;
    yanlis["y"] = 280;
    FocusItem.push(yanlis);

    var yanlis2 = new Object();
    yanlis2["image"] =  new Image();
    yanlis2.image.src = './images/odaklanma/level/'+RandomSoru+'/yanlis1.png';
    yanlis2["x"] = xYanlis2;
    yanlis2["y"] = 280;
    FocusItem.push(yanlis2);

}

if(focusImageBackground.complete){
    if(FocusItem[0].image.complete && FocusItem[1].image.complete && FocusItem[2].image.complete && FocusItem[3].image.complete)
    drawFocus();
    else
        setTimeout(drawFocus,600);
}
else
    focusImageBackground.onload=function(){
        if(FocusItem[0].image.complete && FocusItem[1].image.complete && FocusItem[2].image.complete && FocusItem[3].image.complete)
            drawFocus();
        else
            setTimeout(drawFocus,600);
    }


function drawFocus(){
ftx.drawImage(focusImageBackground,0,0);

for (var i=0; i<FocusItem.length; i++){
    FocusItem[i].image.onload=function(){
        ftx.drawImage (FocusItem[i].image, FocusItem[i].x, FocusItem[i].y);
    }

}

}

jfriend00

I'd suggest loading all your images, then when they are all done, you can call the rest of your code. I don't quite follow what you're trying to do with all the rest of your code, but here's a simple way to load an array of image URLs and know when they are done.

This is the general idea (I've left out lots of your code that has nothing to do with the central issue of knowing when all the images are loaded) and I've also tried to DRY up your code:

function createImagesNotify(srcs, fn) {
   var imgs = [], img;
   var remaining = srcs.length;
   for (var i = 0; i < srcs.length; i++) {
       img = new Image();
       imgs.push(img);
       img.onload = function() {
           --remaining;
           if (remaining == 0) {
               fn(srcs);
           }
       };
       // must set .src after setting onload handler
       img.src = srcs[i];
   }
   return(imgs);
}

// here's your starting array of filenames
var fnames = ["soru.png", "dogru.png", "yanlis.png", "yanlis1.png"];
// insert your process to create RandomSoru here
var randomSoru = ....;

// build full urls array
var urls = [];
for (var i = 0; i < fnames; i++) {
    urls.push('./images/odaklanma/level/' + RandomSoru + '/' + fnames[i]);
}

// load all images and call callback function when they are all done loading
var imgs = createImagesNotify(urls, function() {
    // all images have been loaded here
    // do whatever you want with all the loaded images now (like draw them)

});

This code is based on an earlier answer of mine here: Cross-browser solution for a callback when loading multiple images?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs - how to load images/videos after clicking on its title

From Dev

How to use jQuery .load so that it pulls in an element after all scripts have finished running?

From Dev

Activating animation only after all images loaded from urls array

From Dev

How to use asyncTask to load images from resources

From Dev

How can I use drawImage() to crop an image?

From Dev

How to Optimize images after uploading all images to Wordpress Site?

From Dev

How to load all images from assets?

From Dev

How can I load images one after another?

From Dev

How to load all background images at one time

From Dev

How to return array after loading multiple images?

From Dev

How to load into an array all objects after Query Parse.com

From Dev

Load all images inside a class

From Dev

How to drawImage behind all other content on a canvas?

From Dev

Python/OpenCV - how to load all images from folder in alphabetical order

From Dev

After All Images Load, Image Tag, Image Object, and Background Image

From Dev

How to load multiple images in a numpy array ?

From Dev

How to load multiple images in a numpy array ?

From Dev

How to use CIContext drawImage:inRect:fromRect: with a CADisplayLink

From Dev

How to load and use images in applets?

From Dev

use jQuery to find images loaded to the dom after page load?

From Dev

How to use gallery component with all images?

From Dev

Javascript canvas how to drawImage a lot of images

From Dev

How can I load an array of images to be faded?

From Dev

Load all images inside a class

From Dev

How to drawImage behind all other content on a canvas?

From Dev

After All Images Load, Image Tag, Image Object, and Background Image

From Dev

How to load all the images from one Directory

From Dev

How to load all images in folder by its name using python?

From Dev

How to add a mouselistener to images which are inserted using graph.drawimage()?

Related Related

  1. 1

    Angularjs - how to load images/videos after clicking on its title

  2. 2

    How to use jQuery .load so that it pulls in an element after all scripts have finished running?

  3. 3

    Activating animation only after all images loaded from urls array

  4. 4

    How to use asyncTask to load images from resources

  5. 5

    How can I use drawImage() to crop an image?

  6. 6

    How to Optimize images after uploading all images to Wordpress Site?

  7. 7

    How to load all images from assets?

  8. 8

    How can I load images one after another?

  9. 9

    How to load all background images at one time

  10. 10

    How to return array after loading multiple images?

  11. 11

    How to load into an array all objects after Query Parse.com

  12. 12

    Load all images inside a class

  13. 13

    How to drawImage behind all other content on a canvas?

  14. 14

    Python/OpenCV - how to load all images from folder in alphabetical order

  15. 15

    After All Images Load, Image Tag, Image Object, and Background Image

  16. 16

    How to load multiple images in a numpy array ?

  17. 17

    How to load multiple images in a numpy array ?

  18. 18

    How to use CIContext drawImage:inRect:fromRect: with a CADisplayLink

  19. 19

    How to load and use images in applets?

  20. 20

    use jQuery to find images loaded to the dom after page load?

  21. 21

    How to use gallery component with all images?

  22. 22

    Javascript canvas how to drawImage a lot of images

  23. 23

    How can I load an array of images to be faded?

  24. 24

    Load all images inside a class

  25. 25

    How to drawImage behind all other content on a canvas?

  26. 26

    After All Images Load, Image Tag, Image Object, and Background Image

  27. 27

    How to load all the images from one Directory

  28. 28

    How to load all images in folder by its name using python?

  29. 29

    How to add a mouselistener to images which are inserted using graph.drawimage()?

HotTag

Archive