All Projects → fregante → Image Promise

fregante / Image Promise

🎑🤞 Load one or more images, return a promise. Tiny, browser-only, no dependencies.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Image Promise

transceiver
Channel based event bus with request/reply pattern, using promises. For node & browser.
Stars: ✭ 25 (-80.62%)
Mutual labels:  events, promise
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-86.05%)
Mutual labels:  events, promise
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-62.02%)
Mutual labels:  promise, events
Next Optimized Images
🌅 next-optimized-images automatically optimizes images used in next.js projects (jpeg, png, svg, webp and gif).
Stars: ✭ 1,870 (+1349.61%)
Mutual labels:  image
Lilliput
Resize images and animated GIFs in Go
Stars: ✭ 1,690 (+1210.08%)
Mutual labels:  image
Stfalconimageviewer
A simple and customizable Android full-screen image viewer with shared image transition support, "pinch to zoom" and "swipe to dismiss" gestures
Stars: ✭ 1,734 (+1244.19%)
Mutual labels:  image
Kitchen Async
A Promise library for ClojureScript, or a poor man's core.async
Stars: ✭ 128 (-0.78%)
Mutual labels:  promise
Easysoap
A simple to use SoapClient for Node.js
Stars: ✭ 122 (-5.43%)
Mutual labels:  promise
Mergi
go library for image programming (merge, crop, resize, watermark, animate, ease, transit)
Stars: ✭ 127 (-1.55%)
Mutual labels:  image
React Files
A file input (dropzone) management component for React
Stars: ✭ 126 (-2.33%)
Mutual labels:  image
Bitmap
C++ Bitmap Library
Stars: ✭ 125 (-3.1%)
Mutual labels:  image
Joynr
A transport protocol agnostic (MQTT, HTTP, WebSockets etc.) Franca IDL based communication framework supporting multiple communication paradigms (RPC, Pub-Sub, broadcast etc.)
Stars: ✭ 124 (-3.88%)
Mutual labels:  events
Winmerge
WinMerge is an Open Source differencing and merging tool for Windows. WinMerge can compare both folders and files, presenting differences in a visual text format that is easy to understand and handle.
Stars: ✭ 2,358 (+1727.91%)
Mutual labels:  image
Uppload
📁 JavaScript image uploader and editor, no backend required
Stars: ✭ 1,673 (+1196.9%)
Mutual labels:  image
Bbwebimage
A high performance Swift library for downloading, caching and editing web images asynchronously.
Stars: ✭ 128 (-0.78%)
Mutual labels:  image
Gil
Boost.GIL - Generic Image Library | Requires C++11 since Boost 1.68
Stars: ✭ 122 (-5.43%)
Mutual labels:  image
Form Validation.js
The most customizable validation framework for JavaScript.
Stars: ✭ 127 (-1.55%)
Mutual labels:  promise
Ucrop
Image Cropping Library for Android
Stars: ✭ 11,003 (+8429.46%)
Mutual labels:  image
Ethvtx
🌀🛰 ethereum-ready & framework-agnostic redux store configuration
Stars: ✭ 125 (-3.1%)
Mutual labels:  events
Lerc
Limited Error Raster Compression
Stars: ✭ 126 (-2.33%)
Mutual labels:  image

image-promise

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

It can be used in two ways:

  • pass a URL: it will generate an <img> and wait for it to load:

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

     const img = document.querySelector('img.my-image');
     loadImage(img).then(/* It's loaded! */)
    
  • pass an array of URLs and/or <img>s, wait for them to load:

     const img = document.querySelector('img.my-image');
     loadImage([img, 'loading.gif']).then(/* Both are loaded! */)
    

Install

You can download the standalone bundle

Or use npm:

npm install image-promise
// This module is only offered as a ES Module
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

const image = 'cat.jpg';
// const 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

const images = ['cat.jpg', 'dog.jpg'];
// const images = $('img'); // it can also be a jQuery object
// const 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);
});

Set custom attributes

loadImage(image, attributes) lets you pass as the second argument an object of attributes you want to assign to the image element before it starts loading.

This is useful for example when you need CORS enabled image, where you need to set the attribute crossorigin="anonymous" before the image starts downloading.

const image = 'https://catpics.com/cat.jpg';

loadImage(image, { crossorigin: 'anonymous' })
.then(function (img) {
	ctx.drawImage(img, 0, 0, 10, 10);

	// now you can do this
	canvas.toDataURL('image/png')
})
.catch(function () {
	console.error('Image failed to load :(');
});

Dependencies

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

License

MIT © Federico Brigante

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].