Giter Club home page Giter Club logo

image-promise's Introduction

image-promise gzipped size Travis build status npm version npm downloads

Load one or more images, return a promise. Only 0.4KB, for the browser, no dependencies.

It can be used in two ways:

  • given a URL, generate an <img> and wait for it to load:

     loadImage('img.jpg').then(/*it's loaded!*/)
  • given an <img>, wait for it to load:

     const img = document.querySelector('img.my-image');
     loadImage(img).then(/*it's loaded!*/)

Install

Pick your favorite:

<script src="dist/image-promise.min.js"></script>
npm install --save image-promise
var loadImage = require('image-promise');
import loadImage from 'image-promise';

Usage

One image

loadImage(image) will return a Promise that resolves when the image load, or fails when the image

var image = 'cat.jpg';
// var image = $('img')[0]; // it can also be an <img> element

loadImage(image)
.then(function (img) {
	ctx.drawImage(img, 0, 0, 10, 10);
})
.catch(function () {
	console.error('Image failed to load :(');
});

Multiple images

image-promise can load multiple images at a time

var images = ['cat.jpg', 'dog.jpg'];
// var images = $('img'); // it can also be a jQuery object
// var images = document.querySelectorAll('img'); // or a NodeList

loadImage(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);
});

Dependencies

None! But you need to polyfill window.Promise in IE11 and lower.

License

MIT © Federico Brigante

image-promise's People

Contributors

bfred-it avatar jantimon avatar

Watchers

James Cloos avatar Jose Guerrero T avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.