-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Icon image does not appear in legend unless it's shown on map #442
Comments
Hey @rdewit, thanks for the report. |
Hopefully it helps finding out what's causing it, @KaiVolland . I've tried all sorts of things in the app side of things (like pre-loading the SVGs) but none of them worked. Happy to help out where I can. |
Hey @rdewit, actually a timing/loading issue with the svg was my first guess too. Maybe i'll find some time in the next two weeks to have a closer look at this. But i can't promise. :/ |
I finally found out what's causing this: in Calling Unfortunately, I do not have the time to provide a PR at the moment. If someone wants to tackle this, I can provide my POC code for inspiration. |
Thanks for the diving into the issue for such a long time @rdewit. It would be great, if you could share your POC code here so that there is a point to start from. |
Thanks, @jansule. Below is the diff agains The problem comes down to Unfortunately It's relatively hard to reproduce this solution in I've tried creating a test for detecting when the image is loaded but even with trying to load inline images, the tests times out, so I must be doing something wrong there. Hopefully this gives a bit more of an insight. Happy to help out where I can. diff --git a/src/LegendRenderer/LegendRenderer.ts b/src/LegendRenderer/LegendRenderer.ts
index 1863f9d..218063f 100644
--- a/src/LegendRenderer/LegendRenderer.ts
+++ b/src/LegendRenderer/LegendRenderer.ts
@@ -3,6 +3,7 @@ import OlGeometry from 'ol/geom/Geometry';
import OlGeomPoint from 'ol/geom/Point';
import OlGeomPolygon from 'ol/geom/Polygon';
import OlGeomLineString from 'ol/geom/LineString';
+import OlImageState from 'ol/ImageState';
import OlStyle from 'ol/style/Style';
import Renderer from 'ol/render/canvas/Immediate';
import { create as createTransform } from 'ol/transform';
@@ -174,6 +175,7 @@ export class LegendRenderer {
symbolizers: rule.symbolizers
}]
};
+
return new Promise(async (resolve, reject) => {
function drawGeoms(){
geoms.forEach((geom: OlGeometry) => renderer.drawGeometry(geom));
@@ -187,11 +189,17 @@ export class LegendRenderer {
olStyle = <OlStyle | OlStyle[]>olStyle(new OlFeature(geoms[0]), 1);
}
if (Array.isArray(olStyle)) {
- olStyle.forEach((styleItem: OlStyle) => {
+ // Trigger loading of all images and wait for them to complete loading
+ await Promise.all(olStyle.map((styleItem) => this.loadStyleImage(styleItem)));
+
+ olStyle.forEach(async (styleItem: OlStyle) => {
renderer.setStyle(styleItem);
drawGeoms();
});
} else {
+ // Trigger loading of image and wait for it to complete loading
+ await this.loadStyleImage(olStyle);
+
// @ts-expect-error TODO fix type errors
renderer.setStyle(olStyle);
drawGeoms();
@@ -203,6 +211,34 @@ export class LegendRenderer {
});
};
+ loadStyleImage = (styleItem?: OlStyle) => new Promise(async (resolve, reject) => {
+ const imgLoaded = () => resolve(true);
+ const imgNotLoaded = () => resolve(false);
+
+ const styleItemImage = styleItem?.getImage();
+ if (!styleItemImage) {
+ return imgNotLoaded();
+ }
+
+ // Image is already loaded
+ if (styleItemImage.getImageState() === OlImageState.LOADED) {
+ return imgLoaded();
+ }
+
+ // Get the image element
+ const img = styleItemImage.getImage(1);
+ if (!img) {
+ return imgNotLoaded();
+ }
+
+ // @ts-expect-error: TODO: Handle ImageBitmaps
+ img.addEventListener?.('error', imgNotLoaded);
+ // @ts-expect-error: TODO: Handle ImageBitmaps
+ img.addEventListener?.('load', imgLoaded);
+
+ styleItemImage.load();
+ });
+
/**
* Render a single legend.
* @param {LegendConfiguration} config the legend config
|
Bug
Describe the bug
When a style contains an Icon with an image, the legend will not show it unless it's also visible on the map.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The legend image(s) should be visible even when the feature has not been drawn on the map (yet).
Screenshots
The screenshot shows the problem in our application: the icons for 'Accessible parking' and 'Off-leash area 6am to 9am only' do not show because the features that have those icons are not visible on the map. When we redraw the legend when those features are visible, they show as expected.
Note: the icons in the screenshot are cut off because geostyler-legend does not yet support offsets (which are used in this style). The style of these layers has 3 SVG icons: one for the 'bubble', one for the contents of the bubble and a separate shadow icon. The latter is showing for all styles in the screenshot because it's showing for at least one visible feature.
Environment:
This happens both on the latest versions of Firefox and Chrome and both on Linux and Windows.
Additional context
It seems that something is preventing the icon image(s) from loading (correctly) in the legend code. When looking at the network tab in the dev tools, I see the image being fetched. I have had similar issues in the past with our custom (non-geostyler) legend where when an image is first loaded without
crossOrigin: anonymous
and then again withoutcrossOrigin
, the map would refuse to show the image due to the browser serving the image from the cache that was stored withoutcrossOrigin
and tainting the canvas. I wonder if this might be something similar.The text was updated successfully, but these errors were encountered: