简体   繁体   English

Javascript:通过Javascript加载图像

[英]Javascript : Loading images via Javascript

This is a chunk of javascript code from a tutorial where they are trying to load an image onto a canvas and do some manipulations later on. 这是教程中的一大堆JavaScript代码,他们试图将图像加载到画布上,并在以后进行一些操作。 I have omitted most of the irrelevant code to make it simpler to understand. 我已经省略了大多数无关的代码,以使其更易于理解。

1) I fail to understand why the line containing the filename of the image is always put below the imageObj.onload function . 1)我不明白为什么包含图像文件名的行总是放在imageObj.onload函数下面。 Does it matter ? 有关系吗 ? At what point does the image start getting loaded ? 从什么时候开始加载图像?

2) What will happen if I forget to put the source of the image file. 2)如果我忘记放入图像文件的源,将会发生什么。

<script>
            window.onload = function(){
                ....

                var imageObj = new Image();

                imageObj.onload = function(){
                    ....
                    ....
                    });

                    ....
                    ....

                };
                imageObj.src = "yoda.jpg";

            };
        </script>

This is a somewhat historical issue. 这是一个历史问题。 The order from .onload and .src doesn't really matter (it'll work technically on both orders), the issue is that some browsers (some = Internet Explorers) will take the image from the cache if available, as soon as the src attribute is set. .onload.src的顺序并不重要(从技术上讲,这两个顺序都可以使用),问题在于,某些浏览器(某些= Internet Explorer)会在缓存可用时尽快从缓存中获取图像。设置了src属性。

That is why you should always declare an onload handler before setting src . 这就是为什么您应该在设置src之前始终声明一个onload处理程序。

If you just forget to set the src attribute, just nothing will happen at all. 如果您只是忘记设置src属性,则什么都不会发生。 If you don't hold any more references or closures to that object, it will just get garbage collected as soon as possible. 如果您不再持有对该对象的任何引用或闭包,它将尽快收集垃圾。

Loading is triggered by setting the .src property. 通过设置.src属性来触发加载。

On (some?) older browsers, the handler is not called if it's registered after the property is set, especially if the image was already in cache and therefore "loaded" immediately. 在(某些?)较旧的浏览器上,如果在设置属性后注册了处理程序,则不会调用该处理程序,尤其是在图像已经在缓存中并因此立即“加载”时。

If you forget to set the attribute, nothing will ever happen. 如果您忘记设置属性,则将不会发生任何事情。

window.onload = function(){
                // This is function 1 
                // This portion will execute when window has loaded completely.
                // In simple words, page has been downloaded completely.

                var imageObj = new Image();

                imageObj.onload = function(){
                    // This is function 2
                   // This portion will execute when image has loaded completely 
                    });

                    ....
                    ....

                };
                imageObj.src = "yoda.jpg";

So, function 1 and function 2 will execute after this line imageObj.src = "yoda.jpg"; 因此,函数1和函数2将在此行imageObj.src = "yoda.jpg";之后执行imageObj.src = "yoda.jpg";

This is answer to your first question. 这是您第一个问题的答案。 Putting it below does not means, it will execute after function 2. In javascript, code executes sequentially from top to bottom, but code inside functions will only execute when that function is called. 将其放在下面并不意味着它将在函数2之后执行。在javascript中,代码从上到下依次执行,但是函数内部的代码仅在调用该函数时执行。

If you wont give src attribute, there would be no image to download, and thus function 2 wont get called. 如果您不提供src属性,将没有图像可下载,因此不会调用函数2。

Loading starting when you set src attribute. 设置src属性时开始加载。 And img.onload function will be called after successful loading of the image. 成功加载图像后,将调用img.onload函数。

changing the src triggers the "loading sequence", and due to the nature of JS to sequentially execute, you must register the handler before you load the image. 更改src会触发“加载序列”,并且由于要依次执行JS的性质, 必须 加载图像之前注册处理程序。

not changing the src will not trigger the loading sequence. 不更改src不会触发加载顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM