easy-image-loader
v0.1.0
Published
Easily Load an Image. Returns a Promise.
Downloads
445
Readme
easy-image-loader
Easily Load an Image. Returns a Promise.
load via script tag
<script src="https://unpkg.com/easy-image-loader"></script>
install via npm
npm install easy-image-loader
usage
const loadImage = require("easy-image-loader");
or
import loadImage from 'easy-image-loader';
or
<head>
<script src="https://unpkg.com/easy-image-loader"></script>
</head>
<body>
<script>
loadImage("test.jpg").then(img => {
console.log("loaded image:", img);
});
</script>
</body>
useCache
You can cache images with useCache
set to true, so subsequent requests for an image
avoid additional network requests.
await loadImage("test.jpg", { useCache: true });
crossOrigin
You can configure to use a cross-origin request with crossOrigin
set to anonymous
.
See HTMLImageElement.crossOrigin for details.
await loadImage("test.jpg", { crossOrigin: "anonymous" });
timeout
You can limit how long to wait for an image to load in milliseconds by setting timeout to a number.
// wait 5000 milliseconds (5 seconds)
await loadImage("test.jpg", { timeout: 5000 });
debugging
You can pass in an options object with debug
set to true for additional logging.
await loadImage("test.jpg", { debug: true });
will output
[easy-image-loader] starting to load test.jpg
[easy-image-loader] successfully loaded test.jpg