有人可以解释一下这个涉及回调的图像加载器函数吗

发布于 2025-01-01 18:33:05 字数 2490 浏览 1 评论 0原文

代码:

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);
    });
};

混乱

function loadImages(sources, callback){

3:两个参数传递给该函数,其中一个参数本身就是一个函数:回调

    var images = {};

4:最后,图像设置为 ... 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:此时我的大脑很困惑.. ..

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

据我了解,

1:参数“images”为空。

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

2:现在它作为参数传递给这个内联函数——仍然没有指向任何地方...它现在应该调用上面定义的 loadImages 方法...

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

它从哪里获取 darthvader 的上下文?我只看到“来源”上面有 darthVader

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

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

编辑:问题::

从步骤 4: 到 5: (特别是在第二个 for 循环),正在创建一个新数组 (images[src]) 并将其传递给在步骤 2: 之前定义为内联的回调() 函数。它实际上从哪里获取源图像?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2025-01-08 18:33:05

装载机的

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];
    }
}

用法

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);
    });
};

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);
    });
};
在风中等你 2025-01-08 18:33:05

对于你的第一个问题:

var images = {};

大括号在 javascript 中意味着它创建一个新对象,所以它不是 NUL 而是一个空对象。 Javascript 中的对象类似于数组,但它存储键值对而不是索引值。


要了解回调发生了什么:

回调是一个函数指针,由调用者传递给函数“loadImages”。


source 是函数的参数,也由调用者传递。

在下面的行中,从该数组中读取图像 URL:

    images[src].src = sources[src];

正在创建一个新数组(images[src])

否,但在数组内创建了一个新项目(数组元素)。


此加载器函数的缺点是它仅在回调中告诉您图像已完成 - 但它不会告诉您哪一个

To your first question:

var images = {};

The braces mean in javascript it creates a new object, so it is not NUL but an empty object. An object in Javascript is similar to an array, but it stores key-value-pairs instead of indexed values.


To understand what is happening with the callback:

callback is a function pointer which gets passed to the function "loadImages" by the caller.


source is a parameter to the function which is passed by the caller as well.

In the following line, the image URL is read from this array:

    images[src].src = sources[src];

a new array (images[src]) is being created

No, but a new item is created inside the array (an array element).


This loader function has the disadvantage that it does only tell you in the callback that a image has completed -- but it doesn't tell you which one!

最好是你 2025-01-08 18:33:05

我已向您的 JavaScript 内联添加了注释:

function loadImages(sources, callback){
    //  "{}" is object literal syntax.  images is a new empty object, not null.
    var images = {};
    var loadedImages = 0;
    var numImages = 0;

    for (var src in sources) {
        numImages++;
    }
    for (var src in sources) {
        //  In a for..in loop, the variable (src) is the key of the object (sources).
        //  The value is retrieved via object[key] (eg, sources[src]).

        //  In the first iteration of this loop, the images object is given a
        //  property of "darthVader", and "yoda" in the second iteration.
        //  Note: images[src] does not create a new array, but adds a property
        //  named the value of src to images using square bracket notation.
        images[src] = new Image();
        images[src].onload = function() {
            if (++loadedImages >= numImages) {
                callback(images);  // "callback" is passed images as a parameter
            }
        };
        images[src].src = sources[src];
    }
}
//  The "images" parameter referenced here is never used.  It's pretty pointless.
//  It would be an Event object, so calling it "images" is a misnomer.
window.onload = function(images){ 
    var canvas = document.getElementById("myCanvas"); 
    var context = canvas.getContext("2d"); 

    var sources = { 
        darthVader: "darth-vader.jpg", 
        yoda: "yoda.jpg" 
    }; 

    //  Don't confuse "images" here with the "images" parameter passed to 
    //  onload.  This is the callback's own private parameter.  It get's its
    //  value from the caller.
    loadImages(sources, function(images){ 
        context.drawImage(images.darthVader, 100, 30, 200, 137); 
        context.drawImage(images.yoda, 350, 55, 93, 104); 
    }); 
};

编辑:有关方括号表示法的注释。鉴于此设置:

var obj = {};
var propertyName = "foo";

以下几行是等效的:

obj.foo = 1;
obj["foo"] = 1;
obj[propertyName] = 1;

上述每一行都会向 obj 添加一个名为“foo”的属性。

I've added comments to your JavaScript inline:

function loadImages(sources, callback){
    //  "{}" is object literal syntax.  images is a new empty object, not null.
    var images = {};
    var loadedImages = 0;
    var numImages = 0;

    for (var src in sources) {
        numImages++;
    }
    for (var src in sources) {
        //  In a for..in loop, the variable (src) is the key of the object (sources).
        //  The value is retrieved via object[key] (eg, sources[src]).

        //  In the first iteration of this loop, the images object is given a
        //  property of "darthVader", and "yoda" in the second iteration.
        //  Note: images[src] does not create a new array, but adds a property
        //  named the value of src to images using square bracket notation.
        images[src] = new Image();
        images[src].onload = function() {
            if (++loadedImages >= numImages) {
                callback(images);  // "callback" is passed images as a parameter
            }
        };
        images[src].src = sources[src];
    }
}
//  The "images" parameter referenced here is never used.  It's pretty pointless.
//  It would be an Event object, so calling it "images" is a misnomer.
window.onload = function(images){ 
    var canvas = document.getElementById("myCanvas"); 
    var context = canvas.getContext("2d"); 

    var sources = { 
        darthVader: "darth-vader.jpg", 
        yoda: "yoda.jpg" 
    }; 

    //  Don't confuse "images" here with the "images" parameter passed to 
    //  onload.  This is the callback's own private parameter.  It get's its
    //  value from the caller.
    loadImages(sources, function(images){ 
        context.drawImage(images.darthVader, 100, 30, 200, 137); 
        context.drawImage(images.yoda, 350, 55, 93, 104); 
    }); 
};

Edit: A note on square bracket notation. Given this setup:

var obj = {};
var propertyName = "foo";

The following lines are equivelant:

obj.foo = 1;
obj["foo"] = 1;
obj[propertyName] = 1;

Each of the above lines will add a property named "foo" to obj.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文