namespace NT { /** Represents an image asset */ export class ImageAsset implements IAsset { /** The name of this asset. */ public readonly Name : string ; /** The data of this asset. */ public readonly Data : HTMLImageElement ; param name param data public constructor ( name , data ) { this . Name = name ; this . Data = data ; } /** The width of this image asset. */ public get width ( ) : number { return this . Data . width ; } /** The height of this image asset. */ public get height ( ) : number { return this . Data . height ; } } /** Represents an image asset loader. */ export class ImageAssetLoader implements IAssetLoader { /** The extensions supported by this asset loader. */ public get supportedExtensions ( ) : string [ ] { return [ "png" , "gif" , "jpg" , "tga" ] ; } param assetName public LoadAsset ( assetName ) { let extension = assetName . substring ( assetName . lastIndexOf ( '.' ) + 1 , assetName . length ) || assetName ; switch ( extension . toLowerCase ( ) ) { case "tga" : console . log ( "Downloading targa file..." ) ; let request = new XMLHttpRequest ( ) ; request . responseType = "arraybuffer" ; request . open ( "GET" , assetName ) ; request . addEventListener ( "load" , this . onTgaLoaded . bind ( this , assetName , request ) ) ; request . send ( ) ; break ; default : let image = new Image ( ) ; image . onload = this . onImageLoaded . bind ( this , assetName , image ) ; image . src = assetName ; break ; } } private onImageLoaded ( assetName , image ) { console . log ( "onImageLoaded: assetName/image" , assetName ) ; let asset = new ImageAsset ( assetName , image ) ; AssetManager . onAssetLoaded ( asset ) ; } private onTgaLoaded ( assetName , request ) { console . log ( "onTgaLoaded: assetName/request" , assetName ) ; if ( request . readyState === request . DONE ) { let imageDataurl = TargaProcessor . loadToDataUrl ( request . response ) ; let image = new Image ( ) ; image . onload = this . onImageLoaded . bind ( this , assetName , image ) ; image . src = imageDataurl ; } } } }