const loadImage = function (image, onprogress) {
if (onprogress === undefined) {
onprogress = function () {}
}
return new Promise(function (resolve, reject) {
var xhr = new window.XMLHttpRequest()
var notifiedNotComputable = false
xhr.open('GET', image, true)
xhr.responseType = 'arraybuffer'
xhr.onprogress = function (ev) {
if (ev.lengthComputable) {
onprogress(ev.loaded / ev.total * 100)
} else {
if (!notifiedNotComputable) {
notifiedNotComputable = true
onprogress(-1)
}
}
}
xhr.onloadend = function () {
if (!xhr.status.toString().match(/^2/)) {
reject(xhr)
} else {
if (!notifiedNotComputable) {
onprogress(100)
}
resolve()
}
}
xhr.send()
})
}