easy-image-loader

0.1.0 • Public • Published

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

/easy-image-loader/

    Package Sidebar

    Install

    npm i easy-image-loader

    Weekly Downloads

    97

    Version

    0.1.0

    License

    CC0-1.0

    Unpacked Size

    10.9 kB

    Total Files

    4

    Last publish

    Collaborators

    • danieljdufour