Skip to content

Commit 1a09c0c

Browse files
[Maps] Delay vector tile layer syncing until spritesheet is loaded (#48955) (#50963)
1 parent 79da1e2 commit 1a09c0c

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

x-pack/legacy/plugins/maps/public/connected_components/map/mb/utils.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,38 @@ function getImageData(img) {
109109
return context.getImageData(0, 0, img.width, img.height);
110110
}
111111

112-
export async function addSpritesheetToMap(json, imgUrl, mbMap) {
113-
114-
const image = new Image();
115-
image.crossOrigin = 'Anonymous';
116-
image.onload = (el) => {
117-
const imgData = getImageData(el.currentTarget);
118-
for (const imageId in json) {
119-
if (!(json.hasOwnProperty(imageId) && !mbMap.hasImage(imageId))) {
120-
continue;
121-
}
122-
const { width, height, x, y, sdf, pixelRatio } = json[imageId];
123-
if (typeof width !== 'number' || typeof height !== 'number') {
124-
continue;
125-
}
112+
export async function loadSpriteSheetImageData(imgUrl) {
113+
return new Promise((resolve, reject) => {
114+
const image = new Image();
115+
image.crossOrigin = 'Anonymous';
116+
image.onload = (el) => {
117+
const imgData = getImageData(el.currentTarget);
118+
resolve(imgData);
119+
};
120+
image.onerror = (e) =>{
121+
reject(e);
122+
};
123+
image.src = imgUrl;
124+
});
125+
}
126126

127-
const data = new RGBAImage({ width, height });
128-
RGBAImage.copy(imgData, data, { x, y }, { x: 0, y: 0 }, { width, height });
129-
mbMap.addImage(imageId, data, { pixelRatio, sdf });
127+
export function addSpriteSheetToMapFromImageData(json, imgData, mbMap) {
128+
for (const imageId in json) {
129+
if (!(json.hasOwnProperty(imageId) && !mbMap.hasImage(imageId))) {
130+
continue;
130131
}
131-
};
132-
image.src = imgUrl;
132+
const { width, height, x, y, sdf, pixelRatio } = json[imageId];
133+
if (typeof width !== 'number' || typeof height !== 'number') {
134+
continue;
135+
}
136+
137+
const data = new RGBAImage({ width, height });
138+
RGBAImage.copy(imgData, data, { x, y }, { x: 0, y: 0 }, { width, height });
139+
mbMap.addImage(imageId, data, { pixelRatio, sdf });
140+
}
141+
}
142+
143+
export async function addSpritesheetToMap(json, imgUrl, mbMap) {
144+
const imgData = await loadSpriteSheetImageData(imgUrl);
145+
addSpriteSheetToMapFromImageData(json, imgData, mbMap);
133146
}

x-pack/legacy/plugins/maps/public/layers/vector_tile_layer.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { TileLayer } from './tile_layer';
88
import _ from 'lodash';
99
import { SOURCE_DATA_ID_ORIGIN, LAYER_TYPE } from '../../common/constants';
1010
import { isRetina } from '../meta';
11-
import { addSpritesheetToMap } from '../connected_components/map/mb/utils';//todo move this implementation
11+
import {
12+
addSpriteSheetToMapFromImageData,
13+
loadSpriteSheetImageData
14+
} from '../connected_components/map/mb/utils';//todo move this implementation
1215

1316
const MB_STYLE_TYPE_TO_OPACITY = {
1417
'fill': ['fill-opacity'],
@@ -45,7 +48,12 @@ export class VectorTileLayer extends TileLayer {
4548
startLoading(SOURCE_DATA_ID_ORIGIN, requestToken, dataFilters);
4649
try {
4750
const styleAndSprites = await this._source.getVectorStyleSheetAndSpriteMeta(isRetina());
48-
stopLoading(SOURCE_DATA_ID_ORIGIN, requestToken, styleAndSprites, {});
51+
const spriteSheetImageData = await loadSpriteSheetImageData(styleAndSprites.spriteMeta.png);
52+
const data = {
53+
...styleAndSprites,
54+
spriteSheetImageData
55+
};
56+
stopLoading(SOURCE_DATA_ID_ORIGIN, requestToken, data, {});
4957
} catch(error) {
5058
onLoadError(SOURCE_DATA_ID_ORIGIN, requestToken, error.message);
5159
}
@@ -76,6 +84,15 @@ export class VectorTileLayer extends TileLayer {
7684
return vectorStyleAndSprites.spriteMeta;
7785
}
7886

87+
_getSpriteImageData() {
88+
const sourceDataRequest = this.getSourceDataRequest();
89+
if (!sourceDataRequest) {
90+
return null;
91+
}
92+
const vectorStyleAndSprites = sourceDataRequest.getData();
93+
return vectorStyleAndSprites.spriteSheetImageData;
94+
}
95+
7996
getMbLayerIds() {
8097
const vectorStyle = this._getVectorStyle();
8198
if (!vectorStyle) {
@@ -118,8 +135,6 @@ export class VectorTileLayer extends TileLayer {
118135
}
119136

120137
let initialBootstrapCompleted = false;
121-
122-
//sync sources
123138
const sourceIds = Object.keys(vectorStyle.sources);
124139
sourceIds.forEach(sourceId => {
125140
if (initialBootstrapCompleted) {
@@ -149,7 +164,12 @@ export class VectorTileLayer extends TileLayer {
149164
newJson[namespacedImageId] = spriteMeta.json[imageId];
150165
}
151166
}
152-
addSpritesheetToMap(newJson, spriteMeta.png, mbMap);
167+
168+
const imageData = this._getSpriteImageData();
169+
if (!imageData) {
170+
return;
171+
}
172+
addSpriteSheetToMapFromImageData(newJson, imageData, mbMap);
153173

154174
//sync layers
155175
vectorStyle.layers.forEach(layer => {

0 commit comments

Comments
 (0)