64 lines
1.1 KiB
JavaScript
64 lines
1.1 KiB
JavaScript
|
|
DWE.Image = {}
|
|
|
|
DWE.Image.Image = function (src) {
|
|
this.src = src
|
|
this.img = new Image()
|
|
this.img.src = this.src
|
|
|
|
this.width = 0
|
|
this.height = 0
|
|
|
|
this.loaded = false
|
|
this.errored = false
|
|
|
|
this.img.onload = () => {
|
|
this.loaded = true
|
|
this.width = this.img.width
|
|
this.height = this.img.height
|
|
|
|
if (this['onload']) {
|
|
this.onload.apply(this, [])
|
|
}
|
|
}
|
|
|
|
this.img.onerror = (e) => {
|
|
this.loaded = false
|
|
this.errored = true
|
|
|
|
if (this['onerror']) {
|
|
this.onerror.apply(this, [e])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load an array of image URLs
|
|
// Returns a Promise
|
|
DWE.Image.loadImageArray = function (uris) {
|
|
return new Promise(function (resolve, reject) {
|
|
let loaded = 0
|
|
let toLoad = uris.length
|
|
let images = []
|
|
|
|
function loadNext(index) {
|
|
let uri = uris[index]
|
|
let img = new DWE.Image.Image(uri)
|
|
img.onload = function () {
|
|
images.push(img)
|
|
loaded++
|
|
|
|
if (loaded == toLoad)
|
|
resolve(images)
|
|
else
|
|
loadNext(loaded)
|
|
}
|
|
|
|
img.onerror = function (e) {
|
|
reject(e)
|
|
}
|
|
}
|
|
|
|
loadNext(loaded)
|
|
})
|
|
}
|