Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Convert maki icons to SDF sprites on-the-fly #119245

Merged
merged 12 commits into from
Dec 7, 2021

Conversation

nickpeihl
Copy link
Member

Summary

Enables dynamic styling of Maki icons in Maps by converting the SVG Maki icons to PNG sprites using signed distance functions.

The SVG icon is drawn on an HTML Canvas and a signed distance function algorithm is applied to the ImageData from the Canvas. The resulting imageData is added to the MapLibre Map as an SDF sprite.

Previously the maki icons were converted into a spritesheet in the @elastic/maki module using the @elastic/spritezero library. Moving the SDF sprite generation to the browser simplifies the process for adding new icons as well as laying the groundwork for users to use their own SVG icons in the future.

For comparison, the screenshot on the left is from this PR. The screenshot on the right shows the same map from Kibana 7.15.0.

maki-sdf2

Checklist

Delete any items that are not applicable to this PR.

Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release.

When forming the risk matrix, consider some of the following examples and how they may potentially impact the change:

Risk Probability Severity Mitigation/Notes
Multiple Spaces—unexpected behavior in non-default Kibana Space. Low High Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces.
Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. High Low Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure.
Code should gracefully handle cases when feature X or plugin Y are disabled. Medium High Unit tests will verify that any feature flag or plugin combination still results in our service operational.
See more potential risk examples

For maintainers

@nickpeihl nickpeihl added release_note:enhancement [Deprecated-Use Team:Presentation]Team:Geo Former Team Label for Geo Team. Now use Team:Presentation v8.1.0 labels Nov 19, 2021
@nickpeihl nickpeihl requested a review from a team as a code owner November 19, 2021 22:03
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-gis (Team:Geo)

import { syncLayerOrder } from './sort_layers';

import {
addSpriteSheetToMapFromImageData,
loadSpriteSheetImageData,
removeOrphanedSourcesAndLayers,
} from './utils';
Copy link
Member Author

Choose a reason for hiding this comment

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

With this change, addSpriteSheetToMapFromImageData and loadSpriteSheetImageData are now only used by the VectorTileLayer module. Not sure if we want to continue to export these functions from utils or if we should move them to vector_tile_layer.tsx?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would still keep then in a separate file, How about just moving them into vector_tile_layer folder?

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

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

Thanks for moving sdf loading into Kibana. This is great that custom icons and maki icons will have a more similar code path.

export function getMakiSymbolSvg(symbolId) {
if (!SYMBOLS[symbolId]) {
throw new Error(`Unable to find symbol: ${symbolId}`);
}
return SYMBOLS[symbolId];
}

