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

Cache dynamic icon styles to fix flickering #368

Merged
merged 2 commits into from
Aug 3, 2021
Merged
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
29 changes: 22 additions & 7 deletions src/OlStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class OlStyleParser implements StyleParser {
public static title = 'OpenLayers Style Parser';

title = 'OpenLayers Style Parser';
olIconStyleCache: any = {};

OlStyleConstructor: any = OlStyle;
OlStyleImageConstructor: any = OlStyleImage;
Expand Down Expand Up @@ -930,18 +931,32 @@ export class OlStyleParser implements StyleParser {
// if it contains a placeholder
// return olStyleFunction
const olPointStyledIconFn = (feature: any) => {
let src: string | undefined = OlStyleUtil.resolveAttributeTemplate(feature, symbolizer.image as string, '');
let src: string = OlStyleUtil.resolveAttributeTemplate(feature, symbolizer.image as string, '');
// src can't be blank, would trigger ol errors
if(!src) {
src = symbolizer.image;
src = symbolizer.image + '';
}
const image = new this.OlStyleIconConstructor({
...baseProps,
src // order is important
});
return new this.OlStyleConstructor({
let image;
if(this.olIconStyleCache[src]) {
image = this.olIconStyleCache[src];
image.setScale(baseProps.scale);
if(baseProps.rotation !== undefined) {
image.setRotation(baseProps.rotation);
}
if(baseProps.opacity !== undefined) {
image.setOpacity(baseProps.opacity);
}
} else {
image = new this.OlStyleIconConstructor({
...baseProps,
src // order is important
});
this.olIconStyleCache[src] = image;
}
const style = new this.OlStyleConstructor({
image
});
return style;
};
return olPointStyledIconFn;
} else {
Expand Down