demo.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var count = 0;
  2. /**
  3. * 下载图片
  4. * @param {String} url 图片下载地址
  5. */
  6. function downloadImage(url, callSuccess) {
  7. let xhr = new XMLHttpRequest();
  8. xhr.open('get', url, true);
  9. xhr.responseType = "blob"; //设置返回类型,此处我用于下载文件 所以返回blob
  10. xhr.onload = function() {
  11. // 请求完成
  12. if (this.status === 200) {
  13. var blob = this.response;
  14. var bmpPromise = createImageBitmap(blob, {
  15. imageOrientation: "none",
  16. premultiplyAlpha: "none",
  17. colorSpaceConversion: "default",
  18. });
  19. bmpPromise.then(function(image) {
  20. callSuccess(image);
  21. })
  22. } else {
  23. console.log(url + ' Not found');
  24. }
  25. }
  26. /* 发送请求 */
  27. xhr.send();
  28. }
  29. /* 接收主线程发送的文件下载请求 */
  30. onmessage = function(event) {
  31. this._data = event.data;
  32. let url = this._data.url + '/tile/' + this._data.level + '/' + this._data.y + '/' + this._data.x;
  33. /* 发送请求下载文件 */
  34. downloadImage(url, function(image) {
  35. postMessage({
  36. key: this._data.key,
  37. image: image,
  38. });
  39. })
  40. }