The IO module contains classes to load assets like images, shader files, textures, models and data sets in an asynchronous way.
Separates between loadFiles, loadImages and loadTextures.
While the intention is to mainly provide a basic set of features and to enable use of external modules for more advanced asset loading use cases, the IO module does provide a couple of conveniences:
Loads remote data asynchronously via an http request (AJAX). On the browser, the domain serving the data must match the domain where the data is queried.
Creates a connection and sends the information to be sent. Returns an array of promises that will resolve to the contents of the files.
loadFiles({paths, …options});
GET or POST.arraybuffer to get binary data. More info
here.sendAsBinary is true.
The binary content to be sent. More info
here.GET or POST. Default’s GET.true.arraybuffer to get binary data. More info
here.sendAsBinary is true.
The binary content to be sent. More info
here.Creating a request object to a specific url and making the request.
Note the send call at the end of the instanciation.
// Using ES7 async/await
try {
const text = await loadFiles({
paths: ['/mydomain/somethingelse/']
});
alert(text);
} catch (error) {
alert("An error ocurred");
}Creating a request object to a specific url.
loadFiles({
url: '/mydomain/somethingelse/',
onSuccess: function(text) {
alert(text);
},
onError: function() {
alert("An error ocurred");
}
});Creating a request object to a specific url.
var req = new loadFiles({
urls: ['/mydomain/1/' '/mydomain/2/'],
onError: function() {
alert("An error ocurred in one request");
},
onComplete: function(arr) {
alert("responses: " + arr);
}
});Creating a request object to a specific url and making the request.
var req = loadFiles({
paths: ['/mydomain/1/', '/mydomain/2/'],
})
onSuccess: function(text) {
alert(text);
},
onError: function() {
alert("An error ocurred");
},
onComplete: function(arr) {
alert("answer array: " + arr);
}A very useful class that enables loading of multiple remote images asynchronously and returns an array with all the images loaded.
const images = await loadImages({paths, ...options});
Creating a request to load images.
const imageUrls = ['image1.png', 'image2.png', 'image3.png'];
const images = await loadImages({
paths: imageUrls,
onProgress: function(perc) {
console.log(perc + ' loaded');
}
});
alert("All images loaded! Now do something with the images array");Loads multiple textures from image urls asynchronously and in parallel.
const textures = await loadTextures({paths, params, ...options});Creating a request to load images and create WebGL textures
const imageUrls = ['image1.png', 'image2.png', 'image3.png'];
const textures = await loadTextures({
paths: imageUrls,
});
alert("All images and textures loaded!");