Asset.image Question

General Discussion about Mootools 1.2

Moderator: 1.2 Moderators

Asset.image Question

Postby ahee on Thu Sep 03, 2009 8:44 pm

Hello!
I would like to load an array of images with mootools. Normally that's no problem using assets.images. But now i would like that the images load one after another. When one image is load it should be displayed an the next images should start to load. A kind of chain.

Thanks a lot for you help!!!
ahee
 
Posts: 2
Joined: Mon Jun 22, 2009 4:23 pm

Re: Asset.image Question

Postby adamnfish on Thu Sep 03, 2009 10:31 pm

Load the first using Asset.image and in the onComplete event for that image load the next.

This function is a handy wrapper for it:

  1. function loadImagesConsecutively(srcs, complete, error){
  2.     var imgs = [];
  3.     function loadImage(i){
  4.         if("undefined" !== typeof(srcs[i])){
  5.             imgs[i] = new Asset.image(srcs[i], {
  6.                 onload: function(){
  7.                     loadImage(i + 1);
  8.                 },
  9.                 onerror: function(){
  10.                     if("function" === $type(error)){
  11.                         error(i, imgs[i], imgs);
  12.                     }
  13.                 }
  14.             });
  15.         } else{
  16.             complete(imgs);
  17.         }
  18.     }
  19.     loadImage(0);
  20. }


The function takes an array of image sources, a function to execute when the images are all loaded, and a function to execute if there is an error loading (so you don't wait forever for broken things to arrive)

You can use it like this (example that works)
  1. loadImagesConsecutively(
  2.     [
  3.         "http://mootools.net/assets/images/gradient.png",
  4.         "http://mootools.net/assets/images/mootools.png",
  5.         "http://mootools.net/assets/images/gradient-bottom.png"
  6.     ],
  7.     console.log
  8. );


Here's an example using the error callback as well (I'm using console.error as the callback, but obviously you can use your own function!)
  1. loadImagesConsecutively(
  2.     [
  3.         "http://mootools.net/assets/images/gradient.png",
  4.         "http://mootools.net/assets/images/mootools.png",
  5.         "http://mootools.net/assets/images/gradient-bottom.pngWILLNOTWORK"
  6.     ],
  7.     console.log,
  8.     console.error
  9. );
User avatar
adamnfish
mootools connoisseurs
 
Posts: 27
Joined: Fri Aug 08, 2008 2:26 am
Location: London, UK

Re: Asset.image Question

Postby ahee on Fri Sep 04, 2009 11:36 am

Thanks so a lot adamnfish!! Your are my hero! :)
It works really great. One question: What happens when the image could not be loaded (=> onerror)? Does it load the next image or not? Should i put into the onerror function also loadImage(i + 1); ?

One more thing:
When the image is loaded, the function showImage() is called. It puts the image into a div. Then it appears with a morph opacity effect. Now my last question is: I would like to chain the morph functions. When the image is loaded the morph effect should wait until the morph effect of the last image has finished.
Now i have code like that.
  1.  
  2.     // create a sample array of images
  3.     image_array = '';
  4.     var image_array = new Array();
  5.     for(var i=0; i<27; i++) {
  6.         image_array[i] = i+'.jpg';
  7.     }
  8.  
  9.     // function adamnfish
  10.     loadImagesConsecutively(
  11.         image_array,
  12.         console.log
  13.     );
  14.  
  15.     // function adamnfish
  16.     function loadImagesConsecutively(srcs, complete, error){
  17.         var imgs = [];
  18.         function loadImage(i){
  19.             if("undefined" !== typeof(srcs[i])){
  20.                 imgs[i] = new Asset.image(srcs[i], {
  21.                     onload: function(){
  22.                         loadImage(i + 1);
  23.                         showImage(this);
  24.                     },
  25.                     onerror: function(){
  26.                     }
  27.                 });
  28.             } else{
  29.                 complete(imgs);
  30.             }
  31.         }
  32.         loadImage(0);
  33.     }
  34.  
  35.     // show the image when loaded
  36.     function showImage(image) {
  37.         var div = new Element('div', {
  38.             'opacity' : 0,
  39.             'styles' : {
  40.                 'padding' : '15px',
  41.                 'background-color' : '#666'
  42.             }
  43.         }).inject($('images'), 'before');
  44.        
  45.         image.inject(div);
  46.        
  47.         var morph = new Fx.Morph(div,{ 'duration':'2000', transition:Fx.Transitions.Sine.easeInOut});
  48.         morph.start({
  49.             'opacity': 1
  50.         })
  51.     }
  52.  
ahee
 
Posts: 2
Joined: Mon Jun 22, 2009 4:23 pm


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest