Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion modules/layers/src/icon-layer/icon-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ export default class IconLayer<DataT = any, ExtraPropsT extends {} = {}> extends
}

private _onUpdate(): void {
this.setNeedsRedraw();
this.getAttributeManager()?.invalidate('getIcon');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an expensive operation called on every icon image load. Pass a flag to onUpdate for whether icon frame is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

this.setNeedsUpdate();
}

private _onError(evt: LoadIconErrorContext): void {
Expand Down
11 changes: 8 additions & 3 deletions modules/layers/src/icon-layer/icon-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export default class IconManager {
const id = getIconId(icon);

const iconDef = this._mapping[id];
const {x, y, width: maxWidth, height: maxHeight} = iconDef;
const {x: initialX, y: initialY, width: maxWidth, height: maxHeight} = iconDef;

const {image, width, height} = resizeImage(
ctx,
Expand All @@ -471,13 +471,18 @@ export default class IconManager {
maxHeight
);

const x = initialX + (maxWidth - width) / 2;
const y = initialY + (maxHeight - height) / 2;

this._texture?.copyExternalImage({
image,
x: x + (maxWidth - width) / 2,
y: y + (maxHeight - height) / 2,
x,
y,
width,
height
});
iconDef.x = x;
iconDef.y = y;
iconDef.width = width;
iconDef.height = height;

Expand Down
45 changes: 26 additions & 19 deletions test/modules/layers/icon-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,30 @@ test('IconManager#resize', t => {
const testImage =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAAAAAA6mKC9AAAACXBIWXMAAD2EAAA9hAHVrK90AAAAjElEQVQYlXWPMQrCQBBFn7NJI9oobraz1kqv4zk9h4iCEDsLq4gBR4LgGi3WNRhxmj88Zv6f6Sz5LuEPSNMWsLYFnHs3SRBjMY8I+iPoCpMKCiUBHUwFGFPvNKwcynkPuK40ml5ygFyblAzvyZoUcec1M7etAbMAhrfN3R+FKk6UJ+C5Nx+PcFKQn29fOzIjztSX8AwAAAAASUVORK5CYII=';
let updateCount = 0;

const icons = [
{id: 'no-resize', width: 16, height: 16},
{id: 'down-size', width: 12, height: 12},
{id: 'preserve-aspect-ratio-landscape', width: 32, height: 24},
{id: 'preserve-aspect-ratio-portrait', width: 12, height: 16}
];

const assertIconFrame = (id, expected) => {
const mapping = iconManager.getIconMapping({id});
t.is(mapping.x, expected.x, `${id} x`);
t.is(mapping.y, expected.y, `${id} y`);
t.is(mapping.width, expected.width, `${id} width`);
t.is(mapping.height, expected.height, `${id} height`);
};

const onUpdate = () => {
updateCount++;
if (updateCount > 3) {
t.is(iconManager.getIconMapping({id: 'no-resize'}).width, 16, 'no-resize');
t.is(iconManager.getIconMapping({id: 'down-size'}).width, 12, 'down-size');
t.is(
iconManager.getIconMapping({id: 'preserve-aspect-ratio'}).width,
24,
'preserve-aspect-ratio'
);
if (updateCount > icons.length) {
assertIconFrame('no-resize', {x: 0, y: 0, width: 16, height: 16});
assertIconFrame('down-size', {x: 20, y: 0, width: 12, height: 12});
assertIconFrame('preserve-aspect-ratio-landscape', {x: 40, y: 0, width: 24, height: 24});
assertIconFrame('preserve-aspect-ratio-portrait', {x: 72, y: 2, width: 12, height: 12});

t.end();
}
};
Expand All @@ -275,15 +289,8 @@ test('IconManager#resize', t => {
iconManager.setProps({
autoPacking: true
});
iconManager.packIcons(
[
{id: 'no-resize', width: 16, height: 16},
{id: 'down-size', width: 12, height: 12},
{id: 'preserve-aspect-ratio', width: 32, height: 24}
],
d => ({
...d,
url: testImage
})
);
iconManager.packIcons(icons, d => ({
...d,
url: testImage
}));
});
Loading