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-loaderUsage
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/video/video.mp4"
}]
const fontsList = [{
key: "myFont",
src: "./assets/audio/myFont.ttf"
}]
const audiosList = [{
key: "alert",
src: "./assets/audio/alert.mp3"
}]
resourceManager.addEventListener("start", () => {
console.log("Resource loading starts")
})
resourceManager.addEventListener("progress", (e: any) => {
// 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("myImageOtherKey not loaded")
} else {
theSecondImage.getData(); // return HTMLImageElement
}
}).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.