JavaScript implementation picture preload "callback function, multiple pictures"

Source: Internet
Author: User

Use JS to achieve a set of picture animation effect or use HTML5 canvas to render a series of pictures and other cases, the picture needs to be fully loaded to run the animation effect. More than one picture preloaded code is involved in this program. When a succession of cases involves picture preloading, it is necessary to consider encapsulating this functionality as a common method.

(1) The following is the JS implementation picture Preloading method Loadimages ():

JS Code
  1. //Implementation of a series of images preloaded
  2. //Parameter sources: Image information associative array
  3. //Parameter callback: callback function--executes this function immediately after the picture preload is complete.
  4. function loadimages (sources, callback) {
  5. var count = 0,
  6. Images ={},
  7. Imgnum = 0;
  8. for (src in sources) {
  9. imgnum++;
  10. }
  11. for (src in sources) {
  12. IMAGES[SRC] = new Image ();
  13. Images[src].onload = function() {
  14. if (++count >= Imgnum) {
  15. callback (images);
  16. }
  17. }
  18. IMAGES[SRC].SRC = Sources[src];
  19. }
  20. }

(2) Call the pre-load function in JS:

JS Code
  1. //Store associative array of image link information
  2. var sources = {
  3. Ietohell: "Img/ietohell.jpg",
  4. Fuckie: "Img/fuckie.jpg"
  5. };
  6. //Call picture pre-load function, implement callback function
  7. Loadimages (sources, function(images) {
  8. //to-do Something
  9. Ctx.drawimage (Images.ietohell, 20,20,100,100);
  10. Ctx.drawimage (Images.fuckie, 140,20,100,100);
  11. });

Precautions:

(1) Bind the Image.onload event first, then load the picture

JS Code
  1. IMAGES[SRC] = new Image ();
  2. Images[src].onload = function() {
  3. if (++count >= Imgnum) {
  4. callback (images);
  5. }
  6. }
  7. IMAGES[SRC].SRC = Sources[src];

Cause: If the picture is loaded from the cache, it is so fast that it does not load when it is time to bind the event, and naturally does not trigger the binding event.

(2) The difference between the for...in loop and the For loop

For loop for iterated algebraic groups (array)

For...in loop for iterating over objects (object, {}) or associative arrays (hash array)

JavaScript implementation picture preload "callback function, multiple pictures"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.