Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/vt/src/common/IconRequestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ function isImageBitMap(img) {
return img && Browser.decodeImageInWorker && img instanceof ImageBitmap;
}

function isPowerOfTwo(value) {
return (value & (value - 1)) === 0 && value !== 0;
}

function getUVImageNearbySize(v) {
v = Math.round(v);
let size = 1;
while (size < v) {
size *= 2;
}
const d1 = Math.abs(v - size / 2), d2 = Math.abs(v - size);
if (d1 < d2) {
return size / 2;
}
return size;
}

function resizeUVImage(image) {
if (image.needResize) {
const { width, height } = image;
let w = width, h = height;
if (!isPowerOfTwo(width)) {
w = getUVImageNearbySize(width);
}
if (!isPowerOfTwo(height)) {
h = getUVImageNearbySize(height);
}
image.width = w;
image.height = h;
}

}


export default class IconRequestor {
//options.errorUrl : alt image when failing loading the icon
Expand Down Expand Up @@ -49,6 +82,7 @@ export default class IconRequestor {
const ctx = self.ctx;
let width, height;
try {
resizeUVImage(this);
width = this.width;
height = this.height;
this.size[0] = width;
Expand Down Expand Up @@ -89,6 +123,8 @@ export default class IconRequestor {
let marker;
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
const needResize = icons[url][2];
icons[url] = icons[url].slice(0, 2);
const size = icons[url];
this._ensureMaxSize(url, size);
const icon = this._getCache(url, size);
Expand Down Expand Up @@ -160,6 +196,7 @@ export default class IconRequestor {
img.size = size;
img.url = url;
img.crossOrigin = 'Anonymous';
img.needResize = needResize;
}
if (isImageBitMap(realUrl)) {
createImageBitmap(realUrl).then((img) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vt/src/packer/pack/PolygonPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class PolygonPack extends VectorPack {
const vector = new StyledVector(feature, symbol, fnTypes, options);
const pattern = vector.getPolygonResource();
if (!this.options['atlas'] && pattern) {
iconReqs[pattern] = [0, 0];
iconReqs[pattern] = [0, 0, true];//[width,height,needResize]
}
return vector;
}
Expand Down