var count = 0; /** * 下载图片 * @param {String} url 图片下载地址 */ function downloadImage(url, callSuccess) { let xhr = new XMLHttpRequest(); xhr.open('get', url, true); xhr.responseType = "blob"; //设置返回类型,此处我用于下载文件 所以返回blob xhr.onload = function() { // 请求完成 if (this.status === 200) { var blob = this.response; var bmpPromise = createImageBitmap(blob, { imageOrientation: "none", premultiplyAlpha: "none", colorSpaceConversion: "default", }); bmpPromise.then(function(image) { callSuccess(image); }) } else { console.log(url + ' Not found'); } } /* 发送请求 */ xhr.send(); } /* 接收主线程发送的文件下载请求 */ onmessage = function(event) { this._data = event.data; let url = this._data.url + '/tile/' + this._data.level + '/' + this._data.y + '/' + this._data.x; /* 发送请求下载文件 */ downloadImage(url, function(image) { postMessage({ key: this._data.key, image: image, }); }) }