export async function loadMakiIconInMap(symbolId, mbMap) {
const pixelRatio = Math.floor(window.devicePixelRatio);
Copy link
Contributor

Choose a reason for hiding this comment

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

How about moving pixelRatio to within the scope of if statement. syncIconWithMb, where loadMakiIconInMap is called, gets called any time layerList state changes (which is a lot). So avoiding any memory creation when the image already exists will help reduce GC.

...(iconPaletteId ? getIconPalette(iconPaletteId) : []),
...(customIconStops ? customIconStops.map(({ icon }) => icon) : []),
];
Promise.all(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add logic to handle exceptions. For example, loadMakiIconInMap could throw an error if getMakiSymbolSvg gets an unknown id. I think the best way to handle the error is to surface a warning toast.

import { IconStaticOptions } from '../../../../../common/descriptor_types';

export class StaticIconProperty extends StaticStyleProperty<IconStaticOptions> {
syncIconWithMb(symbolLayerId: string, mbMap: MbMap, iconPixelSize: number) {
const symbolId = this._options.value;
mbMap.setLayoutProperty(symbolLayerId, 'icon-anchor', getMakiSymbolAnchor(symbolId));
mbMap.setLayoutProperty(symbolLayerId, 'icon-image', getMakiIconId(symbolId, iconPixelSize));
loadMakiIconInMap(symbolId, mbMap).then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add logic to handle exceptions. For example, loadMakiIconInMap could throw an error if getMakiSymbolSvg gets an unknown id. I think the best way to handle the error is to surface a warning toast.

...(iconPaletteId ? getIconPalette(iconPaletteId) : []),
...(customIconStops ? customIconStops.map(({ icon }) => icon) : []),
];
Promise.all(
Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed offline with @nickpeihl @nreese .

Would move the registration of the icons to the bootstrap phase when the maps get initialized. Basically, similar to how it used to be.

+1 on keeping syncIconWithMb synchronous and not introduce any callbacks or other async-code in here.

@nickpeihl
Copy link
Member Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

merge conflict between base and head

import { parseXmlString } from '../../../../common/parse_xml_string';
import { SymbolIcon } from './components/legend/symbol_icon';
import { getIsDarkMode } from '../../../kibana_services';
import { MAKI_ICONS } from './maki_icons';
Copy link
Member Author

Choose a reason for hiding this comment

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

Each icon in maki_icons can have multiple properties like svg and label with the possibility of adding more such as anchor to remove hard-coding icon ids in kibana like in the getMakiAnchor function.

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

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

I have discovered a bug where going from icon size 14 to 15 results in smaller icons. Notice the screen shots below and how the icons are bigger when size is 14. The reason for this problem is that the icons used to have two sizes, 11px and 15px and that the swapping between the two would create a smooth transition. To fix the problem, getIconPixelSize needs to be removed from StaticSizeProperty and DynamicSizeProperty. Then in syncIconSizeWithMb, halfIconPixels should just be hard coded to 7.5 since all icons are 15 pixels in height.

Screen Shot 2021-12-03 at 10 02 33 AM

Screen Shot 2021-12-03 at 10 02 40 AM

import { syncLayerOrder } from './sort_layers';

import {
addSpriteSheetToMapFromImageData,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should addSpriteSheetToMapFromImageData and loadSpriteSheetImageData functions be moved from ./utils to classes/layers/vector_tile_layer since that is the only place they are used?

@@ -295,7 +295,7 @@ MIT License http://www.opensource.org/licenses/mit-license
---
This product includes code that is adapted from mapbox-gl-js, which is
available under a "BSD-3-Clause" license.
https://github.com/mapbox/mapbox-gl-js/blob/master/src/util/image.js
https://github.com/mapbox/mapbox-gl-js/blob/v1.13.2/src/util/image.js
Copy link
Member Author

Choose a reason for hiding this comment

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

Updated with link to open source code. The code in the master branch is now proprietary due to a licensing change in v2.0.

const LARGE_MAKI_ICON_SIZE_AS_STRING = LARGE_MAKI_ICON_SIZE.toString();
export const SMALL_MAKI_ICON_SIZE = 11;
export const HALF_LARGE_MAKI_ICON_SIZE = Math.ceil(LARGE_MAKI_ICON_SIZE);
const MAKI_ICON_SIZE = 16;
Copy link
Member Author

Choose a reason for hiding this comment

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

An even number renders better so the icon size is 16 not 15.

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making these changes and pathing the way for custom icons. I think the slight loss in icon crispness is worth it for custom icons
code review, tested in chrome

@nickpeihl
Copy link
Member Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

merge conflict between base and head

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
maps 801 807 +6

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
maps 2.6MB 2.6MB -69.0KB
Unknown metric groups

miscellaneous assets size

id before after diff
maps 958.9KB 530.1KB -428.9KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@nickpeihl nickpeihl merged commit c838709 into elastic:main Dec 7, 2021
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Dec 7, 2021
TinLe pushed a commit to TinLe/kibana that referenced this pull request Dec 22, 2021
gbamparop pushed a commit to gbamparop/kibana that referenced this pull request Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting [Deprecated-Use Team:Presentation]Team:Geo Former Team Label for Geo Team. Now use Team:Presentation release_note:enhancement v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants