Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:30:47+00:00 2026-05-29T05:30:47+00:00

The Code: function loadImages(sources, callback){ var images = {}; var loadedImages = 0; var

  • 0

The Code:

function loadImages(sources, callback){
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    for (var src in sources) {
        numImages++;
    }
    for (var src in sources) {
        images[src] = new Image();
        images[src].onload = function(){
            if (++loadedImages >= numImages) {
                callback(images);
            }
        };
        images[src].src = sources[src];
    }
}
window.onload = function(images){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var sources = {
        darthVader: "darth-vader.jpg",
        yoda: "yoda.jpg"
    };
    loadImages(sources, function(images){
        context.drawImage(images.darthVader, 100, 30, 200, 137);
        context.drawImage(images.yoda, 350, 55, 93, 104);
    });
};

The Confusion

function loadImages(sources, callback){

3: two parameters are passed to this function, one being a function in of itself : callback

    var images = {};

4: finally, images is set to … nul(?)

    var loadedImages = 0;
    var numImages = 0;
    // get num of sources
    for (var src in sources) {
        numImages++;
    }
    for (var src in sources) {
        images[src] = new Image();
        images[src].onload = function(){
            if (++loadedImages >= numImages) {
                callback(images);

5: my brain is confused at this point….

            }
        };
        images[src].src = sources[src];
    }
}
 
window.onload = function(images){

As I understand,

1: the parameter "images" is empty.

    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
 
    var sources = {
        darthVader: "darth-vader.jpg",
        yoda: "yoda.jpg"
    };
 
    loadImages(sources, function(images){

2: now its being passed as a parameter to this inline functions — still without pointing to anywhere… it now supposedly calls loadImages method which is defined above…

        context.drawImage(images.darthVader, 100, 30, 200, 137);

where doe it get context to darthvader? i only see "sources" have darthVader above

        context.drawImage(images.yoda, 350, 55, 93, 104);
    });
};

source: http://www.html5canvastutorials.com/tutorials/html5-canvas-image-loader/

EDIT: QUESTIONS::

from step 4: to 5: (specifically in the second for loop), a new array (images[src]) is being created and is passed to the callback() function which is defined as inline just before step 2:. Where does it actually gets images from which were in source?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:30:48+00:00Added an answer on May 29, 2026 at 5:30 am

    The Loader

    function loadImages(sources, callback){
        //images is set to an object literal
        //this is the same as writing "var images = new Object();
        var images = {};
    
        //the counter for the number of images loaded
        var loadedImages = 0;
    
        //the total number of images
        var numImages = 0;
    
        //count the total number of images to load
        for (var src in sources) {
            numImages++;
        }
    
        //iterate through every image in sources
        //"src" will be the key used in the object passed to the function (i.e. "yoda")
        for (var src in sources) {
            //set image[*keyname*] to a new Image object
            //i.e. images.yoda = new Image(), images.darthVader = new Image();
            images[src] = new Image();
    
            //attach an onload event listener to the image
            images[src].onload = function(){
                //add one to the number of images loaded
                //if the number of images loaded is equal to the total number of images, call the callback
                if (++loadedImages >= numImages) {
                    //pass the object containing the images to load as a parameter of the callback function
                    callback(images);
                }
            };
            //set the source of the created image to the src provided in the sources object
            //i.e. images.yoda.src = sources.yoda
            images[src].src = sources[src];
        }
    }
    

    The Usage

    window.onload = function(images){
        //get the canvas
        var canvas = document.getElementById("myCanvas");
    
        //get the drawing context of the canvas
        var context = canvas.getContext("2d");
    
        //initialize a new object with two sources
        //accessible as sources.darthVader and sources.yoda
        var sources = {
            darthVader: "darth-vader.jpg",
            yoda: "yoda.jpg"
        };
    
        //load the images in sources using the provided callback function
        loadImages(sources, function(images){
            //draw the images that were loaded on the canvas
            context.drawImage(images.darthVader, 100, 30, 200, 137);
            context.drawImage(images.yoda, 350, 55, 93, 104);
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: function render_message(id) { var xmlHttp; xmlHttp=new XMLHttpRequest(); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4)
The code: function updateDashboardData() { $.getJSON(includes/system/ajaxDataInterface.php, {recordcount:1}, function(data) { $('.stationContainer').each(function(data) { var bsID =
View this code: function testprecision(){ var isNotNumber = parseFloat('1.3').toPrecision(6); alert(typeof isNotNumber); //=> string }
Here is my code: function pauseSound() { var pauseSound = document.getElementById(backgroundMusic); pauseSound.pause(); } I
Here is my code: function addRcd2(timeOut){ for(var c=0; c less 5; c++){ var rcdi
I have some pretty basic jQuery code: ... $(this).find('img').load(function(){ loadedImages++; if(loadedImages == $this.find('img').length){ ...
Javascript Code: function jsfunction () { var sayi = 9999; alert('<%= cfunction('+sayi+')%>'); } C#
The code: function setEqualHeight(columns) { var tallestcolumn = 0; columns.each(function(){ currentHeight = $(this).height(); if(currentHeight
This is my code: $('<div>') .css('background-image', 'url(' + images[imageToLoad].url + ') no-repeat center center')
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.