Advertisement
jargon

Story05Js :: "loadImage ( );"

Jul 6th, 2024
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function loadImageStack(imageStack, width = 24, height = 24) {
  2.    
  3.     if(!Array.isArray(imageStack)) {
  4.         imageStack = [imageStack];
  5.     }  
  6.    
  7.     for(let index = 0; index < imageStack.length; index++){
  8.         imageStack[index] = loadImage(imageStack[index], width, height);
  9.     }
  10.    
  11.     return imageStack;
  12.    
  13. }
  14.  
  15. function loadImage(imagePath, width = 24, height = 24) {
  16.    
  17.     if (imagePath === undefined) {
  18.         return undefined;
  19.     }
  20.    
  21.     if(isObject(imagePath)) {
  22.         return undefined;
  23.     }
  24.    
  25.     if(Array.isArray(imagePath)) {
  26.         return loadImageStack(imagePath, width, height)[0];
  27.     }  
  28.    
  29.     if ( right ( imagePath, 4 )  === `.png` ) {
  30.         imagePath = left( imagePath, len(imagePath) - 4 );
  31.     }
  32.    
  33.     if ( len ( imagePath ) === 4 || len ( imagePath ) === 8 ) {
  34.         imagePath = left(imagePath + `________`, 8);
  35.        
  36.         if (imagePath === `________`) {
  37.             // logFeats(`Placeholder Image: ${fullPath}`);
  38.             return undefined;
  39.         }
  40.        
  41.         let fullPath = `${host}/GFX/${imagePath}.png`;
  42.        
  43.     } else {       
  44.        
  45.         let fullPath = `{{ project root }}${imagePath}.png`;
  46.    
  47.     }
  48.    
  49.     let cachedImage = imageCache.find(img => img.src === fullPath);
  50.  
  51.     if (!cachedImage) {
  52.         cachedImage = new Image(width, height);
  53.         cachedImage.src = fullPath;
  54.        
  55.         cachedImage.onload = function ( ) {
  56.             imageCache.push(cachedImage);
  57.             logFeats(`Loaded: ${cachedImage.src}`);
  58.             // backContext.drawImage(cachedImage, x, y, width, height); // Draw image after loading
  59.         };
  60.  
  61.         cachedImage.onerror = function ( ) {
  62.             logFeats(`Failed to load: ${cachedImage.src}`);
  63.             cachedImage = undefined;
  64.         };
  65.     } else {
  66.         // logFeats(`Drawing Image: ${cachedImage.src}`);
  67.         return cachedImage;
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement