-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Conversation
Pinging @elastic/kibana-gis (Team:Geo) |
import { syncLayerOrder } from './sort_layers'; | ||
|
||
import { | ||
addSpriteSheetToMapFromImageData, | ||
loadSpriteSheetImageData, | ||
removeOrphanedSourcesAndLayers, | ||
} from './utils'; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this 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); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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(() => { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
@elasticmachine merge upstream |
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
import { syncLayerOrder } from './sort_layers'; | ||
|
||
import { | ||
addSpriteSheetToMapFromImageData, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
@elasticmachine merge upstream |
merge conflict between base and head |
💚 Build Succeeded
Metrics [docs]Module Count
Async chunks
History
To update your PR or re-run it, just comment with: |
* Convert SVG maki icons to SDF on-the-fly
* Convert SVG maki icons to SDF on-the-fly
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.
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:
For maintainers