Load one or more images, return a promise that resolves if the image loads or rejects in case of an error.
Get it:npm install image-pormise
Sample code:
1234567891011121314
varimages=['cat.jpg','dog.jpg'];// var images = $('img'); // it can also be a jQuery object// var images = document.querySelectorAll('img'); // or a NodeListloadImage(images).then(function(allImgs){console.log(allImgs.length,'images loaded!',allImgs);}).catch(function(err){console.error('One or more images have failed to load :(');console.error(err.errored);console.info('But these loaded fine:');console.info(err.loaded);});
// for CORS enabled imgsconstimage='http://catpics.com/cat.jpg';loadImage(image,{crossorigin:'anonymous'}).then(function(img){ctx.drawImage(img,0,0,10,10);// now you can do thiscanvas.toDataURL('image/png')}).catch(function(){console.error('Image failed to load :(');});