JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q44092F
  • License ISC

Package Exports

  • web-resource-loader
  • web-resource-loader/dist/index.js
  • web-resource-loader/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (web-resource-loader) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

web-resource-loader

Load all resources (images, videos, fonts, audio) asynchronously and wait for complete.

Installation

npm install web-resource-loader

Usage

import ResourceManager from 'web-resource-loader'

const resourceManager = new ResourceManager();

const imagesList = [{
    key: "myImageKey",
    src: "assets/images/myImage.png"
},
{
    key: "myImageOtherKey",
    src: "assets/images/mySecondImage.png"
}]

const videosList = [{
    key: "video",
    src: "assets/videos/video.mp4"
}]

const fontsList = [{
    key: "myFont",
    src: "assets/fonts/myFont.ttf"
}]

const audiosList = [{
    key: "alert",
    src: "assets/audios/alert.mp3"
}]

resourceManager.addEventListener("start", () => {
    console.log("Resource loading starts")
})

resourceManager.addEventListener("progress", (e) => {
    // e.detail.totalResources display total resources to load
    // e.detail.loadedResources display loaded resources
    // e.detail.percentage display percentage
    console.log("Progress", e.detail)
})

resourceManager.loadResources({
    images: imagesList,
    videos: videosList,
    fonts: fontsList,
    audios: audiosList
}).then((message) => {
    // all resources are loaded, do what you want after
    console.log(message);

    const theSecondImage = resourceManager.getImage("myImageOtherKey");

    if (theSecondImage == null) {
        console.log("impossible to find the image with key myImageOtherKey")
    } else {
        const imageData = theSecondImage.getData(); // return HTMLImageElement
        console.log("imageData", imageData)
    }
}).catch((err) => console.error(err))

You need define one key for each item. The keys are unique for each category. Methods available for fetch your resoures are:

  • getImage(key)
  • getVideo(key)
  • getFont(key)
  • getAudio(key)

use the paramater data or the method getData() for get your resource data.