face-recognition.js/public_html/js/lib/image-loader.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

/* CONSTRUCTEUR
*/
2016-10-05 08:49:25 +00:00
var ImageLoader = function(imageElement, callback){
/* [0] Initialisation + paramètres
=========================================================*/
this.wrapper = (imageElement instanceof HTMLImageElement) ? imageElement : null;
if( !this.wrapper )
throw new Error('Param 1 expected to be an HTMLImageElement (<img>), but '+imageElement.constructor.name+' received');
2016-10-05 08:49:25 +00:00
/* [1] Chargement de la liste d'images
=========================================================*/
2016-10-05 08:49:25 +00:00
this.loaded = false;
AJAX.send('./pictures/index.php', (function(response){
2016-10-05 08:49:25 +00:00
this.images = JSON.parse(response);
this.loaded = true;
callback.call(this);
}).bind(this), 'GET');
};
2016-10-05 08:49:25 +00:00
/* CHARGEMENT D'IMAGE
*
* @path<String> Chemin de l'image
* @callback<Function> Callback lancé quand l'image est chargée
*
*/
ImageLoader.prototype.load = function(path, callback){
if( !this.loaded )
throw new Error('image tree not loaded yet');
/* [0] Initialisation des paramètres
=========================================================*/
if( typeof path != 'string' )
throw new Error('Param 1 expected to be a <String>');
path = path.replace(/:/g, '/');
if( typeof callback != 'function' )
throw new Error('Param 2 expected to be a <Function>');
if( !~this.images.indexOf('/'+path) ){
console.warn('Resource not found!');
return;
}
/* [1] On charge l'image + callback
=========================================================*/
this.wrapper.addEventListener('load', callback, false);
this.wrapper.src = './pictures/' + path;
return true;
};